Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question related to dynamic resue of objects

 
Harish Vembu
Ranch Hand
Posts: 33
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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.



 
Arnold Reuser
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is that part of an assignment?
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Harish Vembu
Ranch Hand
Posts: 33
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
State_Design_Pattern_UML_Class_Diagram.png
[Thumbnail for State_Design_Pattern_UML_Class_Diagram.png]
State Design Pattern
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It means aggregation. However aggregation and composition are very similar. So choose carefully.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic