CS_210_August_31 by wanghonghx

VIEWS: 10 PAGES: 17

									      CS 210


Introduction to Design
       Patterns
   August 31, 2006
Introduction to Design
       Patterns


        Chapter 1
     Strategy Pattern
Design Principle
Identify the aspects of your application that
  vary and separate them from what stays
  the same.
                     OR
Take the parts that vary and encapsulate
  them, so that later you can alter or
  extend the parts that vary without
  affecting those that don’t.
  In the Duck simulation context…

                           Parts that vary
Parts that stay the same


                           Duck
                           Behaviors
Design Principle


   Program to an interface, not to an
    implementation.

   Really means program to a super type.
    Implementing duck behaviors -
    revisited
        <<interface>>                                     <<interface>>
        FlyBehavior                                       QuackBehavior

         fly()                                             quack()




                                           Quack                         MuteQuack

                                          quack(){                     quack(){
                                          // implements duck           // do nothing –
 FlyWithWings            FlyNoWay         quacking                     Can’t quack
                                          }                            }
fly(){                  fly(){
// implements duck      // do nothing –                   Squeak
flying                  Can’t fly
}                       }                               quack(){
                                                        // implements duck
                                                        squeak
                                                        }
Integrating the duck behavior
   Key now is that Duck class will delegate
    its flying and quacking behavior instead
    of implementing these itself.
    In the Duck simulation context…


           Duck

FlyBehavior: flyBehavior       Duck
QuackBehavior: quackBehavior
                               Behaviors
performQuack()
swim()
display()
performFly()
//other duck-like methods
        Duck simulation recast using
        the new approach
                                                                       <<interface>>
                                                                       FlyBehavior
                     Duck
                                                                        fly()
              FlyBehavior: flyBehavior
              QuackBehavior: quackBehavior                      FlyWithWings          FlyNoWay
                                                             fly()                 fly()
              performQuack()                                 // implements duck    // do nothing –
              performFly()                                   flying                Can’t fly
              setFlyBehavior()
                                                                           <<interface>>
              setQuackBehavior()
                                                                           QuackBehavior
              swim()
              display()                                                     quack()

                                                                 Quack                 Squeak
                                                            quack()               quack()
MallardDuck     RedHeadDuck        RubberDuck   DecoyDuck   // implements duck    // implements
                                                            quacking              squeak
 display()        display()        display()    display()
                                                                           Mutequack
                                                                    quack()
                                                                    // do nothing
Design Principle


   Favor composition over inheritance

   HAS-A can be better than IS-A
   Allows changing behavior at run time
    The strategy pattern



The Strategy Pattern defines a family of algorithms,
Encapsulates each one, and makes them
interchangeable. Strategy lets the algorithm vary
independently from clients that use it.
Rationale for design patterns
   Shared pattern vocabularies are
    powerful
   Patterns allow you to say more with less
   Reusing tried and tested methods
   Focus is on developing flexible,
    maintainable programs
           Character                       KnifeBehavior              BowAndArrowBehavior

WeaponBehavior weapon;               useWeapon()                     useWeapon()
                                     //implements cutting with       //implements fight with
fight();                             // a knife                      // bow and arrows


       AxeBehavior                       <<interface>>
                                                                   Queen
                                         WeaponBehavior
useWeapon()                                                        fight()
//implements fight with             useWeapon()
// an axe

                                                                 SpearBehavior
  King                    Knight        Bishop             useWeapon()
  fight()                                                  //implements fight with
                          fight()       fight()
                                                           // a spear




                       setWeapon(WeaponBehavior w){
                              this.weapon = w;
                       }
        Abstract
            Character

 WeaponBehavior weapon;

 fight();                                           KnifeBehavior

                                              useWeapon()
                                              //implements cutting with           BowAndArrowBehavior
                                              // a knife
                                                                                 useWeapon()
       AxeBehavior                                                               //implements fight with
                                     <<interface>>                               // bow and arrows
                                     WeaponBehavior             Queen
useWeapon()
//implements fight with                                         fight()
                                useWeapon()
// an axe

                                                                                SpearBehavior

                 King              Knight             Bishop              useWeapon()
                                   fight()            fight()             //implements fight with
                  fight()
                                                                          // a spear


                            setWeapon(WeaponBehavior w){
                                   this.weapon = w;
                            }
        Abstract                                             Behavior Interface
            Character                                              <<interface>>
                                                                   WeaponBehavior
 WeaponBehavior weapon;
                                                             useWeapon()
 fight();




                                                                                   BowAndArrowBehavior
       AxeBehavior                     KnifeBehavior
                                                                                  useWeapon()
                                                                 Queen            //implements fight with
useWeapon()                      useWeapon()
                                                                                  // bow and arrows
//implements fight with          //implements cutting with       fight()
// an axe                        // a knife

                                                                                 SpearBehavior

                 King             Knight               Bishop              useWeapon()
                                  fight()              fight()             //implements fight with
                  fight()
                                                                           // a spear


                            setWeapon(WeaponBehavior w){
                                   this.weapon = w;
                            }
            Abstract                                  Behavior Interface
              Character                                         <<interface>>
                                                                WeaponBehavior
   WeaponBehavior weapon;
                                                          useWeapon()
   fight();


                                            AxeBehavior                          BowAndArrowBehavior
King                      Knight
fight()                   fight()     useWeapon()                                useWeapon()
                                      //implements fight with                    //implements fight with
                                      // an axe                                  // bow and arrows

       Bishop        Queen
                                                   KnifeBehavior                 SpearBehavior
       fight()       fight()
                                            useWeapon()                  useWeapon()
                                            //implements cutting with    //implements fight with
                                            // a knife                   // a spear




                               setWeapon(WeaponBehavior w){
                                      this.weapon = w;
                               }
                                                      Behavior Interface
            Abstract
                                                              <<interface>>
          Character                                           WeaponBehavior
WeaponBehavior weapon;
                                                        useWeapon()
fight();
setWeapon(WeaponBehavior w){
     this.weapon = w;                                                          BowAndArrowBehavior
                                          AxeBehavior
}
                                    useWeapon()                                useWeapon()
                                    //implements fight with                    //implements fight with
                                    // an axe                                  // bow and arrows
King                      Knight
fight()                   fight()
                                                 KnifeBehavior                 SpearBehavior

                                          useWeapon()                  useWeapon()
       Bishop         Queen               //implements cutting with    //implements fight with
                                          // a knife                   // a spear
       fight()        fight()

								
To top