In this article we will try to implement Factory Design pattern using an example of a knife factory which produces different types of knives like chef knife, bread knife etc.
Q. What is the Goal/Purpose of Factory Pattern with this analogy?
A. Just like a knife factory in the real world, the purpose of these factories in object-oriented programming is to create objects.
Q. When/Where we should use Factory Pattern?
A. Let's try to understand this scenario with our knife example.
Below i have implemented a method, orderKnife(){}, which takes string input of the type of knife to be created, creates that type of knife, then performs some common knife operations like sharpen(), polish() etc and returns back the knife object. Here we are using concrete instantiation.
Concrete instantiation is the act of actually instantiating a class to create an object of a specific type.
- Knife orderKnife(string knifetype)
- {
- Knife knife;
- //create knife object using concrete instantiation
- if (knifetype=="steak")
- {
- knife = new SteakKnife();
- }
- else if(knifetype=="chefs")
- {
- knife = new ChefsKnife();
- }
- //prepare the knife
- knife.sharpen();
- knife.polish();
- knife.package();
- return knife;
- }
- Knife orderKnife(string knifetype)
- {
- Knife knife;
- //create knife object using concrete instantiation
- if (knifetype=="steak")
- {
- knife = new SteakKnife();
- }
- else if(knifetype=="chefs")
- {
- knife = new ChefsKnife();
- }
- else if (knifetype == "bread")
- {
- knife = new BreadKnife();
- }
- else if (knifetype == "paring")
- {
- knife = new ParingKnife();
- }
- //prepare the knife
- knife.sharpen();
- knife.polish();
- knife.package();
- return knife;
- }
Q. What is Factory Object?
A. The role of Factory Object is to create product objects of a particular type.
In general, a Factory Object is an instance of such a class, which has a method to create product objects. So in our case we will use this Factory Object class to provide us with different types of knife class objects.
Q. How do we implement Factory Object?
Step 1 - Implementing Factory Class
Here we are creating Knifefactory class which is responsible for creating knife object of knifetype.
- public class KnifeFactory
- {
- public Knife createKnife(string knifetype)
- {
- Knife knife = null;
- //creating knife object
- if (knifetype == "steak")
- {
- knife = new SteakKnife();
- }
- else if (knifetype == "chefs")
- {
- knife = new ChefsKnife();
- }
- return knife;
- }
- }
Here we will create KnifeStore class which is responsible for instantiating KnifeFactory object through constructor and then uses the same factory object to create knifetype object by calling createKnife() of factory class, inside orderKnife() is responsible for executing common knife functions to prepare the knife irrespective of knifetype.
- public class KnifeStore
- {
- private KnifeFactory factory;
- //KnifeFactory object should be passed to this constructor
- public KnifeStore(KnifeFactory factory)
- {
- this.factory = factory;
- }
- public Knife orderKnife(string knifetype)
- {
- Knife knife;
- //using the create method in the factory
- knife = factory.createKnife(knifetype);
- //prepare the knife
- knife.sharpen();
- knife.polish();
- knife.package();
- return knife;
- }
- }
Here we have separated the responsibilty of creating Knife Object and preparing knife to separate classes.
Q. Advantages of Factory Pattern?
-
The Knifestore and its orderKnife() may not be the only client of KnifeFactory. In other words, we may need to implement bulkorder or equitytesting etc classes that can also use the same Knifefactory object .
-
Since all types of actual knife creation happen in the knifeFactory, we can simply add new knife types to our knifefactory without modifying the client code.
-
If there are multiple clients that want to instantiate the same set of classes, then by using a factory object, we have reduced the redundant code and made the software/program easier to modify.

Rushi MehtaPosted Jan 27, 2020, 9:51 AM
Nice Article
Sourav Kumar DasPosted Jan 25, 2020, 12:23 PM
Nice article.