oop - How to avoid to "fill" a generic class with attributes? -


i trying translate poker game correct oop model.
basics :

class hand {     card cards[]; } class game {     hand hands[]; } 

i games , hands text file. parse text file several times, several reasons:

  • get somes infos (reason 1)
  • compute stats (reason 2)
  • ...

for reason 1 need attributes (a1, b1) in class hand. reason 2, need other attributes (a2, b2). think dirty way :

class hand {     card cards[];     int a1,b1;     int a2,b2; } 

i mean attributes useless of time. so, cleaner, do:

class hand {     card cards[]; } class handforreason1 extends hand {     int a1,b1; } 

but feel using hammer...

my question : there intermediate way ? or hammer solution 1 ? (in case, correct semantic ?)

ps : design patterns welcome :-)
ps2 : strategy pattern hammer, isn't it?

* edit * here application :

// parse file, read game infos (reason 1)   // hand.a2 not needed here ! class parser_infos {        game game;        function parse()        {             game.hands[0].a1 = ...        }    }   // later, parse file , statistics (reason 2)   // hand.a1 not needed here ! class parser_stats   {       game game;       function parse()       {            game.hand[0].a2 = ...       }   }  

using chain of responsibility recognize poker hand do. since each hand has it's own characteristics, can't have generic hand.

something like

abstract class hand {    protected hand next;     abstract protected boolean recognizeimpl(card cards[]);     public hand setnext(hand next) {       this.next = next;       return next;    }     public boolean hand recognize(card cards[]) {       boolean result = ;       if (recognizeimpl(cards)) {          return this;       } else if (next != null) {          return next.recognize(cards);       } else {          return null;       }    } } 

and have implementation

class fullhouse extends hand {     protected boolean recognizeimpl(card cards[]) {         //...     } } class triplet extends hand {     protected boolean recognizeimpl(card cards[]) {         //...     } } 

then build chain

// chain start "best" hand first, want best hand // treated first, least hand last hand handchain = new fullhouse(); handchain   .setnext(new triplet())   //.setnext(...)     /* chain method */ ;  //...  hand besthand = handchain.recognize(cards); if (besthand != null) {    // given cards correspond best besthand } 

also, each hand it's own class, can initialize , have hold , compute specific things. since should manipulate hand classes as can (to stay oo possible), should avoid having cast hands specific hand class.

** update **

alright, answer original question (sig) class hand manipulating , treating "hands". if need calculate other statistics or other needs, wrapping hand class might not idea you'll end compound class, not desirable (for maintainability's sake , oop paradigm).

for reason 1, alright have different kinds of hands, chain of responsibility illustrate; can read file, create different kinds of hands many parameters required.

for reason 2, might @ other solutions. 1 have hand classes fire events (ex: when recognized) , application register hands other class listen events. other class should responsible collect necessary data files reading. since hand not (or should not be) responsible collect statistical data, bottom line need have else handle that.

one package = coherent api , functionalities

one class = coherent functionalities (a hand hand, not statistical container)

one method = (single) functionality (if method needs handle more 1 functionality, break functionalities separate private methods, , call them public method)

i'm giving generic answer here because reason 1 , reason 2 not specific.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -