Design patterns should not be applied indiscriminately. Often they achieve flexibility and variability by introducing additional levels of indirection, and that can complicate a design and/or cost you some performance. A design pattern should only be applied when the flexibility it affords is actually needed - Erich Gamma
Introduction
One of the biggest challenges in software development is how to write code that enables easy scalability, maintenance, and flexibility.
One solution for this problem is to use Design Pattern, a guideline of good practices to write clean and organized code, allowing anyone to easily understand and contribute.
Design Patterns can be divided into 4 categories:
Creational: Object instantiation
Structural: Class relationships and hierarchies
Behavioral: Object intercommunication
The creational categories include five methods:
Factory method
Abstract method
Builder method
Prototype method
Singleton method
Today we will focus on the Factory method!
What is Factory Method?
The factory method encapsulates the creation of related objects, providing an interface to create an object but defers the creation to the sub-class. This is one of the most methods used, the reason for that are the benefits:
Creates objects based on runtime parameters
Do not need to know which objects you will need
Lets the subclasses instantiate the object instead of the min
Easily add new products or change existing ones
No need to make changes throughout
Extensible to include objects
Object generation located in one place
Easily switch between factories
Structure
In the factory method, we have three structures:
Class creator: Provide some default implementation of the factory method.
Concrete creator: Override the factory method in order to change the resulting product's type.
Concrete product: Provides various implementation of the product interface.
Pratice
Here we will learn how to use a Factory Method, through a very simple example of a game, where we must spawn different types of rock, in the first moment, we will have two types of rock: Small and Mid. The way the code is written will be very easy to register a new type of rock(E.g: Big, Iron).
#include <iostream>
using namespace std;
//Class creator
class rock {
public:
rock(){}
char *getType(){
return _type;
}
};
//Concrete product
class smallRock:public rock{
public:
smallRock(){
//_type = "Small";
cout << "\nSmall rock created" << endl;
}
};
//Concrete product
class midRock:public rock{
public:
midRock(){
//_type = "mid";
cout<<"\nMid rock created" << endl;
}
};
/*Creator concrete*/
class rockFactoryCreator{
public:
rock *GetRock(){
int choice;
cout << "Select type of rock to make: " << endl;
cout << "1: Small" << endl;
cout << "2: Mid" << endl;
cout << "Selection: ";
cin >> choice;
switch (choice)
{
case 1:
return new smallRock;
case 2:
return new midRock;
default:
cout << "Invalid Selection" << endl;
return NULL;
}
}
};
int main()
{
rockFactoryCreator rockCreator;
rock *asteroid;
asteroid = rockCreator.GetRock();
system("PAUSE");
return 0;
}
Top comments (0)