Factory Method Design Pattern

Source:

In this article it will discuss about the factory method design pattern. When it compare with previous discussed Pattern, it totally opposite for that. Factory method is suitable for the scenario that needs to create multiple instance from one class. This article consists with basic information of factory method, its application and how it is created with real world example.

🏭 What is factory method 🏭

It is a creational design pattern that enable multi object creation. It allows for creating objects from super class while allowing sub classes to alther the object type that wants to create. It is used for following scenarios.

  • When the class doesn’t know what sub classes should be created
  • When the parent class wants that its sub classes specify the objects should be created
  • When the parent classes choose the creation of objects to its sub-classes

Some design patterns like Abstract Factory, Prototype and Builder are starts from the Factory Method and eveolve towards. Developers can use factory method along with the Iterator to return various types of iterator collections. Though this pattern based on inheritence, it doesn’t require any initialization. It can be introduced as the specilization of Template method.

💡 Application of Factory Method

🌠 It can be used to provide capability for the users to extend its components by providing a framework or a library

🌠 It is suitble for when the developers don’t know the exact types and dependencies of the objects related to your scenario

🌠 If you want to save the system resources, factory method is the suitable design pattern. Because it allows for code reusability

Pros 😃

🌠 It provides loose coupling between the classes

🌠 It follows single responsibility principle. It means product creation rules can be isolated

🌠 It allows for open/closed principle. It means adding new products and removing existig products is very easy. It will not effects the exisiting program

Cons 😟

🌠 As you want to implement lots f sub classes, code become more complicated

Use Case of Factory Method

Use Case Scenario: Think there is a cake factory that has different branches like ABC bakers, PQR bakers and XYZ bakers. In the factory they make different types of cake like butter cake, chocolate cake, birthday cake, wedding cakes, and cup cakes. When you select each branch it sell few types of cakes and not all the products. So you have to develop a system to order the cake and choose the correct branch depend on your requirements.

when looking at the following diagram you can clearly understand the structure of the cake factory implementation.

class diagram for the cakefactory

Implementation 💻

To implement the application for above scenario we have to follow few steps.

  • Create an abstract class Cake.java to store the common variables and methods of cakes.
  • Create seperated classes for store the different types of cake implementation. ButterCake.java , ChocolateCake.java , BirthDayCake.java , WeddingCake.java and CupCake.java and they should extends the Cake.java class .
  • Now you need a class to store the branch information. So create another abstract class CakeStore.java to store the common branch information.
  • Then create seperate classes [ ABCBakery.java , PQRBakery.java , XYZBakery.java] to store the each branch information and it should extends the CakeStore.java class.
  • To simplify the implementation we create a enum to store the branch information.
  • Above steps are general implementation. Now we move to the Factory Method implementation.To manage the Cake Store information, you need a factory for that. So now we create the CakeStoreFactory.java class.
  • Finally we create the main application [ MainApp.java ] to get the branch details.
  • Here are the output values for above factory method implementation
Bakery has {cake=[ Birth Day Cake,  Wedding Cake,  Cup Cake]}Bakery has {cake=[ Butter Cake,  Chocolate Cake,  Cup Cake]}Bakery has {cake=[ Butter Cake,  Birth Day Cake,  Cup Cake]}

Hope you have clear understand about factory methods, its application and how it can be implemented in real world scenario. Complete code implementation for above scenario can get .

Stay Safe & Learn New Things!!! 😃

--

--

--

Currently working as an Associate Software Engineer at Virtusa. Has completed degree in B.Sc (Hons) Computing & Information Systems. After all I am a Human...

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Head First Java Chapter 08 — Serious Polymorphism

Easy way for setting up notification for AWS Glue jobs that failed using SNS

The Present & Future of Banking APIs

How to decrease a monolithic build time by 90% using TeamCity, terraform, and common sense

Advanced Mobile App Development Guide for Excellent Mobility Solutions

Why DSA and Algorithms are Important for your Placements and further Career

Simple CTF — TryHackMe — Writeup

READ/DOWNLOAD#> Go in Practice: Includes 70 Techniques FULL BOOK PDF & FULL AUDIOBOOK

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Thilini Weerasinghe

Thilini Weerasinghe

Currently working as an Associate Software Engineer at Virtusa. Has completed degree in (Hons) Computing & Information Systems. After all I am a Human...

More from Medium

How to make Code Review more effective with Pair Programming

Sample code for IBM Event Streams Coding Challenge

How I would architect a new online betting platform in 2022

My Tests are Broken Again, Part 1: Understanding the Problem