Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inheritance structure and polymorphic designs

 
Daniel Shilcock
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Daniel Shilcock
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic