{"id":6439,"date":"2022-09-04T12:25:53","date_gmt":"2022-09-04T12:25:53","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/factory-method\/"},"modified":"2023-06-26T09:01:05","modified_gmt":"2023-06-26T09:01:05","slug":"factory-method","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/factory-method\/","title":{"rendered":"The Factory Method"},"content":{"rendered":"<p>The classic book &#8220;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Design_Patterns\">Design Patterns: Elements of Reusable Object-Oriented Software&#8221;<\/a> has 23 patterns. They are ordered by intent: creational, structural, and behavioral patterns. Today,&nbsp; I focus on the creational pattern Factory Method.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6435\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/09\/CreationalPatterns.png\" alt=\"CreationalPatterns\" width=\"650\" height=\"330\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/09\/CreationalPatterns.png 1220w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/09\/CreationalPatterns-300x152.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/09\/CreationalPatterns-1024x520.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/09\/CreationalPatterns-768x390.png 768w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/p>\n<p>Five patterns in the book &#8220;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Design_Patterns\">Design Patterns: Elements of Reusable Object-Oriented Software&#8221; <\/a>(short Design Patterns) are creational, seven structural, and the remaining one behavioral. First of all, what does this mean?<\/p>\n<ul>\n<li><strong>Creational patterns<\/strong> deal with object creation in a well-defined way.<\/li>\n<li><strong>Structural patterns<\/strong> provide mechanisms to organize class and objects for larger structures.<\/li>\n<li><strong>Behavioral patterns<\/strong> deal with communication patterns between objects.<\/li>\n<\/ul>\n<p>Before I start with the creational patterns,&nbsp; I want to make a short disclaimer.<\/p>\n<h2>Short Disclaimer<\/h2>\n<p>I present about half of the 23 design patterns. For the remaining ones, I only provide a fact sheet. The selection of the presented design pattern is based on two points.<\/p>\n<ol>\n<li>Which patterns did I encounter most often as a software developer in the last twenty years?<\/li>\n<li>Which patterns are still in use?<\/li>\n<\/ol>\n<p>My explanation of the presented design patterns is intentionally concise. My idea is to present the key principle of a pattern and present them from a C++ point of view. If you want to have more details, there is excellent documentation available. Here are a few choices:<\/p>\n<ul>\n<li>The classic: &#8220;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Design_Patterns\">Design Patterns: Elements of Reusable Object-Oriented Software<\/a>&#8220;<\/li>\n<li>Nice introduction: <a href=\"https:\/\/www.oreilly.com\/library\/view\/head-first-design\/9781492077992\/\">&#8220;Head First Design Patterns&#8221;<\/a><\/li>\n<li>Wikipedia articles: <a href=\"https:\/\/en.wikipedia.org\/wiki\/Design_Patterns\">Design Patterns<\/a><\/li>\n<\/ul>\n<p>Creational patterns deal with object creation. Let&#8217;s dive deeper.<\/p>\n<\/p>\n<h2>Creational Patterns<\/h2>\n<p>I will write about two of the five creational patterns: <a href=\"https:\/\/en.wikipedia.org\/wiki\/Factory_method_pattern\">Factory Method<\/a> and <a href=\"https:\/\/en.wikipedia.org\/wiki\/Singleton_pattern\">Singleton<\/a>. I know, I know the Singleton could also be regarded as an Anti-Pattern. In a later post, I will discuss the Singleton in more depth. Let&#8217;s start with the Factory Method:<\/p>\n<h3>Factory Method<\/h3>\n<p>Here are the facts:<\/p>\n<h4>Purpose<\/h4>\n<p>The factory method defines an interface to create a single object but lets subclasses decide which objects to create. The interface can provide a default implementation for creating objects.<\/p>\n<h4>Also known as<\/h4>\n<p>Virtual constructor<\/p>\n<h4>Use Case<\/h4>\n<ul>\n<li>A class does not know what kind of objects to create<\/li>\n<li>Subclasses decide what object to create<\/li>\n<li>Classes delegate the creation of objects to subclasses<\/li>\n<\/ul>\n<h4>Example<\/h4>\n<p>Each container of the Standard Template Library has eight factory functions to generate various iterators.<\/p>\n<div>\n<ul>\n<li><strong><code>begin<\/code>, <code>cbegin<\/code><\/strong>: returns an iterator to the beginning of the container<\/li>\n<li><strong><code>end, cend<\/code><\/strong>: returns an iterator to the end of the container<\/li>\n<li><strong><code>rbegin, crbegin<\/code><\/strong>: returns a reverse iterator to the beginning of the container<\/li>\n<li><strong><code>rend, crend<\/code><\/strong>: returns a reverse iterator to the end of the container<\/li>\n<\/ul>\n<div>The factory functions starting with <code>c,<\/code> return constant iterators.<\/div>\n<\/div>\n<h4>Structure<\/h4>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6436\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/09\/FactoryMethod.png\" alt=\"FactoryMethod\" width=\"400\" height=\"259\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/09\/FactoryMethod.png 509w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/09\/FactoryMethod-300x194.png 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Product<\/strong><\/p>\n<ul>\n<li>Objects created by <code>factoryMethod.<\/code><\/li>\n<\/ul>\n<p><strong>Concrete Product<\/strong><\/p>\n<ul>\n<li>Implements the interface<\/li>\n<\/ul>\n<p><strong>Creator<\/strong><\/p>\n<ul>\n<li>Declares the factory method<\/li>\n<li>Calls the factory method<\/li>\n<\/ul>\n<p><strong>Concrete Creator<\/strong><\/p>\n<ul>\n<li>Overwrites the factory method<\/li>\n<\/ul>\n<p>The <strong>Creator<\/strong> does not instantiate the <strong>Concrete Product<\/strong>. It calls its virtual member function <code>factoryMethod<\/code>. Consequentially, the <strong>Concrete Product<\/strong> is created by the <strong>Concrete Creator<\/strong>, and the object creation is independent of the <strong>Creator<\/strong>.<\/p>\n<p>This pattern is also known as a virtual constructor.<\/p>\n<h4>Related Patterns<\/h4>\n<ul>\n<li><a href=\"https:\/\/en.wikipedia.org\/wiki\/Abstract_factory_pattern\">Abstract Factory<\/a> is typically implemented using Factory Methods.<\/li>\n<li>Factory Methods are often steps of the<a href=\"https:\/\/en.wikipedia.org\/wiki\/Template_method_pattern\"> Template Method<\/a>.<\/li>\n<\/ul>\n<h3>Virtual Constructor<\/h3>\n<p>Honestly, the name virtual constructor is misleading. We don&#8217;t have a virtual constructor in C++, but we have virtual construction to simulate a virtual constructor.<\/p>\n<p>Assume you have a class hierarchy with an interface class <code>Window<\/code> and two implementation classes, <code>DefaultWindow<\/code> and <code>FancyWindow.<\/code><\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ Product<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Window <\/span>{ \r\n <span style=\"color: #006699; font-weight: bold;\">public<\/span><span style=\"color: #555555;\">:<\/span> \r\n    <span style=\"color: #006699; font-weight: bold;\">virtual<\/span> <span style=\"color: #555555;\">~<\/span>Window() {};\r\n};\r\n\r\n<span style=\"color: #0099ff; font-style: italic;\">\/\/ Concrete Products <\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">DefaultWindow<\/span><span style=\"color: #555555;\">:<\/span> <span style=\"color: #006699; font-weight: bold;\">public<\/span> Window {};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">FancyWindow<\/span><span style=\"color: #555555;\">:<\/span> <span style=\"color: #006699; font-weight: bold;\">public<\/span> Window {};\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Now, you want to create a new <code>Window<\/code> based on an existing one. This means that when you put an instance <code>of <\/code><code>DefaultWindow <\/code>or<code> FancyWindow<\/code> inside the factory function <code>getNewWindow<\/code>, it should r<code>eturn <\/code>an instance of the same class.<\/p>\n<p>Classically, the factory method is implemented using an <code>enum<\/code> and a factory function. Here is the first try:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ factoryMethodClassic.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">enum<\/span> <span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">WindowType<\/span> {                                          <span style=\"color: #0099ff; font-style: italic;\">\/\/ (5)<\/span>\r\n    DefaultWindow,\r\n    FancyWindow\r\n};\r\n\r\n<span style=\"color: #0099ff; font-style: italic;\">\/\/ Product<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Window<\/span> { \r\n <span style=\"color: #006699; font-weight: bold;\">public<\/span><span style=\"color: #555555;\">:<\/span> \r\n    <span style=\"color: #006699; font-weight: bold;\">virtual<\/span> <span style=\"color: #555555;\">~<\/span>Window() {};\r\n    <span style=\"color: #006699; font-weight: bold;\">virtual<\/span> WindowType getType() <span style=\"color: #006699; font-weight: bold;\">const<\/span> <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">0<\/span>;\r\n    <span style=\"color: #006699; font-weight: bold;\">virtual<\/span> std<span style=\"color: #555555;\">::<\/span>string getName() <span style=\"color: #006699; font-weight: bold;\">const<\/span> <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">0<\/span>;\r\n};\r\n\r\n<span style=\"color: #0099ff; font-style: italic;\">\/\/ Concrete Products <\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">DefaultWindow<\/span><span style=\"color: #555555;\">:<\/span> <span style=\"color: #006699; font-weight: bold;\">public<\/span> Window { \r\n <span style=\"color: #006699; font-weight: bold;\">public<\/span><span style=\"color: #555555;\">:<\/span>\r\n    WindowType getType() <span style=\"color: #006699; font-weight: bold;\">const<\/span> override {\r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> WindowType<span style=\"color: #555555;\">::<\/span>DefaultWindow;\r\n    }\r\n    std<span style=\"color: #555555;\">::<\/span>string getName() <span style=\"color: #006699; font-weight: bold;\">const<\/span> override { \r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #cc3300;\">\"DefaultWindow\"<\/span>;\r\n    }\r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">FancyWindow<\/span><span style=\"color: #555555;\">:<\/span> <span style=\"color: #006699; font-weight: bold;\">public<\/span> Window {\r\n <span style=\"color: #9999ff;\">public:<\/span> \r\n     WindowType getType() <span style=\"color: #006699; font-weight: bold;\">const<\/span> override {\r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> WindowType<span style=\"color: #555555;\">::<\/span>FancyWindow;\r\n    }\r\n    std<span style=\"color: #555555;\">::<\/span>string getName() <span style=\"color: #006699; font-weight: bold;\">const<\/span> override { \r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #cc3300;\">\"FancyWindow\"<\/span>;\r\n    }\r\n};\r\n\r\n<span style=\"color: #0099ff; font-style: italic;\">\/\/ Concrete Creator or Client<\/span>\r\nWindow<span style=\"color: #555555;\">*<\/span> <span style=\"color: #cc00ff;\">getNewWindow<\/span>(Window<span style=\"color: #555555;\">*<\/span> window) {                           <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n    <span style=\"color: #006699; font-weight: bold;\">switch<\/span>(window<span style=\"color: #555555;\">-&gt;<\/span>getType()){                                   <span style=\"color: #0099ff; font-style: italic;\">\/\/ (4)<\/span>\r\n    <span style=\"color: #006699; font-weight: bold;\">case<\/span> WindowType:<span style=\"color: #555555;\">:<\/span>DefaultWindow<span style=\"color: #555555;\">:<\/span>\r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #006699; font-weight: bold;\">new<\/span> DefaultWindow();\r\n        <span style=\"color: #006699; font-weight: bold;\">break<\/span>;\r\n    <span style=\"color: #006699; font-weight: bold;\">case<\/span> WindowType:<span style=\"color: #555555;\">:<\/span>FancyWindow<span style=\"color: #555555;\">:<\/span>\r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #006699; font-weight: bold;\">new<\/span> FancyWindow();\r\n        <span style=\"color: #006699; font-weight: bold;\">break<\/span>;\r\n    }\r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> nullptr;\r\n}\r\n  \r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n    DefaultWindow defaultWindow;\r\n    FancyWindow fancyWindow;\r\n\r\n    <span style=\"color: #006699; font-weight: bold;\">const<\/span> Window<span style=\"color: #555555;\">*<\/span> defaultWindow1 <span style=\"color: #555555;\">=<\/span> getNewWindow(<span style=\"color: #555555;\">&amp;<\/span>defaultWindow); <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n    <span style=\"color: #006699; font-weight: bold;\">const<\/span> Window<span style=\"color: #555555;\">*<\/span> fancyWindow1 <span style=\"color: #555555;\">=<\/span> getNewWindow(<span style=\"color: #555555;\">&amp;<\/span>fancyWindow);     <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> defaultWindow1<span style=\"color: #555555;\">-&gt;<\/span>getName() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> fancyWindow1<span style=\"color: #555555;\">-&gt;<\/span>getName() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n  \r\n    <span style=\"color: #006699; font-weight: bold;\">delete<\/span> defaultWindow1;\r\n    <span style=\"color: #006699; font-weight: bold;\">delete<\/span> fancyWindow1;\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n  \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The factory function in line (1) decides, based on the incoming <code>Window<\/code>, which <code>Window<\/code> (lines 2 and 3) should be created. It uses window-&gt;getType() (line 4) to get the right <code>Window<\/code> type. The <code>WindowType<\/code> is an enumeration.<\/p>\n<p>Finally, here is the output of the program:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6437\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/09\/factoryMethodClassic.png\" alt=\"factoryMethodClassic\" width=\"422\" height=\"159\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/09\/factoryMethodClassic.png 422w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/09\/factoryMethodClassic-300x113.png 300w\" sizes=\"auto, (max-width: 422px) 100vw, 422px\" \/><\/p>\n<p>Honestly, I don&#8217;t like this solution for the following reasons:<\/p>\n<ol>\n<li>When my application should support, I would have to extend the enumeration <code>WindowType<\/code> and the switch statement.<\/li>\n<li>The switch statement becomes more and more difficult to maintain if I add new <code>WindowType<\/code>&#8216;s.<\/li>\n<li>The code is too complicated. This is mainly due to the <code>switch<\/code> statement.<\/li>\n<\/ol>\n<p>Let me replace the switch statement with a virtual dispatch. Additionally, I also want to clone the existing <code>Window<\/code>&#8216;s.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0px; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ factoryMethod.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #0099ff; font-style: italic;\">\/\/ Product<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Window<\/span>{ \r\n <span style=\"color: #006699; font-weight: bold;\">public<\/span><span style=\"color: #555555;\">:<\/span> \r\n    <span style=\"color: #006699; font-weight: bold;\">virtual<\/span> Window<span style=\"color: #555555;\">*<\/span> create() <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">0<\/span>;                       <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n    <span style=\"color: #006699; font-weight: bold;\">virtual<\/span> Window<span style=\"color: #555555;\">*<\/span> clone() <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">0<\/span>;                        <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n    <span style=\"color: #006699; font-weight: bold;\">virtual<\/span> <span style=\"color: #555555;\">~<\/span>Window() {};\r\n};\r\n\r\n<span style=\"color: #0099ff; font-style: italic;\">\/\/ Concrete Products <\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">DefaultWindow<\/span><span style=\"color: #555555;\">:<\/span> <span style=\"color: #006699; font-weight: bold;\">public<\/span> Window { \r\n    DefaultWindow<span style=\"color: #555555;\">*<\/span> create() override { \r\n        std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Create DefaultWindow\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #006699; font-weight: bold;\">new<\/span> <span style=\"color: #cc00ff;\">DefaultWindow<\/span>();\r\n    } \r\n     DefaultWindow<span style=\"color: #555555;\">*<\/span> clone() override { \r\n        std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Clone DefaultWindow\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #006699; font-weight: bold;\">new<\/span> <span style=\"color: #cc00ff;\">DefaultWindow<\/span>(<span style=\"color: #555555;\">*<\/span><span style=\"color: #006699; font-weight: bold;\">this<\/span>);\r\n    } \r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">FancyWindow<\/span><span style=\"color: #555555;\">:<\/span> <span style=\"color: #006699; font-weight: bold;\">public<\/span> Window { \r\n    FancyWindow<span style=\"color: #555555;\">*<\/span> create() override { \r\n        std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Create FancyWindow\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #006699; font-weight: bold;\">new<\/span> <span style=\"color: #cc00ff;\">FancyWindow<\/span>();\r\n    } \r\n    FancyWindow<span style=\"color: #555555;\">*<\/span> clone() override { \r\n        std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Clone FancyWindow\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #006699; font-weight: bold;\">new<\/span> <span style=\"color: #cc00ff;\">FancyWindow<\/span>(<span style=\"color: #555555;\">*<\/span><span style=\"color: #006699; font-weight: bold;\">this<\/span>);                  <em><span style=\"color: #0099ff;\">\/\/ (5)<\/span><\/em>\r\n    } \r\n};\r\n\r\n<span style=\"color: #0099ff; font-style: italic;\">\/\/ Concrete Creator or Client                             <\/span>\r\nWindow<span style=\"color: #555555;\">*<\/span> <span style=\"color: #cc00ff;\">createWindow<\/span>(Window<span style=\"color: #555555;\">&amp;<\/span> oldWindow) {               <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> oldWindow.create();\r\n}\r\n\r\nWindow<span style=\"color: #555555;\">*<\/span> <span style=\"color: #cc00ff;\">cloneWindow<\/span>(Window<span style=\"color: #555555;\">&amp;<\/span> oldWindow) {                <span style=\"color: #0099ff; font-style: italic;\">\/\/ (4)    <\/span>\r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> oldWindow.clone();\r\n}\r\n  \r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n    DefaultWindow defaultWindow;\r\n    FancyWindow fancyWindow;\r\n  \r\n    <span style=\"color: #006699; font-weight: bold;\">const<\/span> Window<span style=\"color: #555555;\">*<\/span> defaultWindow1 <span style=\"color: #555555;\">=<\/span> createWindow(defaultWindow);\r\n    <span style=\"color: #006699; font-weight: bold;\">const<\/span> Window<span style=\"color: #555555;\">*<\/span> fancyWindow1 <span style=\"color: #555555;\">=<\/span> createWindow(fancyWindow);\r\n    \r\n    <span style=\"color: #006699; font-weight: bold;\">const<\/span> Window<span style=\"color: #555555;\">*<\/span> defaultWindow2 <span style=\"color: #555555;\">=<\/span> cloneWindow(defaultWindow);\r\n    <span style=\"color: #006699; font-weight: bold;\">const<\/span> Window<span style=\"color: #555555;\">*<\/span> fancyWindow2 <span style=\"color: #555555;\">=<\/span> cloneWindow(fancyWindow);\r\n  \r\n    <span style=\"color: #006699; font-weight: bold;\">delete<\/span> defaultWindow1;\r\n    <span style=\"color: #006699; font-weight: bold;\">delete<\/span> fancyWindow1;\r\n    <span style=\"color: #006699; font-weight: bold;\">delete<\/span> defaultWindow2;\r\n    <span style=\"color: #006699; font-weight: bold;\">delete<\/span> fancyWindow2;\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n  \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Window supports now two ways to create new ones: a default constructed <code>Window<\/code> with the member function <code>create<\/code> (line 1) and a copy constructed <code>Window<\/code> with the member function <code>clone<\/code> (line 2). The subtle difference is that the constructor takes the this pointer in the member function <code>clone<\/code>. (line The factory functions <code>createWindow<\/code> (line 3) and <code>cloneWindow<\/code> (line 4) dispatch on the dynamic type.<\/p>\n<p>The output of the program is promising. Both member functions <code>create<\/code> and <code>clone<\/code> display the name of the object they create.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6438\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/09\/factoryMethodCreateClone.png\" alt=\"factoryMethodPointer\" width=\"397\" height=\"201\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/09\/factoryMethodCreateClone.png 397w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/09\/factoryMethodCreateClone-300x152.png 300w\" sizes=\"auto, (max-width: 397px) 100vw, 397px\" \/>&nbsp;<\/p>\n<p>By the way. It is fine that the virtual member functions <code>create <\/code>and <code>clone<\/code> member functions of the <code>DefaultWindow<\/code> and the <code>FancyWindow<\/code> are <code>private<\/code>, because you use them via the <code>Window<\/code> interface. In the interface, both<code><\/code> member function are <code>public<\/code>.<\/p>\n<h2>What&#8217;s Next?<\/h2>\n<p>Am I&#8217;m done with the factory method? NO! The program<code> factoryMethod.cpp<\/code> has two serious issues. ownership semantic and slicing. Let me write more about it in my next post.<\/p>\n<p>&nbsp;<\/p>\n<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The classic book &#8220;Design Patterns: Elements of Reusable Object-Oriented Software&#8221; has 23 patterns. They are ordered by intent: creational, structural, and behavioral patterns. Today,&nbsp; I focus on the creational pattern Factory Method.<\/p>\n","protected":false},"author":21,"featured_media":6435,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[379],"tags":[409],"class_list":["post-6439","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-patterns","tag-virtual-constructor"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6439","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=6439"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6439\/revisions"}],"predecessor-version":[{"id":6661,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6439\/revisions\/6661"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/6435"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=6439"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=6439"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=6439"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}