File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Inheritance structure and polymorphic designs Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Inheritance structure and polymorphic designs" Watch "Inheritance structure and polymorphic designs" New topic
Author

Inheritance structure and polymorphic designs

Daniel Shilcock
Greenhorn

Joined: Mar 29, 2006
Posts: 3
Hi everyone,

I am hoping for some advice regarding inheritance.

Currently I am working in a system incorporating employees of various types. The base class is Employee, with other classes in the hierarchy requiring additional functionality and extending this class.

My question is one of good design. What is the best way to represent this relationship while allowing the type of an employee to be changed dynamically? And are there any existing design patterns that can be applied?

I have included a quick diagram to illustrate the problem.



Many Thanks,

Dan.
[ March 29, 2006: Message edited by: Daniel Shilcock ]
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
I wouldn't extend Employee to Worker or Admin. Instead, Employee has-a job:

Job getJob()
void setJob(Job job)

AWorker and Admin extend/implement Job. Then itis trivial for an employee object to dynamically change jobs, as well as an easy adjustment for an Employee to have more than one job.


There is no emoticon for what I am feeling!
Daniel Shilcock
Greenhorn

Joined: Mar 29, 2006
Posts: 3
I see what your saying there, its a very interesting design issue.

Would it not be possible to achieve a similar result making use of composition?

In this way the object's behaviour can be extended dynamically at runtime. I.E. the decorator pattern?
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
I would describe my suggestion as simply composition: an employee has-a job. I wouldn't elevate that to the status of a design pattern.

I'm not so sure treating this as a decorator is necessary. My favorite example of decorators in Java is I/O. You want to able to build up a pipeline of decorators:

new PrintWriter(new OutputStreamWriter(new BufferedO.S.(new FileO.S.(file)), charset)

I don't see that approach needed with employees, if you want the keep the design as simple as possible.
Daniel Shilcock
Greenhorn

Joined: Mar 29, 2006
Posts: 3
Yes i see the simplicity in your suggestion.

I was just thinking about adding behavior in the future without the need to modify existing code. And also the possibility to mix and match behaviors at run-time.

Thanks for your ideas! I really want to ensure the design is as solid as possible. Often too much emphasis is put on implementation and not nearly enough on good design!


[ March 29, 2006: Message edited by: Daniel Shilcock ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inheritance structure and polymorphic designs
 
Similar Threads
Polymorphism
Interfaces
Is this multiple inheritance
JPA Inheritance
Interfaces