The Factory Method Pattern: The Full Code
The complete Factory Method Pattern project, browsable file by file, organized the same way it would actually sit in a real codebase.
September 12, 20267 min read13 / 19
The last two posts built the factory method pattern piece by piece. This post is the reference: the whole project, as real files in real folders.
main.cpp
#include <iostream>
#include "zoo/AnimalFactory.h"
#include "zoo/RandomAnimalFactory.h"
#include "zoo/BalancedAnimalFactory.h"
void populateZoo(AnimalFactory& factory, int count) {
for (int i = 0; i < count; ++i) {
auto animal = factory.createAnimal();
std::cout << animal->name() << "\n";
}
}
int main() {
std::cout << "Random:\n";
RandomAnimalFactory randomFactory;
populateZoo(randomFactory, 6);
std::cout << "Balanced:\n";
BalancedAnimalFactory balancedFactory;
populateZoo(balancedFactory, 6);
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