Sitemap

Factory Method Pattern

3 min readMay 23, 2021
Press enter or click to view image in full size

The factories can have different types of production lines as their value propositions. The different production lines may consists with its specific operations as well collaboration operations. Each line should deliver accurate and error-free deliveries through their operations to support for entire factory.

When we come to programming, the directly object creation in the class is really inflexible since it binds with the class. Also, such a instantiation makes it impossible to change independently from class to class.

But fortunately, we have Factory Method Pattern. 😃

What it does is help to create new objects using an abstract classes and also hides the implementation logic from the users. Thereby, Factory Method Pattern creates the better level of encapsulation.

Pros and Cons of Factory Method Pattern

👍Pros:

  • Increase the agility by enhancing the adaptability over business needs.
  • Make it easy to conduct validation (Unit test) over components.
  • Make stable structure when the business requirements are complicated.

👎Cons:

  • Not easy to implement than singleton.
  • Newbies may not be understandable the concept in factory method.
  • Required more memory.

Comparison between Singleton pattern and Factory Method Pattern

Now it’s time to go in depth about the Factory Method Patter. Let’s discuss about this with real case scenario.

|Case Scenario

We have a garment factory called GKGarment Factory. They have soo many employees who work on different departments with their factory like Marketing and Sales, Finance and Human Resources, Production, Logistics Operation and Transportation and so on.

Note: For this example I only consider about 2 departments called Production and Transportation.

With this Covid-19 situation, they have decided to work in Shift-base by based on the employee’s id. If the employee’s ID is an even number then they are working on 3 days (Monday, Wednesday, Friday) while others working on rest of 3 days ( Tuesday, Thursday, Saturday). Sunday is a holyday for everyone in GKGarment.

My implementation will show you the days which are specific employee should work on by based on their ID and Department. ⭐️Please keep remember that I have only created this program for 2 departments in GKGarment.

At the beginning, I have created a “Employee” abstract class (you can use interface also) to define all the methods and variables which are used by our sub-classes. Also, you can see here, I made the displayDates() (line no 8) as abstract to hide the implementation details.

Next, I created a enum (called Areas — Which employee is working on) to mentioned about the 2 department that the GKGarment Factory have (which we only consider).

Then, I created 2 sub classes by extending Employee “Production” and “Transportation” to manage the employee details who are working on these 2 departments and invoke the displayDates().

Now we are going to the actual factory implementation. This is the place where an actual object is created according to the working area.

At the end, I use main method to display dates by invoking the methods in our abstract class (Employee) and object creation (GKGarmentFactory).

Right we have done implementation of Factory Method Pattern 👏

If you need to add another area to display working dates for employees, what you only need to do is,

  • Add your Area into enum (Areas),
  • Implement it by extending Employee class(Like Production.java, Transportation.java),
  • Come and create object is the factory (In this example, inside of GKGarmentFactory.java)

So, I hope you can get a clear understand about Factory Method Pattern implementation with this very real scenario. In my next article, let’s discuss about the Prototype Design Pattern.

References:

https://www.ionos.com/digitalguide/websites/web-development/what-is-a-factory-method-pattern/

https://www.c-sharpcorner.com/interview-question/what-is-the-difference-between-singleton-design-pattern-and-factory-design-pattern

--

--

No responses yet