#include #include #include #include using namespace std; class Node { //abstract base class public: virtual string get_name() = 0; //signal output }; class A_node: public Node { public: string get_name(){ return "A"; }; string get_occupation(){ return "an A"; }; }; class B_node: public Node { public: string get_name(){ return "B"; }; string get_occupation(){ return "a B"; }; }; class C_node: public Node { public: string get_name(){ return "C"; }; string get_occupation(){ return "a C"; }; }; class Node_factory { public: template T* clone(T* proto) { return new T(); }; Node_factory() { add_type("A",new A_node()); add_type("B",new B_node()); add_type("C",new C_node()); } Node *get_node(string type) { if (type=="A") return new A_node(); if (type=="B") return new B_node(); if (type=="C") return new C_node(); }; map type_map; void add_type(string type,Node *proto){ type_map[type]=proto; }; Node *create(string type) { if (type_map.find(type)!=type_map.end()) { return (Node*)clone(type_map[type]); } } }; int main() { Node_factory f=Node_factory(); vector nodes; nodes.push_back(f.get_node("A")); nodes.push_back(f.get_node("B")); nodes.push_back(f.get_node("C")); for (auto &i: nodes) { cout << i->get_name() << endl; } }