Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Decorator Question

 
Kishore Dandu
Ranch Hand
Posts: 1934
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Kishore Dandu
Ranch Hand
Posts: 1934
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1934
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1934
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1934
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1934
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Kishore Dandu
Ranch Hand
Posts: 1934
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome!

And don't forget to tell us what you finally come up with!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic