Presentation is loading. Please wait.

Presentation is loading. Please wait.

Spring 2010ACS-3913 Ron McFadyen1 Duck Example Consider the text example (up to page 6). Each type of duck is a subclass of Duck Most subclasses implement.

Similar presentations


Presentation on theme: "Spring 2010ACS-3913 Ron McFadyen1 Duck Example Consider the text example (up to page 6). Each type of duck is a subclass of Duck Most subclasses implement."— Presentation transcript:

1 Spring 2010ACS-3913 Ron McFadyen1 Duck Example Consider the text example (up to page 6). Each type of duck is a subclass of Duck Most subclasses implement their own fly() and quack() operations Maintenance appears difficult as new duck types are required and as behaviour changes. As flying is seen to be subject to change there is motivation to extract that behaviour into its own class As quacking is seen to be subject to change there is motivation to extract that behaviour into its own class The above two points lead to applying the strategy pattern – once for flying and once for quacking.

2 Spring 2010ACS-3913 Ron McFadyen2 Consider that we have a family or collection of algorithms, one of which is to utilized at runtime by a client. When we utilize the strategy pattern we make the algorithms interchangeable and implement each in its own subclass of an interface or abstract class. At runtime the appropriate algorithm is associated with the client. Strategy Pattern

3 Spring 2010ACS-3913 Ron McFadyen3 Generic pattern Context … «interface» Strategy … ConcreteStrategy 1 … ConcreteStrategy n … … Note that the context knows only the strategy interface and not the concrete strategy types. This is all the context needs to know and this allows us to add/remove strategies without affecting the context.

4 Spring 2010ACS-3913 Ron McFadyen4 Generic pattern with collaboration Context … «interface» Strategy … ConcreteStrategy 1 … ConcreteStrategy n … … Strategy Concrete strategy strategy context The strategy participant declares the common interface for all supported algorithms. The context uses this interface to invoke an algorithm defined by a ConcreteStrategy. Each concrete strategy implements an algorithm using the strategy interface. The context participant is composed with a ConcreteStrategy object.

5 Spring 2010ACS-3913 Ron McFadyen5 Duck Example Duck display() performFly() setFlyBehavior() … «interface» FlyBehavior fly() MallardDuck display() … FlyWithWings fly() FlyNoWay fly() Quack quack() «interface» QuackBehavior quack() Squeak quack() Mute quack() flyBehavior quackBehavior DecoyDuck display() … … The algorithms that implement flying are each encapsulated in a subclass of FlyBehavior, and the algorithms that implement quacking are each encapsulated in a subclass of QuackBehavior. A duck is composed with one flying behaviour and one quacking behaviour.

6 Spring 2010ACS-3913 Ron McFadyen6 Duck Example Duck display() performFly() setFlyBehavior() … «interface» FlyBehavior fly() MallardDuck display() … FlyWithWings fly() FlyNoWay fly() Quack quack() «interface» QuackBehavior quack() Squeak quack() Mute quack() flyBehavior quackBehavior DecoyDuck display() … … Modify this class diagram to include the strategy collaborations. (There are two.)

7 Spring 2010ACS-3913 Ron McFadyen7 Duck Example Consider the text example. Examine the code to ensure you understand how the strategy pattern is implemented. Run the example. Three design principles are discussed. Identify the aspects of your application that vary and separate them from what stays the same Program to an interface and not an implementation Favour composition over inheritance

8 Spring 2010ACS-3913 Ron McFadyen8 Duck Example Draw an object diagram for the case immediately after the statement (page 21): Duck mallard = new MallardDuck(); Draw sequence diagrams to illustrate behaviour. What messages are sent When the Main() on page 21 executes? Duck mallard = new MallardDuck(); mallard.performQuack(); mallard.performFly();...

9 Spring 2010ACS-3913 Ron McFadyen9 Duck Example Suppose you are a developer and there is a requirement for a new quacking behavour. What must you change in the existing code? What classes must you write? Create a new algorithm for flying behavior for hopping. If two ducks must hop, must we instantiate a second hop object?


Download ppt "Spring 2010ACS-3913 Ron McFadyen1 Duck Example Consider the text example (up to page 6). Each type of duck is a subclass of Duck Most subclasses implement."

Similar presentations


Ads by Google