File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes OO, Patterns, UML and Refactoring and the fly likes Question related to dynamic resue of objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Question related to dynamic resue of objects" Watch "Question related to dynamic resue of objects" New topic
Author

Question related to dynamic resue of objects

Harish Vembu
Ranch Hand

Joined: Jul 19, 2004
Posts: 33



I was posed a question regarding dynamic reuse of class/objects at Runtime

Question:
I have a generic Person class which like a parent class. I have Pilot, Passenger and Flight Attendant class which are derived some properties and behavior from Parent.

During runtime usage sometimes Pilot could become a Passenger and vice versa for some segment of travel by implementing/not implementing /overriding/not overriding flyPlane() method.

How to model such a set of Classes(objects) so that we can interchange as desired.

We do not want to use Inheritance and Composition is the preferred way of achieving it. In other words use "Has A" relationship instead of "Is a" relationship.


Please provide full answer with code snippets. " Has a" relationship seems to be sometimes not straightforward to use in some instances as above.

Also mention what type of "Has a " relationship would be used a) Composition or b) Aggregation and why one over other in this case.


Thanks in Advance.





Harish
SCJP ,SCDJWS
Arnold Reuser
Ranch Hand

Joined: Nov 20, 2003
Posts: 196
Is that part of an assignment?
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

An object can't change its type. Once you've created an Pilot it will always be a Pilot. However one way of achieving similar functionality is to implement the State pattern.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Harish Vembu
Ranch Hand

Joined: Jul 19, 2004
Posts: 33


Thanks All for the replies.

I was of the belief a Decorator pattern would help in this case as it provides alternative to Subclassing and relies on Composition rather than inheritance for such interchangeable behaviour.

If State pattern is applicable. Can anyone provide a high level class diagram (UML representation) of this example under study?

Or other Design pattern like Template method/Strategy would be useful?

Casting subtype to Supertype like below and subsequent deleting it is not desired.

Pilot pilotObj = new Passenger();

Please provide some code snippets.


Thanks in Advance.

Harish

Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

There is an UML diagram on the page my link was referring to.

I wouldn't use the Decorator Pattern in for you problem since if Person would change then you would need to update all your references to that object since you want to wrap it.
I don't see how the Template Pattern can be of use here. The Strategy and State Pattern are similar. Just google for the differences between them.

And finally making a Passenger a Pilot doesn't sound like a good solution. I'm sometimes a Passenger but I'm not able to fly a plane (at least never tried it).
Harish Vembu
Ranch Hand

Joined: Jul 19, 2004
Posts: 33



Thanks for the reply. State pattern seems appropriate. In reference to the attached image on the reference link (Wiki)

There is an interface called State and implementing classes StateA and StateB and there is a Context class which is used for swapping between states .

In terms of UML representation I could understand the Dotted Line (Generalization) relationship between states and interface.

How is the empty diamond symbol (relationship) between Context and State can be explained?.

Is it a Has a relationship ? Aggregation or Composition?? or Something else ?

Please clarify. Sometimes I get more curious


Best Regards

Harish



[Thumbnail for State_Design_Pattern_UML_Class_Diagram.png]

Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

It means aggregation. However aggregation and composition are very similar. So choose carefully.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question related to dynamic resue of objects