OOD issue

I encounter a OOP design problem at my work, I want to

ask your suggestion, so if you have some free time to

spare, please help me out.

Let's say I have three classes, A, AHolder, and B.

class B extends A

class AHolder is a container of A, it have

constructors look something like this:

AHolder(X x, Y y, Z z) {

....

...

Queue.add(X.constructA(y,z));

...

..

}

it also have getter like this:

A getNext() {

....

}

now I want to have holder that also can hold B, B can

be constructed by call X.constructB(Y,Z)....based on

Y, determination can be made weather A or B should be

constructed.

the question is which of the following way is better?

1) modify the AHolder class to support to hold both A

and B, B is-a A after all. Since it's possible to

determine weather A or B should be constructed based

on the input y, the constructor can be modified to

look something like this

AHolder(X x,Y y,Z z) {

....

...

if(shouldConstructA(y)) Queue.add(X.constructA(y,z));

else Queue.add(X.constructB(y,z));

...

...

}

caller of the getNext() would have to type cast the

result to B if they are getting a B, they should know

when they need to do this, only thing is it is not obvious that they can use AHolder to hold B.

2)write a class BHolder that extends AHolder...

so the constructor for BHolder will look like

BHolder(X x, Y y, Z z) {

.... // repeat of the code in AHolder(x,y,z)

.... //

Queue.add(X.constructB(y,z)); // only different code

//more repeat of code....

}

B getNext() {

(B) super.getNext();

}

no type cast is needed by the caller...but the code is

repeatitive...and a default Constructor that does

nothing would have to be added to AHolder because

BHolder(..) have to call some constrcutor in super

class and It can not call the AHolder constructor

because the AHolder Constructor constructs A, not B...This

can be dangious because if someone uses the default

constructor, when they call getNext(), they will get a

null object.

I think the first way is better because it's simpler

and cleaner. If you agree, are there some kind of OOP

Design Pattern that address this issue so I can use it

to convince my team lead?

thanks

[2515 byte] By [joelyua] at [2007-9-28 9:33:00]
# 1

I quickly browsed through the code and i would say you are making a mistake somewhere wrt OOD.

The follwing are the reason i would say so.

1) Class B extends A, and still you want to typecast to B or A when you want to use it. Why is it happening so, if you want to reuse only the generic code from A, then move them to a utility class and have it aggregated in these classes.

2) If A and B are used interchangebly in a problem then they have to have an interface that define all the behaviour (common) of these classes.

3) Why the place holder creates these classes, create these classes and put it in the place holder. The reason i am suggesting this is, assume that you have to configure few other parameters when you create them, what will you do?

You are able to do this now because your classes might be are value centric and not behaviour centric.

If you think through the above points, then you will get a solution, write it here the forum will help you.

- Karthicraja

karthicrajaa at 2007-7-11 22:41:52 > top of Java-index,Other Topics,Patterns & OO Design...
# 2

I quickly browsed through the code and i would say you are making a mistake somewhere w.r.t OOD.

The following are the reason i would say so.

1) Class B extends A, and still you want to typecast to B or A when you want to use it. Why is it happening so, if you want to reuse only the generic code from A, then move them to a utility class and have it aggregated in these classes.

2) If A and B are used interchangeably in a problem then they have to have an interface that define all the behavior (common) of these classes.

3) Why the placeholder creates these classes, create these classes and put it in the placeholder. The reason i am suggesting this is, assume that you have to configure few other parameters when you create them, what will you do?

You are able to do this now because your classes might be are value centric and not behavior centric.

If you think through the above points, then you will get a solution; write it here the forum will help you.

Karthicraja

karthicrajaa at 2007-7-11 22:41:52 > top of Java-index,Other Topics,Patterns & OO Design...