Note that there are some explanatory texts on larger screens.

plurals
  1. USDaniel Yankowsky
    primarykey
    data
    text
    plurals
    1. COThe idea behind the strategy pattern is to hide different behaviors behind a common interface. Applied correctly, you would have a Ball class which would have a field of type IBallStrategy. There would be two implementors of IBallStrategy: StandardBallStrategy and PoppingBallStrategy (of course, you can add more). When you construct a ball, you pass the strategy to the constructor. Ball's methods then call into the strategy as necessary. Ball only contains the behavior that is common to all balls. It needs to consult its strategy to make more specific decisions.
      singulars
    2. COYes, you would still need the enums for the Factories. I'm sorry that I wasn't clearer. Ideally, the factory would take the enum value, create an instance of a strategy, create an instance of Ball (and pass the instance of the strategy to the ball's constructor), and return the ball. Unless, of course, you could get rid of the factories... :)
      singulars
    3. COI think I see what you mean. From your UML, it looks like your board has a BoardType field, and your ball has a BallType field. My guess is that these are enums, and that you use an if or switch statement somewhere in your code to do different things depending on the BoardType field. This is typically the perfect case for a strategy pattern. The strategy is an interface, and you will have one implementation for each enum value. The enum goes away. This pushes the decision making from explicit "if" tests into the land of polymorphism.
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload