1 / 17

CS 210

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

sidney
Download Presentation

CS 210

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 210 Introduction to Design Patterns August 31, 2006

  2. Introduction to Design Patterns Chapter 1 Strategy Pattern

  3. 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.

  4. In the Duck simulation context… Parts that vary Parts that stay the same Duck Behaviors Flying Behaviors Duck Class Quacking Behaviors

  5. Design Principle • Program to an interface, not to an implementation. • Really means program to a super type.

  6. <<interface>> FlyBehavior <<interface>> QuackBehavior fly() quack() FlyWithWings Quack FlyNoWay Squeak MuteQuack fly(){ // implements duck flying } quack(){ // implements duck quacking } fly(){ // do nothing – Can’t fly } quack(){ // implements duck squeak } quack(){ // do nothing – Can’t quack } Implementing duck behaviors - revisited

  7. Integrating the duck behavior • Key now is that Duck class will delegate its flying and quacking behavior instead of implementing these itself.

  8. Duck FlyBehavior: flyBehavior QuackBehavior: quackBehavior performQuack() swim() display() performFly() //other duck-like methods In the Duck simulation context… Duck Behaviors Flying Behaviors Quacking Behaviors

  9. <<interface>> FlyBehavior Duck fly() FlyBehavior: flyBehavior QuackBehavior: quackBehavior performQuack() performFly() setFlyBehavior() setQuackBehavior() swim() display() FlyWithWings FlyNoWay fly() // implements duck flying fly() // do nothing – Can’t fly <<interface>> QuackBehavior quack() DecoyDuck MallardDuck RubberDuck Quack Squeak quack() // implements squeak quack() // implements duck quacking RedHeadDuck display() display() display() display() Mutequack quack() // do nothing Duck simulation recast using the new approach

  10. Design Principle • Favor composition over inheritance • HAS-A can be better than IS-A • Allows changing behavior at run time

  11. 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.

  12. 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

  13. KnifeBehavior AxeBehavior SpearBehavior BowAndArrowBehavior useWeapon() //implements cutting with // a knife useWeapon() //implements fight with // an axe useWeapon() //implements fight with // a spear useWeapon() //implements fight with // bow and arrows Character WeaponBehavior weapon; fight(); King Queen Bishop Knight <<interface>> WeaponBehavior fight() fight() fight() fight() useWeapon() setWeapon(WeaponBehavior w){ this.weapon = w; }

  14. KnifeBehavior AxeBehavior SpearBehavior BowAndArrowBehavior useWeapon() //implements cutting with // a knife useWeapon() //implements fight with // an axe useWeapon() //implements fight with // a spear useWeapon() //implements fight with // bow and arrows Character WeaponBehavior weapon; fight(); King Queen Bishop Knight <<interface>> WeaponBehavior fight() fight() fight() fight() useWeapon() Abstract setWeapon(WeaponBehavior w){ this.weapon = w; }

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

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

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

More Related