Design Patterns: Strategy Pattern
Design Patterns: Strategy Pattern
2014
Design Patterns
Strategy Pattern
© 2014 by Prof. Christian Madritsch
Literature
• E. Freeman, Head First Design Patterns, O‘Reilly, 2005
• E. Gamma, R. Helm, R. Johnson, J. Vlissides, Design Patterns.
Elements of Reusable Object‐Oriented Software, Addison‐
Wesley Longman, 1994
1
21.10.2014
Design Patterns ‐ General
• In software engineering, a design pattern is a general
reusable solution to a commonly occurring problem in
software design.
• A design pattern is not a finished design that can be
transformed directly into code.
• It is a description or template for how to solve a
problem that can be used in many different situations.
• Object‐oriented design patterns typically show
relationships and interactions between classes or
objects, without specifying the final application classes
or objects that are involved.
• Independent of Programming Language
Design Patterns ‐ General
• Design patterns can speed up the
development process by providing tested,
proven development paradigms.
• Split into the following sections:
– Creational Patterns
– Behavioral Patterns
– Structural Patterns
– Concurrency Patterns
2
21.10.2014
Design Patterns Overview
• Creational Patterns • Behavioral Patterns
– Factory – Command
– Abstract Factory – Observer
– Singleton – Strategy
• Structural Patterns – Template Method
– Adapter
– Decorator
– Facade
3
21.10.2014
but also...
4
21.10.2014
5
21.10.2014
6
21.10.2014
Implementation vs. Interface
• Programming to an implementation:
Dog *h = new Dog();
h‐>bark();
• Programming to an interface/supertype:
Animal *animal = new Dog();
tier‐>makeSound();
• Assign the concrete implementation
object at runtime:
Animal = getAnimal();
animal‐>makeSound();
7
21.10.2014
class Duck {
protected: FlyBehavior* flyBehavior;
protected: QuackBehavior* quackBehavior;
8
21.10.2014
class FlyBehavior {
public: virtual void fly () = 0;
};
RedHead redHead;
redHead.display();
redHead.performFly();
redHead.performQuack();
redHead.swim();
RubberDuck rubberDuckie;
rubberDuckie.display();
rubberDuckie.performFly();
rubberDuckie.performQuack();
rubberDuckie.swim();
return 0;
}
9
21.10.2014
10
21.10.2014
11