|
| 1 | +#include <iostream> |
| 2 | +#include <vector> |
| 3 | +#include <memory> |
| 4 | +#include <unordered_map> |
| 5 | + |
| 6 | +typedef enum { |
| 7 | + XML = 1, |
| 8 | + PLAIN = 2, |
| 9 | + SPREAD = 3 |
| 10 | +} DocType; |
| 11 | + |
| 12 | +/* |
| 13 | + * @brief Prototype declares an interface to clone itself. |
| 14 | + */ |
| 15 | +class DocPrototype |
| 16 | +{ |
| 17 | +public: |
| 18 | + virtual std::unique_ptr<DocPrototype> clone(void) const = 0; |
| 19 | + virtual void print(void) const = 0; |
| 20 | + virtual ~DocPrototype() { } |
| 21 | +}; |
| 22 | + |
| 23 | +/* |
| 24 | + * @brief : ConcretePrototypes : xmlDoc, plainDoc, spreadsheetDoc |
| 25 | + * Implement the interface of the Prototype to be able |
| 26 | + * to clone themselves. |
| 27 | + */ |
| 28 | +class xmlDoc : public DocPrototype |
| 29 | +{ |
| 30 | +public: |
| 31 | + xmlDoc(void) {} |
| 32 | + ~xmlDoc() {} |
| 33 | + |
| 34 | + std::unique_ptr<DocPrototype> clone(void) const override { |
| 35 | + return std::make_unique<xmlDoc>(*this); |
| 36 | + } |
| 37 | + void print(void) const override { std::cout << "xmlDoc\n"; } |
| 38 | +}; |
| 39 | + |
| 40 | +class plainDoc : public DocPrototype |
| 41 | +{ |
| 42 | +public: |
| 43 | + plainDoc(void) {} |
| 44 | + ~plainDoc() {} |
| 45 | + |
| 46 | + std::unique_ptr<DocPrototype> clone(void) const override { |
| 47 | + return std::make_unique<plainDoc>(*this); |
| 48 | + } |
| 49 | + void print(void) const override { std::cout << "plainDoc\n"; } |
| 50 | +}; |
| 51 | + |
| 52 | +class spreadsheetDoc : public DocPrototype |
| 53 | +{ |
| 54 | +public: |
| 55 | + spreadsheetDoc(void) {} |
| 56 | + ~spreadsheetDoc() {} |
| 57 | + |
| 58 | + std::unique_ptr<DocPrototype> clone(void) const override { |
| 59 | + return std::make_unique<spreadsheetDoc>(*this); |
| 60 | + } |
| 61 | + void print(void) const override { std::cout << "spreadsheetDoc\n"; } |
| 62 | +}; |
| 63 | + |
| 64 | +/*! |
| 65 | + * @brief : FactoryManager contains one instance of each |
| 66 | + * ConcretePrototype so that it can use their |
| 67 | + * clone() method to provide the client with the |
| 68 | + * required ConcretePrototype. |
| 69 | + */ |
| 70 | +class DocFactoryManager { |
| 71 | +public: |
| 72 | + DocFactoryManager(void) { |
| 73 | + m_docs[XML ] = std::make_unique<xmlDoc >(); |
| 74 | + m_docs[PLAIN ] = std::make_unique<plainDoc >(); |
| 75 | + m_docs[SPREAD] = std::make_unique<spreadsheetDoc>(); |
| 76 | + } |
| 77 | + ~DocFactoryManager(){} |
| 78 | + |
| 79 | + std::unique_ptr<DocPrototype> makeDocument( DocType type ) { |
| 80 | + return m_docs[type]->clone(); |
| 81 | + } |
| 82 | + |
| 83 | +private: |
| 84 | + std::unordered_map<DocType, |
| 85 | + std::unique_ptr<DocPrototype> > m_docs; |
| 86 | +}; |
| 87 | + |
| 88 | +/*! |
| 89 | + * @brief : Client code |
| 90 | + * Only uses the FactoryManager to create the document |
| 91 | + * type he needs. |
| 92 | + */ |
| 93 | +void clientCode(DocFactoryManager& mngr) { |
| 94 | + std::vector<std::unique_ptr<DocPrototype> > clientDocs; |
| 95 | + int clientChoice; |
| 96 | + |
| 97 | + std::cout << "quit(0), xml(1), plain(2), spreadsheet(3): \n"; |
| 98 | + while (true) |
| 99 | + { |
| 100 | + std::cout << "Type in your choice (0-3)\n"; |
| 101 | + std::cin >> clientChoice; |
| 102 | + if ( clientChoice <= 0 || clientChoice > SPREAD ) break; |
| 103 | + clientDocs.push_back( mngr.makeDocument( static_cast<DocType>(clientChoice) ) ); |
| 104 | + } |
| 105 | + |
| 106 | + for ( auto& it : clientDocs ) it->print(); |
| 107 | +} |
| 108 | + |
| 109 | +int main() { |
| 110 | + DocFactoryManager mngr; |
| 111 | + clientCode(mngr); |
| 112 | + |
| 113 | + return 0; |
| 114 | +} |
0 commit comments