aspose file tools*
The moose likes OO, Patterns, UML and Refactoring and the fly likes Decorator Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Decorator Question" Watch "Decorator Question" New topic
Author

Decorator Question

Kishore Dandu
Ranch Hand

Joined: Jul 10, 2001
Posts: 1934
I have the following class design question related to Decorator pattern.

I am trying to design a class hierarchy for a baseball team. It has a Player(with all the general properties like name, college history, age etc). But the player can have multiple positional aspects like 1st base man, outfielder, catcher, pitcher, relief pitcher etc. As you can expect the same player can have multiple positional statistics.

My question is, can i use Decorator to add the positional aspects as concrete decorators instread of creating multiple sub-classes and creating a player object using a helper class.

Any thoughts along these lines are welcome..


Kishore
SCJP, blog
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
This qestion cannot really be answered until we know what the responsibility - in terms of behaviour - of those objects will be.

What operations will be on the player class? How will it be used?

How will the behaviour differ between positions?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Kishore Dandu
Ranch Hand

Joined: Jul 10, 2001
Posts: 1934
Originally posted by Ilja Preuss:
This qestion cannot really be answered until we know what the responsibility - in terms of behaviour - of those objects will be.

What operations will be on the player class? How will it be used?

How will the behaviour differ between positions?


Player will have operation like getStats(), getBirthDay() etc.

The concrete decorators(example itcher) will have getStats() but with the pitcher related stats returned.

So the difference between different positions is: they have a different set of statistics.

My idea is 'A player object will exist in the system when instantiated'; but the app will add the player position decorators when getStats is called.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
So does a player have a number of positions, and different stats for each position; or does he not have any stats at all until he is assigned a position?

As an aside, the "operations" you cite - basically a bunch of getters - makes for very weak objects. It's much better to define objects by their *behaviour* than by their *data* - the latter leads to procedural programming. See http://c2.com/cgi/wiki?TellDontAsk
Kishore Dandu
Ranch Hand

Joined: Jul 10, 2001
Posts: 1934
Originally posted by Ilja Preuss:
So does a player have a number of positions, and different stats for each position; or does he not have any stats at all until he is assigned a position?

As an aside, the "operations" you cite - basically a bunch of getters - makes for very weak objects. It's much better to define objects by their *behaviour* than by their *data* - the latter leads to procedural programming. See http://c2.com/cgi/wiki?TellDontAsk


A player can have multiple positions. Each position will have different stats.

If i have bunch of data(related to a position) what would you suggest to model along behaviour?? Is there a pattern that can take care of this scenario.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Kishore Dandu:
A player can have multiple positions. Each position will have different stats.


Why wouldn't it be a good idea to model it that way, too?


If i have bunch of data(related to a position) what would you suggest to model along behaviour??


Data alone doesn't make a program. You probably want the program to *do* something, don't you? What is it that the program should do?

Is there a pattern that can take care of this scenario.


We don't yet have a scenario. All we have is a bunch of data...
Kishore Dandu
Ranch Hand

Joined: Jul 10, 2001
Posts: 1934
Originally posted by Ilja Preuss:


We don't yet have a scenario. All we have is a bunch of data...


Ok here is the scenario:
" The baseball team player has basic details. Player has multiple positional statistics. The system needs to print the above positional stats as needed for output purposes".
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Ok, so printing is all the program has to do?

So:


quote:
--------------------------------------------------------------------------------
Originally posted by Kishore Dandu:
A player can have multiple positions. Each position will have different stats.

--------------------------------------------------------------------------------



Why wouldn't it be a good idea to model it that way, too?


Or what would you hope to get from using the Decorator pattern?
Kishore Dandu
Ranch Hand

Joined: Jul 10, 2001
Posts: 1934
Originally posted by Ilja Preuss:
Ok, so printing is all the program has to do?

So:



Or what would you hope to get from using the Decorator pattern?


I plan to add the positions(and the stats that are part of the position) at run time.

But are you suggesting to use a position abstract & multiple concrete position classes to be used by the Player class??
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Kishore Dandu:
I plan to add the positions(and the stats that are part of the position) at run time.


So a player can only have one position at a time?

Where are the stats coming from? How do you decide which position a player has?



But are you suggesting to use a position abstract & multiple concrete position classes to be used by the Player class??


No, not really. Until now we were only talking about players and positions, as far as I can tell.

But regarding stats, how would an abstract position help? Could you make use of any polymorphic behaviour?

And are stats more than just collections of name-value-pairs?
Kishore Dandu
Ranch Hand

Joined: Jul 10, 2001
Posts: 1934
Originally posted by Ilja Preuss:


No, not really. Until now we were only talking about players and positions, as far as I can tell.

But regarding stats, how would an abstract position help? Could you make use of any polymorphic behaviour?

And are stats more than just collections of name-value-pairs?


A player can have more than one position(example pitcher can also have hitting statistics). Stats are available from a data base & usually saved by a player id.

The trick part is some stats are not only name, value pairs the value is for a certain breakdown(example: number of wins but in case of home ground).
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Frankly, I don't have a clear picture of what it looks like. Perhaps a concrete example would help.

You might also get some ideas from reading http://www.martinfowler.com/apsupp/properties.pdf
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
It looks like you are still only considering the data involved, there's still no mention of behaviour.

To me, this sounds like you do not (so far) need any domain objects (such as Player, Pitcher, or whatever) in your system at all, just a bunch of instances of some general purpose table class, with methods such as displayStatistics(playerId, positionId).

Please have a think about how and when the data might change, what rules apply to possible changes, and what other data or user-visible information might need to change as a result. Then we can begin discussing these changes, and maybe consider them as methods and build classes to gather together messages which share things.

Does that make any sense?


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Kishore Dandu
Ranch Hand

Joined: Jul 10, 2001
Posts: 1934
Originally posted by Ilja Preuss:
Frankly, I don't have a clear picture of what it looks like. Perhaps a concrete example would help.

You might also get some ideas from reading http://www.martinfowler.com/apsupp/properties.pdf


This link is of very good help. I think i am going to drop the idea of Decorator(and the maintenance complexity that may go with it). Since we are talking about properties only; without a lot of behavioral changes; i would take one of the properties approaches suggested by Martin.

Thanks all for your insights.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
You're welcome!

And don't forget to tell us what you finally come up with!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Decorator Question