Using the factory method
A factory is essentially a warehouse for creating objects of other types. In a factory method design pattern, the creation of a new type of object, such as an enemy or a building, happens from an interface and the subclass decides which class it needs to instantiate. This is also a commonly used pattern in games and can be quite useful.
Getting ready
You need to have a working copy of Visual Studio installed on your Windows machine.
How to do it…
In this recipe, we will find out how easy it is to write a factory method design pattern:
Open Visual Studio.
Create a new C++ project.
Select a Win32 console application.
Add a source file called
Source.cpp
.Add the following lines of code to it:
#include <iostream> #include <conio.h> #include <vector> using namespace std; class IBuilding { public: virtual void TotalHealth() = 0; }; class Barracks : public IBuilding { public: void TotalHealth() { cout << "Health of Barrack is :" << 100;...