Cs 210
This presentation is the property of its rightful owner.
Sponsored Links
1 / 17

CS 210 PowerPoint PPT Presentation


  • 80 Views
  • Uploaded on
  • Presentation posted in: General

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

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Cs 210

CS 210

Introduction to Design Patterns

August 31, 2006


Introduction to design patterns

Introduction to Design Patterns

Chapter 1

Strategy Pattern


Design principle

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

In the Duck simulation context…

Parts that vary

Parts that stay the same

Duck

Behaviors

Flying Behaviors

Duck Class

Quacking Behaviors


Design principle1

Design Principle

  • Program to an interface, not to an implementation.

  • Really means program to a super type.


Implementing duck behaviors revisited

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


Integrating the duck behavior

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 context1

Duck

FlyBehavior: flyBehavior

QuackBehavior: quackBehavior

performQuack()

swim()

display()

performFly()

//other duck-like methods

In the Duck simulation context…

Duck

Behaviors

Flying Behaviors

Quacking Behaviors


Duck simulation recast using the new approach

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


Design principle2

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

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

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


Cs 210

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;

}


Cs 210

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;

}


Cs 210

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;

}


Cs 210

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;

}


Cs 210

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


  • Login