• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Inheritance structure and polymorphic designs

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Men call me Jim. Women look past me to this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic