The Strategy Pattern: The Full Code
The complete Strategy Pattern project, browsable file by file, organized the same way it would actually sit in a real codebase.
September 8, 20266 min read3 / 19
The last two posts built the strategy pattern piece by piece. This post is the reference: the whole project, as real files in real folders.
main.cpp
#include <memory>
#include "duck/Duck.h"
#include "duck/SimpleFlyBehavior.h"
#include "duck/JetFlyBehavior.h"
#include "duck/NoFlyBehavior.h"
#include "duck/SimpleQuackBehavior.h"
#include "duck/NoQuackBehavior.h"
int main() {
Duck wildDuck(
std::make_unique<SimpleFlyBehavior>(),
std::make_unique<SimpleQuackBehavior>()
);
Duck rubberDuck(
std::make_unique<NoFlyBehavior>(),
std::make_unique<NoQuackBehavior>()
);
Duck mountainDuck(
std::make_unique<JetFlyBehavior>(),
std::make_unique<SimpleQuackBehavior>()
);
wildDuck.performQuack();
wildDuck.performFly();
rubberDuck.performQuack();
rubberDuck.performFly();
mountainDuck.performQuack();
mountainDuck.performFly();
return 0;
}Run this yourself: This exact file layout lives in the code-practice repo. Clone it,
cd into whichever language folder, and build it the same way as the other labs in this series.Keep reading