encapsulate what varies - viucsci.viu.ca/~barskym/teaching/oop2012/lecture 11... · strategy design...

25
Strategy pattern Lecture 11

Upload: others

Post on 22-Aug-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Encapsulate what varies - VIUcsci.viu.ca/~barskym/teaching/OOP2012/Lecture 11... · Strategy design pattern •Defines a family of algorithms, encapsulates them, and makes them interchangeable

Strategy pattern

Lecture 11

Page 2: Encapsulate what varies - VIUcsci.viu.ca/~barskym/teaching/OOP2012/Lecture 11... · Strategy design pattern •Defines a family of algorithms, encapsulates them, and makes them interchangeable

Inheritance syntax reminder

• Class Art

• Drawing extends Art

• Cartoon extends Drawing

• The object of class cartoon is created from the base class outwards

Page 3: Encapsulate what varies - VIUcsci.viu.ca/~barskym/teaching/OOP2012/Lecture 11... · Strategy design pattern •Defines a family of algorithms, encapsulates them, and makes them interchangeable

Non-default constructors: reminder

• Game

• BoardGame

• Chess

Page 4: Encapsulate what varies - VIUcsci.viu.ca/~barskym/teaching/OOP2012/Lecture 11... · Strategy design pattern •Defines a family of algorithms, encapsulates them, and makes them interchangeable

Final keyword

• Final class attribute – can’t change a value (what does it mean for attributes-objects?)

• Final method – can’t override a method

– Private methods are final by default

• Final class – can’t extend class

Page 5: Encapsulate what varies - VIUcsci.viu.ca/~barskym/teaching/OOP2012/Lecture 11... · Strategy design pattern •Defines a family of algorithms, encapsulates them, and makes them interchangeable

Ducks simulator design

Duck

flyBehavior: FlyBehavior quackBehavior: QuackBehavior

display (); performFly() performQuack() swim() setFlyBehavior(FlyBehavior) setQuackBehavior (QuackBehavior)

<<interface>> FlyBehavior

fly();

FlyWithWings

fly() FlyNoWay

fly()

<<interface>> QuackBehavior

quack();

Quack

quack() Squeak

quack() MallardDuck

display() RubberDuck

display()

WoodenDuck

display()

Page 6: Encapsulate what varies - VIUcsci.viu.ca/~barskym/teaching/OOP2012/Lecture 11... · Strategy design pattern •Defines a family of algorithms, encapsulates them, and makes them interchangeable

Duck: abstract class with abstract method: display public abstract class Duck {

FlyBehavior flyBehavior;

QuackBehavior quackBehavior;

public Duck() {

}

abstract void display();

public void performFly() {

flyBehavior.fly();

}

public void performQuack() {

quackBehavior.quack();

}

public void swim() {

System.out.println("All ducks float, even decoys!");

}

}

public void setFlyBehavior (FlyBehavior fb) { flyBehavior = fb; } public void setQuackBehavior(QuackBehavior qb) { quackBehavior = qb; }

Page 7: Encapsulate what varies - VIUcsci.viu.ca/~barskym/teaching/OOP2012/Lecture 11... · Strategy design pattern •Defines a family of algorithms, encapsulates them, and makes them interchangeable

Subclass: MallardDuck

public class MallardDuck extends Duck {

public MallardDuck() {

quackBehavior = new Quack();

flyBehavior = new FlyWithWings();

}

public void display() {

System.out.println("I'm a real Mallard duck");

}

}

Page 8: Encapsulate what varies - VIUcsci.viu.ca/~barskym/teaching/OOP2012/Lecture 11... · Strategy design pattern •Defines a family of algorithms, encapsulates them, and makes them interchangeable

Subclass: RubberDuck

public class RubberDuck extends Duck {

public RubberDuck() {

flyBehavior = new FlyNoWay();

quackBehavior = new Squeak();

}

public void display() {

System.out.println("I'm a rubber duckie");

}

}

Page 9: Encapsulate what varies - VIUcsci.viu.ca/~barskym/teaching/OOP2012/Lecture 11... · Strategy design pattern •Defines a family of algorithms, encapsulates them, and makes them interchangeable

Subclass: WoodenDuck

public class WoodenDuck extends Duck {

public WoodenDuck () {

setFlyBehavior(new FlyNoWay());

setQuackBehavior(new MuteQuack());

}

public void display() {

System.out.println("I'm a Wooden Decoy duck");

}

}

Page 10: Encapsulate what varies - VIUcsci.viu.ca/~barskym/teaching/OOP2012/Lecture 11... · Strategy design pattern •Defines a family of algorithms, encapsulates them, and makes them interchangeable

Interface FlyBehavior

public interface FlyBehavior {

public void fly();

}

Page 11: Encapsulate what varies - VIUcsci.viu.ca/~barskym/teaching/OOP2012/Lecture 11... · Strategy design pattern •Defines a family of algorithms, encapsulates them, and makes them interchangeable

Implementation of FlyBehavior: FlyWithWings

public class FlyWithWings implements FlyBehavior {

public void fly() {

System.out.println("I'm flying!!");

}

}

Page 12: Encapsulate what varies - VIUcsci.viu.ca/~barskym/teaching/OOP2012/Lecture 11... · Strategy design pattern •Defines a family of algorithms, encapsulates them, and makes them interchangeable

