Encapsulate what varies - VIUcsci.viu.ca/~barskym/teaching/OOP2012/Lecture 11... · Strategy design...
Transcript of Encapsulate what varies - VIUcsci.viu.ca/~barskym/teaching/OOP2012/Lecture 11... · Strategy design...
![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](https://reader036.fdocuments.in/reader036/viewer/2022071017/5fd07456da14dd60911d328e/html5/thumbnails/1.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022071017/5fd07456da14dd60911d328e/html5/thumbnails/2.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022071017/5fd07456da14dd60911d328e/html5/thumbnails/3.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022071017/5fd07456da14dd60911d328e/html5/thumbnails/4.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022071017/5fd07456da14dd60911d328e/html5/thumbnails/5.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022071017/5fd07456da14dd60911d328e/html5/thumbnails/6.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022071017/5fd07456da14dd60911d328e/html5/thumbnails/7.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022071017/5fd07456da14dd60911d328e/html5/thumbnails/8.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022071017/5fd07456da14dd60911d328e/html5/thumbnails/9.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022071017/5fd07456da14dd60911d328e/html5/thumbnails/10.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022071017/5fd07456da14dd60911d328e/html5/thumbnails/11.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022071017/5fd07456da14dd60911d328e/html5/thumbnails/12.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022071017/5fd07456da14dd60911d328e/html5/thumbnails/13.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022071017/5fd07456da14dd60911d328e/html5/thumbnails/14.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022071017/5fd07456da14dd60911d328e/html5/thumbnails/15.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022071017/5fd07456da14dd60911d328e/html5/thumbnails/16.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022071017/5fd07456da14dd60911d328e/html5/thumbnails/17.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022071017/5fd07456da14dd60911d328e/html5/thumbnails/18.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022071017/5fd07456da14dd60911d328e/html5/thumbnails/19.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022071017/5fd07456da14dd60911d328e/html5/thumbnails/20.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022071017/5fd07456da14dd60911d328e/html5/thumbnails/21.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022071017/5fd07456da14dd60911d328e/html5/thumbnails/22.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022071017/5fd07456da14dd60911d328e/html5/thumbnails/23.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022071017/5fd07456da14dd60911d328e/html5/thumbnails/24.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022071017/5fd07456da14dd60911d328e/html5/thumbnails/25.jpg)
OOP concepts used in Strategy pattern
• Encapsulation
• Composition
• Inheritance