{"id":4115,"date":"2019-12-17T12:15:32","date_gmt":"2019-12-17T12:15:32","guid":{"rendered":"http:\/\/www.max-sperling.bplaced.net\/?p=4115"},"modified":"2024-02-16T10:03:08","modified_gmt":"2024-02-16T10:03:08","slug":"factory","status":"publish","type":"post","link":"http:\/\/www.max-sperling.bplaced.net\/?p=4115","title":{"rendered":"Factory (Design Pattern)"},"content":{"rendered":"<p><strong>Concrete Factory<\/strong><br \/>\n&#8230; is a class that creates objects without exposing the instantiation logic.<\/p>\n<pre class=\"brush: cpp; gutter: false; title: ; notranslate\" title=\"\">\r\n#include &lt;memory&gt;\r\n \r\nclass Car {};\r\nclass VWCar : public Car {};\r\nclass BMWCar : public Car {};\r\n  \r\nclass CarFactory {\r\npublic:\r\n    enum CarType { VW, BMW };\r\n    std::unique_ptr&lt;Car&gt; createCar(CarType type) {\r\n        switch(type) {\r\n            case VW: return std::make_unique&lt;VWCar&gt;(); break;\r\n            case BMW: return std::make_unique&lt;BMWCar&gt;(); break;\r\n        }\r\n    }\r\n};\r\n \r\nint main() {\r\n    CarFactory carFactory;\r\n\r\n    std::unique_ptr&lt;Car&gt; vwCar = carFactory.createCar(CarFactory::VW);\r\n    std::unique_ptr&lt;Car&gt; bmwCar = carFactory.createCar(CarFactory::BMW);\r\n\r\n    \/\/ ...\r\n\r\n    return 0;\r\n}\r\n<\/pre>\n<hr>\n<p><strong>Abstract Factory<\/strong><br \/>\n&#8230; is a class which delegates the object creation to its subclasses (concrete factories).<\/p>\n<pre class=\"brush: cpp; gutter: false; title: ; notranslate\" title=\"\">\r\n#include &lt;memory&gt;\r\n \r\nclass Car {};\r\nclass VWCar : public Car {};\r\nclass BMWCar : public Car {};\r\n \r\nclass CarFactory {\r\npublic:\r\n    virtual std::unique_ptr&lt;Car&gt; createCar() = 0;\r\n};\r\n  \r\nclass VWFactory : public CarFactory {\r\npublic:\r\n  std::unique_ptr&lt;Car&gt; createCar() { return std::make_unique&lt;VWCar&gt;(); }\r\n};\r\n  \r\nclass BMWFactory : public CarFactory {\r\npublic:\r\n  std::unique_ptr&lt;Car&gt; createCar() { return std::make_unique&lt;BMWCar&gt;(); }\r\n};\r\n  \r\nint main() {\r\n    VWFactory vwFactory;\r\n    BMWFactory bmwFactory;\r\n\r\n    std::unique_ptr&lt;Car&gt; vwCar  = vwFactory.createCar();\r\n    std::unique_ptr&lt;Car&gt; bmwCar = bmwFactory.createCar();\r\n\r\n    \/\/ ...\r\n\r\n    return 0;\r\n}\r\n<\/pre>\n<hr>\n<p><strong>Factory Method<\/strong><br \/>\n&#8230; is a method within a class for an object creation and it gets implemented (if pure virtual) or possibly overritten (if virtual) in the subclasses.<\/p>\n<pre class=\"brush: cpp; gutter: false; title: ; notranslate\" title=\"\">\r\n#include &lt;memory&gt;\r\n\r\nclass Car {};\r\nclass VWCar : public Car {};\r\nclass BMWCar : public Car {};\r\n\r\nclass Engineer {\r\npublic:\r\n    void buildCar() {\r\n        std::unique_ptr&lt;Car&gt; car = createCar();\r\n    }\r\nprotected:\r\n    virtual std::unique_ptr&lt;Car&gt; createCar() = 0;\r\n};\r\n \r\nclass VWEngineer: public Engineer {\r\nprotected:\r\n    std::unique_ptr&lt;Car&gt; createCar() override { return std::make_unique&lt;VWCar&gt;(); }\r\n};\r\n \r\nclass BMWEngineer: public Engineer {\r\nprotected:\r\n    std::unique_ptr&lt;Car&gt; createCar() override { return std::make_unique&lt;BMWCar&gt;(); }\r\n};\r\n \r\nint main() {\r\n    VWEngineer vwEngineer;\r\n    BMWEngineer bmwEngineer;\r\n\r\n    vwEngineer.buildCar();\r\n    bmwEngineer.buildCar();\r\n\r\n    \/\/ ...\r\n\r\n    return 0;\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Concrete Factory &#8230; is a class that creates objects without exposing the instantiation logic. Abstract Factory &#8230; is a class<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false},"categories":[30],"tags":[],"_links":{"self":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/4115"}],"collection":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4115"}],"version-history":[{"count":1,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/4115\/revisions"}],"predecessor-version":[{"id":16694,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/4115\/revisions\/16694"}],"wp:attachment":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4115"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4115"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}