Prototype pattern implementation
Intent
- Creates a new instance which is the same as the prototype instance by using polymorphism.
Explain
In the example code for the previous post, LSP, is it possible to copy a specific instance? If second instance in the vector should be copied, how can we copy it? From the vector, we only know the parent type of the target instance, not itself.
If each class keeps its class type in a member variable, this problem can be solved easily. However, it may requires many changes, such as condition for each class type. This violates OCP.
For this problem, prototype pattern is a good solution. It is called Virtual Copy Constructor, alternatively. As prototype pattern, each class has clone() to copy prototype instance to a new instance. Code 1 shows how to implement prototype pattern.
// 01_C_Prototype.cpp
#include <iostream>
#include <vector>
using namespace std;
class BaseClass
{
public:
virtual void foo()
{
cout << "Foo for Base class" << endl;
}
virtual BaseClass* clone()
{
return new BaseClass(*this);
}
};
class DerivedClassFirst : public BaseClass
{
public:
virtual void foo() override
{
cout << "Foo for First derived class" << endl;
}
DerivedClassFirst* clone() override
{
return new DerivedClassFirst(*this);
}
};
class DerivedClassSecond : public BaseClass
{
public:
virtual void foo() override
{
cout << "Foo for Second derived class" << endl;
}
DerivedClassSecond* clone() override
{
return new DerivedClassSecond(*this);
}
};
int main()
{
vector v;
v.push_back(new DerivedClassFirst);
v.push_back(new DerivedClassSecond);
int size = v.size();
for(int i = 0; i < size; i++)
{
v.push_back(v[i]->clone());
}
for(BaseClass* p : v)
{
p->foo();
}
return 0;
}
// Compile: clang++ -std=c++14
// -o 01_C_Prototype 01_C_Prototype.cpp
Foo for First derived class
Foo for Second derived class
Foo for First derived class
Foo for Second derived class
In main(), the vector can store pointers for BassClass or child of BaseClass. After two push_back(), the vector holds the pointers for two instances, DerivedClassFirst and DerivedClassSecond. Then, at the first for loop, the instances are copied by clone().
One of refactoring rules is "Replace type code with polymorphism". Prototype pattern is a good solution for the refactoring like the code 1.
Summary
- Replace type code with polymorphism
- Prototype pattern is to copy the prototype instance with using polymorphism.
COMMENTS