Improve Scalability Using Factory Method Design Pattern

Factory pattern makes our code more robust, less coupled, and highly scalable.

Susith Hemathilaka
Javarevisited
3 min readMar 7, 2022

--

Photo by Vlad Kutepov on Unsplash

Factory design pattern is a creational design pattern also known as Virtual Constructor. It delegates object creation and provides an interface for creating objects in a superclass and altering the sub classes without affecting the existing client implementation. It promotes the loose-coupling by eliminating the need to bind application-specific classes into the code.

Coupling means how tightly a class is related to another class. Coupling must be kept low. Modules anyway depend little on each other. All classes and methods must have small direct visible and flexible relations to other classes. But one module must be easily used by other modules.

Factory pattern removes the instantiation of actual implementation classes from client code. The code interacts solely with the resultant interface or abstract class so that it will work with any classes that implement that interface or that extend that abstract class. It decouples client code from concrete classes and you need to instantiate by allowing the sub-classes to choose the type of objects to create and alter the type of objects that will be created.

When to use?

1.Use the Factory Method when you don’t know beforehand the exact types and dependencies of the objects your code should work with.

2.Use the Factory Method when you want to extend its internal components without breaking the initial client implementation.

3. Use the Factory Method when you want to save system resources by reusing existing objects instead of rebuilding them each time.

Real-world System-Design Analogy

Problem

Think our application has 3 types of data which are categorized by the usage. The application should support different types of data connections according to the situation. Creating a data connection each and every time is not sensible from design aspects. It makes boilerplate code and tight coupling. The following data types are used in our example.

Hot data - data that have frequent access. stores in an in-memory cache store.

Warm data - less frequent access. stores in a database.

Solution

If we applied the factory design pattern, Then we only have to pass a parameter from client code. Then the factory comes into play and it knows which type of data connection instance should return to us.

The client source is only written to give the data viewpoint. Client asks from factory request from the abstract class. the abstract class gives the selected object.

Class Diagram

Implementation

  1. Create an abstract class.

2.Create concrete classes extending the same abstract class.

3. Factory Class- Create a factory to generate objects of concrete class based on given information.

4. Demo- Use the factory to get objects of the concrete class by passing information such as type.

Factory Design Pattern Advantages

  1. Factory design pattern provides an approach to code for interface rather than implementation.
  2. Factory pattern removes the instantiation of actual implementation classes from client code. The factory pattern makes our code more robust, less coupled and easy to extend. As example, we can easily change the HotData class implementation because the client program is unaware of this.
  3. Factory pattern provides abstraction between implementation and client classes through inheritance.

Wrapping Things Up

Finally, I hope you got a clear idea of the factory-method design pattern and its applicability. This article attempts to consolidate most of the common designs approach of the Factory method.

If you willing to learn about other design patterns I wrote,

If there is anything that I have mentioned incorrectly, feel free to comment down below!. If you liked the post, click the 👏 below so more people can see it! Make sure that you follow me on Medium or on My Blog to get updates whenever new article gets published.

Happy Coding!👌

--

--

Susith Hemathilaka
Javarevisited

An avid developer with an unquenchable thirst for anything in programming ✌ University of Westminster • Software Engineer @ DirectFN