Creational Patterns  «Prev  Next»
Lesson 6 Factory Method: structure
Objective Develop the subclasses for the Vehicle Class.

Develop the Subclasses for the Vehicle Class

I am working as a software Architect for a fortune 500 Company that offers (SAAS) Software as a Service. Under which scenarios are Factory Methods most applicable during the Software Development Lifecycle?
As a software architect working in a Software as a Service (SaaS) environment, the Factory Method pattern is an invaluable tool in your design toolbox. This pattern excels in scenarios where the system needs to remain decoupled and flexible to changes in how objects are created and represented.
Here are some scenarios where the Factory Method pattern is most applicable:
  1. When You Need to Decouple Code from Concrete Classes: The Factory Method is a great way to increase the flexibility of your code by decoupling client code from specific classes it needs to instantiate. The client only interacts with the abstract interface, so it remains functional even if new concrete classes are added to the system.
  2. Dynamic Implementation Switching: If your SaaS platform needs to provide support for multiple database systems, payment gateways, or third-party services, you could use a Factory Method to create connections to these services. This way, the type of connection can be decided at runtime, allowing you to switch between different implementations smoothly.
  3. When Products Require a Complex Creation Process: If the instantiation process involves business logic or dependencies, using a Factory Method can encapsulate this complexity. This way, the client code is shielded from these details, making it easier to read and maintain.
  4. When Derived Classes Determine the Objects to be Created: In cases where the object creation process is based on some specific conditions, a Factory Method is a perfect fit. For instance, based on user role or subscription level, different features or services might need to be instantiated. A Factory Method can encapsulate these rules, keeping the client code simple and unaware of the underlying complexity.
  5. When You Need to Enhance Code Readability and Maintainability: If instantiating an object requires setting many properties or involves complicated logic, a Factory Method can encapsulate this complexity and provide a straightforward way to create the object. This can make the code more readable and easier to maintain.
  6. When You Want to Improve Testability: The Factory Method pattern can improve the testability of your application by providing a straightforward way to substitute real implementations with mock objects.

By applying the Factory Method pattern in these scenarios, you can keep your SaaS platform's code flexible, maintainable, and robust, ready to adapt to changes as they come.

When are Factory Methods most common?

Factory Methods are most common when programmers know at compile time that they need to instantiate an instance of a subclass of a common base class but they do not know which concrete subclass to instantiate. For example, they know the Vehicle class but not the Car, Truck, or Bicycle subclasses. The Factory Method pattern defers the decision of which subclass to instantiate to happen at runtime. The Factory Method pattern has several variations. What unites them all is that a nonconstructor method creates instances of a class. The main variant of the Factory Method uses an abstract Creator class to create instances of an abstract Product class. Concrete subclasses of the Creator class create particular concrete instances of the Product class.

Diagram of Factory Method structure
Factory Method uses an abstract Creator class to create instances of an abstract Product class

Clients see only the two abstract classes "above the water line."
There may in fact be many different classes below the water line, but client programmers never see them.

Parameterized Factory Method

Another popular variation is called the parameterized Factory Method. This uses a single, generally concrete, Creator class with a single Factory Method to create instances of different subclasses. The subclass instantiated is chosen based on the arguments passed to the Factory Method. The openConnection() method of java.net.URL is an example of a parameterized Factory Method.

Diagram of the Factory Method using parameterized Factory Method
Diagram of the Factory Method using parameterized Factory Method

Concrete Subclasses - Exercise

In this exercise, you will write four subclasses for the Vehicle class.
Concrete Subclasses - Exercise