0% found this document useful (0 votes)
78 views

Design Patterns: Strategy Pattern

strategu pattern in c++

Uploaded by

ilijaklj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views

Design Patterns: Strategy Pattern

strategu pattern in c++

Uploaded by

ilijaklj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

21.10.

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

SimUDuck app 1.0

3
21.10.2014

SimUDuck app 2.0

but also...

4
21.10.2014

How about an Interface?

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;

protected: Duck() : flyBehavior(0), quackBehavior(0) {


}
public: virtual void setFlyBehavior(FlyBehavior* fb) {
flyBehavior = fb;
}
public: virtual void setQuackBehavior(QuackBehavior* qb) {
quackBehavior = qb;
}
public: virtual void performFly() {
flyBehavior->fly();
}
public: virtual void performQuack() {
quackBehavior->quack();
}
public: virtual void swim() {
cout << "All ducks float, even decoys!" << endl;
}
public: virtual void display() = 0;
};

class MallardDuck : public Duck {


public: MallardDuck() {
quackBehavior = new Quack();
flyBehavior = new FlyWithWings();
}
public: virtual void display() {
cout << "I'm a real Mallard duck" << endl;
}
};

class DecoyDuck : public Duck {


public: DecoyDuck() {
quackBehavior = new MuteQuack();
flyBehavior = new FlyNoWay();
}
public: virtual void display() {
cout << "I'm a duck Decoy" << endl;
}
};

8
21.10.2014

class FlyBehavior {
public: virtual void fly () = 0;
};

class FlyWithWings : public FlyBehavior {


public: virtual void fly () {
cout << "I'm flying!!" << endl;
}
};

class FlyNoWay : public FlyBehavior {


public: virtual void fly () {
cout << "I can't fly" << endl;
}
};

int main(int argc, char* argv[])


{
MallardDuck mallard;
mallard.display();
mallard.performFly();
mallard.performQuack();
mallard.swim();

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

You might also like