Hi - A Class Design Doubt

hi i'm new to class designs - pls help to get the good solution.

i'm having 2 classed Customer and Category

Customer Contains...

CustomerId,CustomerName

Category Contains

CategoryId, Desc.

i've to design a class in a way that i've to specify the category of the customer.

is it feasible to add a Category class object to Customer class

ie

class Customer

{

String customerId;

String customerName;

Category cat;

}

but this gives the Customer access to Category Desc also. and i dont want that.

how to design this pls help

Thanking You.

Saju

[668 byte] By [saju_ma] at [2007-9-28 12:06:28]
# 1

Code your category like this:

public class Category{

public static final Category STUPID = new Category(1,"stupid");

public static final Category UGLY = new Category(2,"ugly");

public static final Category DUMB = new Category(3,"dumb");

private final int id;

private final String description;

private Category(int aId, String aDescription){

id = aId;

description = aDescription;

}

public int getId(){return id;}

public String getDescription(){return description;}

}

This way nobody can change the description of a category.

regards

spieler

spielera at 2007-7-12 3:04:44 > top of Java-index,Other Topics,Patterns & OO Design...
# 2

yeah boss,

but how will i represent it on customer

is it feasible to give access to the whole category.

class Category

{

-

-

}

pls tell me which of these is feasible

-

1.

class Customer

{

String id;

String name;

Category cat;

}

or

2.

class Customer

{

String id;

String name;

String cat;

}

which of these 2 customer class s good one.

if second is the good one how will i ensure the relation ship.

could u pls explain it to me...i'm trying to study the design conept.

thanking u

saju.m

saju_ma at 2007-7-12 3:04:44 > top of Java-index,Other Topics,Patterns & OO Design...
# 3

> yeah boss,

>

> but how will i represent it on customer

>

> is it feasible to give access to the whole category.

[..]

> 1.

>

> class Customer

> {

>String id;

>String name;

>Category cat;

> }

>

yes, this is the way to go. Due to the way Category is constructed no one (not the Customer not anybody else) can change the relation between Category.id and Category.description without changing the source of that class.

regards

Spieler

spielera at 2007-7-12 3:04:44 > top of Java-index,Other Topics,Patterns & OO Design...
# 4
boss, what will happen if i'm taking the category from the database. i didnt get ur point could u pls explain a bit more. i'm confused. Thank you Saju
saju_ma at 2007-7-12 3:04:44 > top of Java-index,Other Topics,Patterns & OO Design...
# 5

Ah, things are getting more complex.

One Option:

- loose the static final Categories

- add code like the following to your Category class (consider it pseudo code):

private static final Map categories = new HashMap();

public static void init(){

// read categories as a ResultSet

ResultSet rs = .... // your database access goes here

categories.clear();

while (rs.hasNext()){

Category cat = new Category(rs.getInt(0), rs.getString(1));

categories.put(cat);

}

}

public static Category getCategory(int id){

return categories.get(id);

}

So the available Categories come from the db. The con for this is that you have a dependency to jdbc in your categories.

If you want to get rid of this dependency but still keep

the Customer object or clients there of from fooling around with categories you need more then one class.

At leas one to create the Categories and the Category itself. But then it is quite challenging to ensure that nobody can create Rogue Categories.

regards Spieler

spielera at 2007-7-12 3:04:44 > top of Java-index,Other Topics,Patterns & OO Design...
# 6

Saju,

If I am reading this correctly, you want to know how to establish the relationship between Customer and Category, right? The way that you had identified it at first is fine:

public class Customer {

private String id;

private String name;

private Category cat;

// more stuff here

}

In addition, at some point you will need to have a container of Category objects to be able to choose from for a specific customer. There are a lot of options here - everything from an array up to a customized container. This container can then be loaded from the DB as identified in another post on this thread...

Then your challenge is to create a UI that will 1) display the customer info, 2) Display the container of possible categories, and 3) Allow you to pick a category and assign it to the displayed customer.

jonathanhousea at 2007-7-12 3:04:44 > top of Java-index,Other Topics,Patterns & OO Design...