{"id":1610,"date":"2023-04-16T16:38:44","date_gmt":"2023-04-16T09:38:44","guid":{"rendered":"https:\/\/csharptutorial.net\/?page_id=1610"},"modified":"2024-12-14T23:07:36","modified_gmt":"2024-12-14T16:07:36","slug":"csharp-abstract-factory","status":"publish","type":"page","link":"https:\/\/www.csharptutorial.net\/csharp-design-patterns\/csharp-abstract-factory\/","title":{"rendered":"C# Abstract Factory"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use the C# abstract factory pattern to create families of dependent objects without specifying their concrete classes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to the C# Abstract Factory design pattern<\/h2>\n\n\n\n<p>The Abstract Factory is a creational design pattern that provides an interface for creating families of related or dependent objects without specifying their concrete classes.<\/p>\n\n\n\n<p>The Abstract Factory pattern is proper when you create families of related objects that work seamlessly with each other as a group and have consistent behavior.<\/p>\n\n\n\n<p>You are building a UI toolkit for creating cross-platform mobile applications. And you want to create UI widgets like buttons, and text fields that work on both iOS and Android platforms.<\/p>\n\n\n\n<p>In this case, you can use the Abstract Factory pattern to create an abstract factory interface that defines methods for building each widget. You then implement this interface with concrete factories that create widgets on each platform.<\/p>\n\n\n\n<p>The advantage of usin g the Abstract Factory pattern is that it encapsulates the creation of objects and separates the object creation logic from the rest of your code, which allows you to easily change the implementation of the objects without affecting the code base.<\/p>\n\n\n\n<p>For example, you want to support additional mobile platforms besides iOS and Android.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Abstract Factory UML diagram<\/h3>\n\n\n\n<p>The following UML diagram illustrates the abstract factory design pattern:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/csharptutorial.net\/wp-content\/uploads\/2023\/04\/csharp-abstract-factory.svg\" alt=\"C# Abstract Factory Design Pattern\" class=\"wp-image-1611\"\/><\/figure>\n\n\n\n<p>The Abstract Factory pattern involves the following participants:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Abstract Factory: This is the <a href=\"https:\/\/csharptutorial.net\/csharp-tutorial\/csharp-interface\/\">interface<\/a> that declares a set of factory methods for creating abstract products. It is responsible for defining the operations that create the families of related objects.<\/li>\n\n\n\n<li>Concrete Factory: These are the classes that implement the abstract factory interface and create families of related objects. Each concrete factory is responsible for creating a different variant of the product family.<\/li>\n\n\n\n<li>Abstract Product: This is the interface that declares a set of operations that the concrete products must implement. It defines a type of product but does not provide the implementation details.<\/li>\n\n\n\n<li>Concrete Product: These are the classes that implement the abstract product interface and provide a specific implementation of the product. Each concrete factory creates a different variant of the product family.<\/li>\n\n\n\n<li>Client: This is the class that uses the abstract factory and abstract product interfaces to create families of related objects. The client code does not know which concrete products it is creating, only that they belong to the same family of products.<\/li>\n<\/ul>\n\n\n\n<p>The Client uses the abstract factory interface to create a family of related objects. <\/p>\n\n\n\n<p>The abstract factory creates the concrete factories that create the concrete products. Also, the Client uses the abstract product interface to create concrete products, without knowing the specific implementation details.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">C# Abstract Factory design pattern examples<\/h2>\n\n\n\n<p>The following C# program demonstrates how to use the Abstract Factory design pattern by creating different families of widgets for iOS and Android platforms:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">using<\/span> <span class=\"hljs-keyword\">static<\/span> System.Console;\n\n<span class=\"hljs-keyword\">namespace<\/span> <span class=\"hljs-title\">AbstractFactory<\/span>;\n\n<span class=\"hljs-comment\">\/\/ Widget abstract class<\/span>\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">abstract<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Widget<\/span>\n{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">abstract<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">Render<\/span>(<span class=\"hljs-params\"><\/span>)<\/span>;\n}\n\n<span class=\"hljs-comment\">\/\/ Button widget for iOS platform<\/span>\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">IOSButton<\/span> : <span class=\"hljs-title\">Widget<\/span>\n{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">override<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">Render<\/span>(<span class=\"hljs-params\"><\/span>)<\/span>\n    {\n        WriteLine(<span class=\"hljs-string\">\"Rendering iOS button...\"<\/span>);\n    }\n}\n\n<span class=\"hljs-comment\">\/\/ TextField widget for iOS platform<\/span>\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">IOSTextField<\/span> : <span class=\"hljs-title\">Widget<\/span>\n{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">override<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">Render<\/span>(<span class=\"hljs-params\"><\/span>)<\/span>\n    {\n        WriteLine(<span class=\"hljs-string\">\"Rendering iOS text field...\"<\/span>);\n    }\n}\n\n<span class=\"hljs-comment\">\/\/ Button widget for Android platform<\/span>\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">AndroidButton<\/span> : <span class=\"hljs-title\">Widget<\/span>\n{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">override<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">Render<\/span>(<span class=\"hljs-params\"><\/span>)<\/span>\n    {\n        WriteLine(<span class=\"hljs-string\">\"Rendering Android button...\"<\/span>);\n    }\n}\n\n<span class=\"hljs-comment\">\/\/ TextField widget for Android platform<\/span>\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">AndroidTextField<\/span> : <span class=\"hljs-title\">Widget<\/span>\n{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">override<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">Render<\/span>(<span class=\"hljs-params\"><\/span>)<\/span>\n    {\n        WriteLine(<span class=\"hljs-string\">\"Rendering Android text field...\"<\/span>);\n    }\n}\n\n<span class=\"hljs-comment\">\/\/ Widget factory interface<\/span>\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">interface<\/span> <span class=\"hljs-title\">IWidgetFactory<\/span>\n{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> Widget <span class=\"hljs-title\">CreateButton<\/span>(<span class=\"hljs-params\"><\/span>)<\/span>;\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> Widget <span class=\"hljs-title\">CreateTextField<\/span>(<span class=\"hljs-params\"><\/span>)<\/span>;\n}\n\n<span class=\"hljs-comment\">\/\/ iOS widget factory<\/span>\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">IOSWidgetFactory<\/span> : <span class=\"hljs-title\">IWidgetFactory<\/span>\n{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> Widget <span class=\"hljs-title\">CreateButton<\/span>(<span class=\"hljs-params\"><\/span>)<\/span>\n    {\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">new<\/span> IOSButton();\n    }\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> Widget <span class=\"hljs-title\">CreateTextField<\/span>(<span class=\"hljs-params\"><\/span>)<\/span>\n    {\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">new<\/span> IOSTextField();\n    }\n}\n\n<span class=\"hljs-comment\">\/\/ Android widget factory<\/span>\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">AndroidWidgetFactory<\/span> : <span class=\"hljs-title\">IWidgetFactory<\/span>\n{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> Widget <span class=\"hljs-title\">CreateButton<\/span>(<span class=\"hljs-params\"><\/span>)<\/span>\n    {\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">new<\/span> AndroidButton();\n    }\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> Widget <span class=\"hljs-title\">CreateTextField<\/span>(<span class=\"hljs-params\"><\/span>)<\/span>\n    {\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">new<\/span> AndroidTextField();\n    }\n}\n\n<span class=\"hljs-comment\">\/\/ App (Client) that to create and use widgets<\/span>\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">App<\/span>\n{\n    <span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-keyword\">readonly<\/span> IWidgetFactory factory;\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-title\">App<\/span>(<span class=\"hljs-params\">IWidgetFactory factory<\/span>)<\/span>\n    {\n        <span class=\"hljs-keyword\">this<\/span>.factory = factory;\n    }\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">RenderUI<\/span>(<span class=\"hljs-params\"><\/span>)<\/span>\n    {\n        Widget button = factory.CreateButton();\n        Widget textField = factory.CreateTextField();\n\n        button.Render();\n        textField.Render();\n    }\n}\n\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Program<\/span>\n{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">Main<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">string<\/span>&#91;] args<\/span>)<\/span>\n    {\n        WriteLine(<span class=\"hljs-string\">\"Enter the platform (ios or android): \"<\/span>);\n        <span class=\"hljs-keyword\">var<\/span> platform = ReadLine().ToLower();\n\n        IWidgetFactory factory;\n        <span class=\"hljs-keyword\">if<\/span> (platform == <span class=\"hljs-string\">\"ios\"<\/span>)\n        {\n            factory = <span class=\"hljs-keyword\">new<\/span> IOSWidgetFactory();\n        }\n        <span class=\"hljs-keyword\">else<\/span> <span class=\"hljs-keyword\">if<\/span> (platform == <span class=\"hljs-string\">\"android\"<\/span>)\n        {\n            factory = <span class=\"hljs-keyword\">new<\/span> AndroidWidgetFactory();\n        }\n        <span class=\"hljs-keyword\">else<\/span>\n        {\n            WriteLine(<span class=\"hljs-string\">\"Invalid platform entered.\"<\/span>);\n            <span class=\"hljs-keyword\">return<\/span>;\n        }\n\n        <span class=\"hljs-keyword\">var<\/span> app = <span class=\"hljs-keyword\">new<\/span> App(factory);\n        app.RenderUI();\n    }\n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, define an <a href=\"https:\/\/csharptutorial.net\/csharp-tutorial\/csharp-abstract-class\/\">abstract class<\/a> called <code>Widget<\/code> that represents a generic widget. The <code>Widget<\/code> class has an abstract method <code>Render()<\/code> that renders the widget on a specific platform:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">abstract<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Widget<\/span>\n{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">abstract<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">Render<\/span>(<span class=\"hljs-params\"><\/span>)<\/span>;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, define two concrete classes, <code>IOSButton<\/code> and <code>IOS<code>TextField<\/code><\/code> for creating <code>Button<\/code> and <code>TextField<\/code> on the iOS platform. Both classes <a href=\"https:\/\/csharptutorial.net\/csharp-tutorial\/csharp-inheritance\/\">inherit<\/a> from the <code>Widget<\/code> class:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-comment\">\/\/ Button widget for iOS platform<\/span>\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">IOSButton<\/span> : <span class=\"hljs-title\">Widget<\/span>\n{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">override<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">Render<\/span>(<span class=\"hljs-params\"><\/span>)<\/span>\n    {\n        WriteLine(<span class=\"hljs-string\">\"Rendering iOS button...\"<\/span>);\n    }\n}\n\n<span class=\"hljs-comment\">\/\/ TextField widget for iOS platform<\/span>\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">IOSTextField<\/span> : <span class=\"hljs-title\">Widget<\/span>\n{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">override<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">Render<\/span>(<span class=\"hljs-params\"><\/span>)<\/span>\n    {\n        WriteLine(<span class=\"hljs-string\">\"Rendering iOS text field...\"<\/span>);\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, define two similar concrete classes, <code>AndroidButton<\/code> and <code>Android<code>TextField<\/code><\/code>, for creating <code>Button<\/code> and <code>TextField<\/code> widgets on the Android platform. These classes also inherit from the <code>Widget<\/code> class.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-comment\">\/\/ Button widget for Android platform<\/span>\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">AndroidButton<\/span> : <span class=\"hljs-title\">Widget<\/span>\n{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">override<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">Render<\/span>(<span class=\"hljs-params\"><\/span>)<\/span>\n    {\n        WriteLine(<span class=\"hljs-string\">\"Rendering Android button...\"<\/span>);\n    }\n}\n\n<span class=\"hljs-comment\">\/\/ TextField widget for Android platform<\/span>\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">AndroidTextField<\/span> : <span class=\"hljs-title\">Widget<\/span>\n{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">override<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">Render<\/span>(<span class=\"hljs-params\"><\/span>)<\/span>\n    {\n        WriteLine(<span class=\"hljs-string\">\"Rendering Android text field...\"<\/span>);\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Fourth, define an interface <code>IWidgetFactory<\/code> for creating <code>Button<\/code> and <code>TextField<\/code> widgets:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-comment\">\/\/ Widget factory interface<\/span>\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">interface<\/span> <span class=\"hljs-title\">IWidgetFactory<\/span>\n{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> Widget <span class=\"hljs-title\">CreateButton<\/span>(<span class=\"hljs-params\"><\/span>)<\/span>;\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> Widget <span class=\"hljs-title\">CreateTextField<\/span>(<span class=\"hljs-params\"><\/span>)<\/span>;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Fifth, define a class called <code>IOSWidgetFactory<\/code> that implements the <code>IWidgetFactory<\/code>. The <code>IOSWIdgetFactory<\/code> is responsible for creating <code>Button<\/code> and <code>TextField<\/code> widgets on the iOS platform:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-comment\">\/\/ iOS widget factory<\/span>\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">IOSWidgetFactory<\/span> : <span class=\"hljs-title\">IWidgetFactory<\/span>\n{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> Widget <span class=\"hljs-title\">CreateButton<\/span>(<span class=\"hljs-params\"><\/span>)<\/span>\n    {\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">new<\/span> IOSButton();\n    }\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> Widget <span class=\"hljs-title\">CreateTextField<\/span>(<span class=\"hljs-params\"><\/span>)<\/span>\n    {\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">new<\/span> IOSTextField();\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Sixth, define the <code>AndroidWidgetFactory<\/code> class that implements the <code>IWidgetFactory<\/code> interface. The <code>AdroidWidgetFactory<\/code> is in charge of creating <code>Button<\/code> and <code>TextField<\/code> widgets on the Android platform:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-comment\">\/\/ Android widget factory<\/span>\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">AndroidWidgetFactory<\/span> : <span class=\"hljs-title\">IWidgetFactory<\/span>\n{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> Widget <span class=\"hljs-title\">CreateButton<\/span>(<span class=\"hljs-params\"><\/span>)<\/span>\n    {\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">new<\/span> AndroidButton();\n    }\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> Widget <span class=\"hljs-title\">CreateTextField<\/span>(<span class=\"hljs-params\"><\/span>)<\/span>\n    {\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">new<\/span> AndroidTextField();\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Seventh, define the <code>App<\/code> class that takes an instance of the <code>IWidgetFactory<\/code> interface and uses it to create and use widgets:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-comment\">\/\/ App (Client) that to create and use widgets<\/span>\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">App<\/span>\n{\n    <span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-keyword\">readonly<\/span> IWidgetFactory factory;\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-title\">App<\/span>(<span class=\"hljs-params\">IWidgetFactory factory<\/span>)<\/span>\n    {\n        <span class=\"hljs-keyword\">this<\/span>.factory = factory;\n    }\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">RenderUI<\/span>(<span class=\"hljs-params\"><\/span>)<\/span>\n    {\n        Widget button = factory.CreateButton();\n        Widget textField = factory.CreateTextField();\n\n        button.Render();\n        textField.Render();\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Eighth, the <code>Main()<\/code> method of the <code>Program<\/code> class prompts the user to enter the platform they want to create widgets for and then uses the appropriate concrete factory to create the widgets. It then creates an <code>App<\/code> object and passes the factory to render the widget to it:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Program<\/span>\n{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">Main<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">string<\/span>&#91;] args<\/span>)<\/span>\n    {\n        WriteLine(<span class=\"hljs-string\">\"Enter the platform (ios or android): \"<\/span>);\n        <span class=\"hljs-keyword\">var<\/span> platform = ReadLine().ToLower();\n\n        IWidgetFactory factory;\n        <span class=\"hljs-keyword\">if<\/span> (platform == <span class=\"hljs-string\">\"ios\"<\/span>)\n        {\n            factory = <span class=\"hljs-keyword\">new<\/span> IOSWidgetFactory();\n        }\n        <span class=\"hljs-keyword\">else<\/span> <span class=\"hljs-keyword\">if<\/span> (platform == <span class=\"hljs-string\">\"android\"<\/span>)\n        {\n            factory = <span class=\"hljs-keyword\">new<\/span> AndroidWidgetFactory();\n        }\n        <span class=\"hljs-keyword\">else<\/span>\n        {\n            WriteLine(<span class=\"hljs-string\">\"Invalid platform entered.\"<\/span>);\n            <span class=\"hljs-keyword\">return<\/span>;\n        }\n\n        <span class=\"hljs-keyword\">var<\/span> app = <span class=\"hljs-keyword\">new<\/span> App(factory);\n        app.RenderUI();\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following shows the output of the program when you enter iOS as the platform:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">Enter the platform (ios or android):\nios\nRendering iOS button...\nRendering iOS text field...<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If you enter Android as the platform, you&#8217;ll see the output like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">Enter the platform (ios or android):\nandroid\nRendering Android button...\nRendering Android text field...<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Here&#8217;s how the classes and interfaces in the program map to the participants in the Abstract Factory pattern:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Abstract Factory: The <code>IWidgetFactory<\/code> interface.<\/li>\n\n\n\n<li>Concrete Factory: The <code>IOSWidgetFactory<\/code> and <code>AndroidWidgetFactory<\/code> classes.<\/li>\n\n\n\n<li>Abstract Product: The <code>Widget<\/code> abstract class.<\/li>\n\n\n\n<li>Concrete Product: The <code>IOSButton<\/code>, <code>IOSTextField<\/code>, <code>AndroidButton<\/code>, and <code>AndroidTextField<\/code> classes.<\/li>\n\n\n\n<li>Client: The <code>App<\/code> class.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The Abstract Factory pattern is a creational design pattern that provides an interface for creating families of related or dependent objects without specifying their concrete classes.<\/li>\n\n\n\n<li>The Abstract Factory pattern is useful when a system needs to be independent of the way the objects it creates are generated or composed.<\/li>\n\n\n\n<li>The Abstract Factory pattern promotes loose coupling by allowing a client to create objects without needing to know the specific classes of objects it creates.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"1610\"\n\t\t\t\tdata-post-url=\"https:\/\/www.csharptutorial.net\/csharp-design-patterns\/csharp-abstract-factory\/\"\n\t\t\t\tdata-post-title=\"C# Abstract Factory\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"1610\"\n\t\t\t\tdata-post-url=\"https:\/\/www.csharptutorial.net\/csharp-design-patterns\/csharp-abstract-factory\/\"\n\t\t\t\tdata-post-title=\"C# Abstract Factory\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn how to use the C# abstract factory pattern to create families of dependent objects without specifying their concrete classes.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1441,"menu_order":7,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1610","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/1610","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/comments?post=1610"}],"version-history":[{"count":5,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/1610\/revisions"}],"predecessor-version":[{"id":2717,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/1610\/revisions\/2717"}],"up":[{"embeddable":true,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/1441"}],"wp:attachment":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/media?parent=1610"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}