DEV Community

Cover image for Factory Method Design Pattern
Youssef Ghonem
Youssef Ghonem

Posted on

Factory Method Design Pattern

Please read my previous article i discussed the Factory Design Pattern in C# with example before read this.
What is Abstract Factory Design Pattern?

Define an interface for creating an object, but let the subclasses decide which class to instantiate. The Factory method lets a class defer instantiation it uses to subclasses.

when we need to create the object (i.e. instance of the Product class) without exposing the object creation logic to the client.
في المقال اللي فات وضحت ان الفاكتوري باترن بيستخدم اثناء الكرييت الاوبجيت بتاعك والاهميه بتاعه هي انه بيخفي الامبلمنتيشن بتاعك عن الكلاينت
وكان فيه مشكلتيين في الفاكتوري
tight coupling between the Factory and violates the open/closed design principle
هشرح على نفس المثال بتاع الكريديت كارد
Create a interface with the name ICreditCard and (MoneyBack, Titanium, and Platinum) classes implementing the ICreditCard.
A pie chart showing 60% responded "Yes", 70% responded "No" and 10% responded "Not sure"
هنا بدأت اعمل امبلمينت للفاكتوري وهو اني اكريت لكل انتيتي فاكتوري خاص بيه وهو المسؤل عن كريت الاوبجيت بتاعه واقدر اكتب اي لوجيك انا عايزه براحتي
A pie chart showing 60% responded "Yes", 70% responded "No" and 10% responded "Not sure"
Consuming the factory Method in the Client Code (program.cs):
If you want to create an instance of the Platinum CreditCard then call the Create method of the PlatinumFactory instance

Image description
code reference:

I am going to discuss the Abstract Factory Design Pattern in C# with an example. In this article, I try to explain the Factory Method Design Pattern in C# step by step with an example. I hope this article will help you with your needs. I would like to have your feedback. Please post your feedback, question, or comments about this article.


Top comments (0)