This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes OO, Patterns, UML and Refactoring and the fly likes Simple OO Design Question... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Simple OO Design Question..." Watch "Simple OO Design Question..." New topic
Author

Simple OO Design Question...

Johnathan Baker
Greenhorn

Joined: Aug 08, 2008
Posts: 1
Dear All,

I apologize for not first contributing to this forum before asking my first question (Actually, I did contribute some 7 years ago when I did my first Java cert but the account is now lost).

Anyway, I have a relatively simple OO question to ask, at least it should be relatively simple for people that have a clue. I do not. I'm effectively a newbie all over again at this moment.

Here goes:

Assume the following three abstractions:

Person
- wakeUp()
- goToSleep()

and then Waiters:

Waiter
- takeCustomerOrder()

and then Managers:

Manager
- yellAtEmployees()



Waiters ARE People. Managers ARE People too (believe it or not).

Suppose Bob IS A Waiter.
Suppose Sue IS A Waiter and a Manager.

Here are my questions:

1) Should Waiter and Manager be interfaces that extend the Person interface?

i.e.

interface Waiter extends Person
interface Manager extends Person


then it would result in:

class Bob implements Waiter
class Sue implements Waiter, Manager

2)Suppose now that ALL people share some base functionality, is the following a legitimate way to go about things:

class AbstractPerson implements Person { implements some base functionality }

Then to get the inherited base functionality:

class Bob extends AbstractPerson implements Waiter
class Sue extends AbstractPerson implements Waiter, Manager

3) Suppose now that even though all Persons have a wakeUp() operation, depending on the type of the person, the wakeUp() operation takes different arguments, is it legitimate to pass in a collection of some sort to the wakeUp() operation or rather an object that implements another specified interface e.g.

wakeUp(IWakeUpParameters parms)

And then have different wake up parameters for Sue and Bob

e.g.
class BobWakeUpParameters implements IWakeUpParameters
class SueWakeUpParameters implements IWakeUpParameters

Then when implementing the wakeUp() operation for Bob and Sue it would be something like:

in class Bob:
wakeUp(BobWakeUpParameters bwup)

in class Sue:
wakeUp(SueWakeUpParameters swup)

In this case it's starting to look like a family of related classes e.g.
Sue + SueWakeUpParameters
Bob + BobWakeUpParameters

...so does the Abstract Factory design pattern come in to play here?

Sorry if that's too much information for one post. Any advice, help appreciated.

JB.
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 861
Sorry if that's too much information for one post.

That it is. My answer will probably be a mess, as the only way to answer this is to know in more detail what you are modeling.

For example if you have other things that need to wakeUp and goToSleep you might want to have something more abstract than Person. Also, in your domain if you don't care about the sleeping aspects of Waiters and Managers (say you only care about their behaviours during working hours and they don't sleep on the job) then the Person class may not be needed.

1) Should Waiter and Manager be interfaces that extend the Person interface?

Seems reasonable.


class Bob implements Waiter
class Sue implements Waiter, Manager

This doesn't feel right. I would be careful about having names of people as classes. Name should be a property of a class.

2)Suppose now that ALL people share some base functionality, is the following a legitimate way to go about things:

Not sure if you need an abstract person or not. You may be able to create a concrete person object that has an Occupation class where Waiter and Manager are different instantiations of Occupation. If you are both a Waiter and Manager perhaps this could be a composite.


3) Suppose now that even though all Persons have a wakeUp() operation, depending on the type of the person, the wakeUp() operation takes different arguments, is it legitimate to pass in a collection of some sort to the wakeUp() operation or rather an object that implements another specified interface e.g.

If possible I would avoid this. If needed I would rather pass in the parameters to a constructor and keep the interface simply wakeUp().


In this case it's starting to look like a family of related classes e.g.
Sue + SueWakeUpParameters
Bob + BobWakeUpParameters


Are there 2 types of wakeup Persons and sue and bob are 2 instances of them? That would be much more flexible. Not sure what types of parameters would be needed to determine wakeup behaviour, but it sounds like this may need to be a WakeUp interface, and possibly person is just a collection of various implementations such as WakeUp and Occupation.
[ August 10, 2008: Message edited by: steve souza ]

http://www.jamonapi.com/ - a fast, free open source performance tuning api.
JavaRanch Performance FAQ
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Simple OO Design Question...