DEV Community

Cover image for Factory Design Pattern
Youssef Ghonem
Youssef Ghonem

Posted on

Factory Design Pattern

A factory is an object which is used for creating other objects.

we can say that a factory is a class with a method. That method will create and return different types of objects based on the input parameter.
Why Use Factory ?
we create an object without exposing the object creation logic to the client.
الفاكتوري باترن بيستخدم اثناء الكرييت الاوبجيت بتاعك والاهميه بتاعه هي انه بيخفي الامبلمنتيشن بتاعك عن الكلاينت و الكلاينت اقصد بيها هي ال
main method ( console application) or application layer (in clean architecture ) or services .. etc
انك تقلل كميه الكود اللي بتكتبها في الابلكيشن لاير و تكون منفصله ف مكان تاني بيحافظ على السلوك بتاع الانكبسوليشن
هتحس بأهميه الباترن دا لما يكون الاوبجيكت بياخد باراميترز كتيره في الكونستراتور ف انت عايز تكريت الاوبجيت ده ف مكان منفصل
لوعندي كلاس اسمه كريديت كارد و ده ليه اكتر من نوع
(Titanium, Platinum, MoneyBack)
الثلاث انواع دول بيورث من الكلاس الكريديت كارد وعلى حسب ما اليوزر يختار النوع انا عايز اكريت الاوبجيكت بتاعه الحل التقليدي هيكون كدا
A pie chart showing 40% responded "Yes", 50% responded "No" and 10% responded "Not sure"
The above code implementation is very straightforward. Once we get the CardType value, then by using the if-else condition we are creating the appropriate Credit Card instance.
هنا انا عملت الكونديشنز بتاعتي في البروجرم و دا عادي بس الباترن هنا بيقولك على طريقةاحسن للكرييت بتاعه الابجيكتس دي لان فيه مشاكل ممكن تواجهك وهي لو الاوبجيت مثلا بياخد 20 باراميتر ف الكونستراكتور فهتلاحظ ان كميه الكود اللي هتتكتب كتيره ف الافضل انك تفصلها ده غير انك خليت فيه اعتماديه على الكلاينت وده مش صح
The above code implementation introduces the following problems

  1. First, the tight coupling between the client class (Program.cs) and Product Class (MoneyBack, Titanium, and Platinum).
  2. Secondly, if we add a new Credit Card, then also we need to modify the Main method by adding an extra if-else condition which not only overheads in the development but also in the testing process الحل بقى اننا نعمل كلاس فاكتوري نحط فيه الامبلمنتيشن بتاعنا زيكدا A pie chart showing 60% responded "Yes", 70% responded "No" and 10% responded "Not sure" in program.cs انت هنا بتعمل كول للفاكتوري بتاعك A pie chart showing 60% responded "Yes", 70% responded "No" and 10% responded "Not sure" Problems of Simple Factory Pattern

في الباترن دا فيه مشكلتين اول مشكله وهي اني لو ضيفت نوع كمان هضطر اني ازود كونديشنز تانيه وده طبعا
violates the open/closed design principle.
ثانيا احنا هنا عملنا فاكتوري واحد بيعمل مانيج للثلاث اوبجيكتس وده يعتبر
tight coupling between the Factory (CreditCardFactory) class and product classes (MoneyBack, Titanium, and Platinum).

code reference:https://github.com/YoussefGhonem/DesignPattern


I am going to discuss how to overcome the above problem by using the Factory Method Design Pattern

Please post your feedback, question, or comments about this article
facebook
linkedIn

Top comments (0)