Implementation of FlyBehavior: no fly

public class FlyNoWay implements FlyBehavior {

public void fly() {

System.out.println("I can't fly");

}

}

Page 13: Encapsulate what varies - VIUcsci.viu.ca/~barskym/teaching/OOP2012/Lecture 11... · Strategy design pattern •Defines a family of algorithms, encapsulates them, and makes them interchangeable

Interface: QuackBehavior

public interface QuackBehavior {

public void quack();

}

Page 14: Encapsulate what varies - VIUcsci.viu.ca/~barskym/teaching/OOP2012/Lecture 11... · Strategy design pattern •Defines a family of algorithms, encapsulates them, and makes them interchangeable

Implementation of QuackBehavior: real quack

public class Quack implements QuackBehavior {

public void quack() {

System.out.println("Quack");

}

}

Page 15: Encapsulate what varies - VIUcsci.viu.ca/~barskym/teaching/OOP2012/Lecture 11... · Strategy design pattern •Defines a family of algorithms, encapsulates them, and makes them interchangeable

Implementation of QuackBehavior: squeak

public class Squeak implements QuackBehavior {

public void quack() {

System.out.println("Squeak");

}

}

Page 16: Encapsulate what varies - VIUcsci.viu.ca/~barskym/teaching/OOP2012/Lecture 11... · Strategy design pattern •Defines a family of algorithms, encapsulates them, and makes them interchangeable

Implementation of QuackBehavior: silence

public class MuteQuack implements QuackBehavior {

public void quack() {

System.out.println("<< Silence >>");

}

}

Page 17: Encapsulate what varies - VIUcsci.viu.ca/~barskym/teaching/OOP2012/Lecture 11... · Strategy design pattern •Defines a family of algorithms, encapsulates them, and makes them interchangeable

Simulator public class MiniDuckSimulator {

public static void main(String[] args) {

Duck mallard = new MallardDuck();

Duck rubberDuckie = new RubberDuck();

Duck decoy = new WoodenDuck();

mallard.performQuack();

rubberDuckie.performQuack();

decoy.performQuack();

mallard.performFly();

rubberDuckie.performFly(); decoy.performFly();

}

}

Page 18: Encapsulate what varies - VIUcsci.viu.ca/~barskym/teaching/OOP2012/Lecture 11... · Strategy design pattern •Defines a family of algorithms, encapsulates them, and makes them interchangeable

Change 1: implementation of new FlyBehavior: Rocket-powered fly

public class FlyRocketPowered implements FlyBehavior {

public void fly() {

System.out.println("I'm flying with a rocket");

}

}

Page 19: Encapsulate what varies - VIUcsci.viu.ca/~barskym/teaching/OOP2012/Lecture 11... · Strategy design pattern •Defines a family of algorithms, encapsulates them, and makes them interchangeable

Change 2: Additional Implementation of new QuackBehavior: Russian quack

public class RussianQuack implements QuackBehavior {

public void quack() {

System.out.println("Qwa");

}

}

Page 20: Encapsulate what varies - VIUcsci.viu.ca/~barskym/teaching/OOP2012/Lecture 11... · Strategy design pattern •Defines a family of algorithms, encapsulates them, and makes them interchangeable

Change 3: new sub-class of Duck

public class ModelDuck extends Duck {

public ModelDuck() {

flyBehavior = new FlyNoWay();

quackBehavior = new Quack();

}

public void display() {

System.out.println("I'm a model duck");

}

}

Page 21: Encapsulate what varies - VIUcsci.viu.ca/~barskym/teaching/OOP2012/Lecture 11... · Strategy design pattern •Defines a family of algorithms, encapsulates them, and makes them interchangeable

Changing behavior at run tine

public class MiniDuckSimulator {

public static void main(String[] args) {

ModelDuck model= new ModelDuck();

model.performFly();

model.setFlyBehavior(new FlyRocketPowered());

model.performFly();

}

}

Page 22: Encapsulate what varies - VIUcsci.viu.ca/~barskym/teaching/OOP2012/Lecture 11... · Strategy design pattern •Defines a family of algorithms, encapsulates them, and makes them interchangeable

Actor changes behavior

Page 23: Encapsulate what varies - VIUcsci.viu.ca/~barskym/teaching/OOP2012/Lecture 11... · Strategy design pattern •Defines a family of algorithms, encapsulates them, and makes them interchangeable

Strategy design pattern

• Defines a family of algorithms, encapsulates them, and makes them interchangeable by using a common interface

• Strategy lets the algorithm vary independently from clients that use it

Page 24: Encapsulate what varies - VIUcsci.viu.ca/~barskym/teaching/OOP2012/Lecture 11... · Strategy design pattern •Defines a family of algorithms, encapsulates them, and makes them interchangeable

OOP Design Principles used in strategy pattern

• Encapsulate what varies and pull it away from what stays the same

• Program to an interface not to an implementation

Page 25: Encapsulate what varies - VIUcsci.viu.ca/~barskym/teaching/OOP2012/Lecture 11... · Strategy design pattern •Defines a family of algorithms, encapsulates them, and makes them interchangeable

OOP concepts used in Strategy pattern

• Encapsulation

• Composition

• Inheritance