mirror of
https://github.com/JakubVojvoda/design-patterns-cpp.git
synced 2025-12-17 12:54:36 +03:00
add Prototype pattern
This commit is contained in:
84
prototype/Prototype.cpp
Normal file
84
prototype/Prototype.cpp
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
/*
|
||||||
|
* C++ Design Patterns: Prototype
|
||||||
|
* Author: Jakub Vojvoda [github.com/JakubVojvoda]
|
||||||
|
* 2016
|
||||||
|
*
|
||||||
|
* Source code is licensed under MIT licence
|
||||||
|
* (for more details see LICENCE)
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Prototype
|
||||||
|
* declares an interface for cloning itself
|
||||||
|
*/
|
||||||
|
class Prototype {
|
||||||
|
public:
|
||||||
|
virtual Prototype *clone() = 0;
|
||||||
|
virtual std::string type() = 0;
|
||||||
|
// ...
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Concrete Prototype A and B
|
||||||
|
* implement an operation for cloning itself
|
||||||
|
*/
|
||||||
|
class ConcretePrototypeA : public Prototype {
|
||||||
|
public:
|
||||||
|
Prototype *clone() {
|
||||||
|
return new ConcretePrototypeA;
|
||||||
|
}
|
||||||
|
std::string type() {
|
||||||
|
return "type A";
|
||||||
|
}
|
||||||
|
// ...
|
||||||
|
};
|
||||||
|
|
||||||
|
class ConcretePrototypeB : public Prototype {
|
||||||
|
public:
|
||||||
|
Prototype *clone() {
|
||||||
|
return new ConcretePrototypeB;
|
||||||
|
}
|
||||||
|
std::string type() {
|
||||||
|
return "type B";
|
||||||
|
}
|
||||||
|
// ...
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Client
|
||||||
|
* creates a new object by asking a prototype to clone itself
|
||||||
|
*/
|
||||||
|
class Client {
|
||||||
|
public:
|
||||||
|
static Prototype* make(int index) {
|
||||||
|
return types[index]->clone();
|
||||||
|
}
|
||||||
|
// ...
|
||||||
|
|
||||||
|
private:
|
||||||
|
static Prototype* types[2];
|
||||||
|
};
|
||||||
|
|
||||||
|
Prototype* Client::types[] =
|
||||||
|
{
|
||||||
|
new ConcretePrototypeA,
|
||||||
|
new ConcretePrototypeB
|
||||||
|
// ...
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
Prototype* prototype;
|
||||||
|
|
||||||
|
prototype = Client::make(0);
|
||||||
|
std::cout << "Prototype: " << prototype->type() << std::endl;
|
||||||
|
|
||||||
|
prototype = Client::make(1);
|
||||||
|
std::cout << "Prototype: " << prototype->type() << std::endl;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
12
prototype/README.md
Normal file
12
prototype/README.md
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
## Prototype
|
||||||
|
|
||||||
|
Specify the kinds of objects to create using a prototypical instance, and create
|
||||||
|
new objects by copying this prototype. Pattern has creational purpose and deals
|
||||||
|
with object relationships, which are more dynamic. The pattern hides the complexities
|
||||||
|
of making new instances from the client.
|
||||||
|
|
||||||
|
### When to use
|
||||||
|
|
||||||
|
* when the classes to instantiate are specified at run-time
|
||||||
|
* to avoid building a class hierarchy of factories that parallels the class hierarchy of products
|
||||||
|
* when instances of a class can have one of only a few different combinations of state
|
||||||
Reference in New Issue
Block a user