A Guide to Abstract Factory Methods

Design Patterns
Reading Time: 10 minutes

Introduction

Design patterns serve as proven solutions to common challenges, providing a shared language and best practices that elevate the quality and efficiency of software construction. In this exploration, we delve deeper into the Factory and Abstract Factory Methods, both categorised under creational design patterns. Uncover the core principles behind these methods, reveal the secrets that empower developers to optimise their code, enhance scalability, and strike a harmonious balance between flexibility and structure.

Moreover, design patterns and SOLID principles are intertwined in software development. SOLID, an acronym for five crucial design principles (SRP, OCP, LSP, ISP, and DIP), plays a vital role in crafting robust, maintainable, and adaptable code. Understanding their importance in the context of design patterns is fundamental for developers.

As we explore the Factory and Abstract Factory Methods, keep in mind that adherence to SOLID principles is key to achieving a better and optimised codebase. The Abstract Factory Method, for instance, emphasises the Open/Closed Principle, showcasing the interplay between design patterns and SOLID principles for a more efficient and adaptable software development process.


What are those?

Single Responsibility Principle (SRP)

  • A class should have only one reason to change.

  • Encourages a class to have a single responsibility.

Open/Closed Principle (OCP)

  • Software entities should be open for extension but closed for modification.

  • New functionalities should be added through extension without altering existing code.

Liskov Substitution Principle (LSP)

  • Subtypes should be interchangeable with their base types without compromising program correctness.

  • Objects of a base class should be replaceable with objects of a derived class.

Interface Segregation Principle (ISP)

  • A class should not be obligated to implement interfaces it doesn’t utilise.

  • Promotes the creation of specific interfaces for clients, preventing unnecessary method burdens.

Dependency Inversion Principle (DIP)

  • High-level modules ought not to rely on low-level modules; instead, both should be dependent on abstractions.

  • Abstractions should avoid relying on specific details; instead, details should depend on abstractions.


What are Design Patterns in Software Development?

Design patterns originated from architectural principles introduced by Christopher Alexander. In 1994, they were adapted for software engineering by Erich Gamma and their team. The purpose is to provide a shared language and proven practices, enhancing collaboration, reducing development time, and improving overall efficiency in construction processes.

Design patterns provide reusable solutions for common issues that arise in the process of software design and development. They represent best practices and proven solutions that have evolved through experience and usage in various software development scenarios. These patterns provide a structured way to solve design problems, making code more maintainable, flexible, and easier to understand.

Design patterns are further classified into three distinct categories:

Structural Design Patterns
Structural patterns in software development are blueprints or templates that help organise and connect different parts of a system. They define how classes and objects collaborate, making it easier to build flexible and maintainable software by outlining relationships between components without specifying exact implementation details. Examples include Singleton, Facade Pattern, Composite Pattern, and others.

Creational Design Patterns
These patterns focus on object creation mechanisms, providing ways to create objects in a manner suitable for the situation. Instances of design patterns include Singleton, Factory Method, Abstract Factory, Builder, and Prototype.

Behavioural Design Patterns
Behavioural design patterns are a category of design patterns in software development that focus on how objects interact and communicate with each other. These patterns define how classes and objects can collaborate to accomplish more complex behaviours, distributing responsibilities among them.


Why Should Developers Pay Attention?

  • Proven Solutions: Design patterns encapsulate proven solutions to common problems. They are tried and tested approaches to recurring challenges in software design.

  • Code Reusability: Design patterns promote code reusability by providing templates for solving specific types of problems. This reduces redundancy and makes the codebase more maintainable.

  • Shared Language: Design patterns establish a shared language among developers. They provide a common vocabulary, making it easier for team members to communicate and understand each other’s code.

  • Best Practices: They encapsulate the collective wisdom of experienced developers and help ensure that code adheres to established standards.

  • Abstraction and Flexibility: Design patterns encourage abstraction, allowing developers to work with high-level concepts rather than dealing with low-level details. This abstraction enhances flexibility and adaptability to changes in requirements.

  • Scalability: By providing scalable and flexible solutions, design patterns enable applications to grow and evolve without compromising their structural integrity. This is particularly crucial as software requirements change over time.

  • Improved Maintainability: Design patterns contribute to the maintainability of code by organising it in a structured and understandable manner. This makes it easier for developers to debug, modify, and extend existing code.

  • Efficient Problem Solving: Design patterns offer efficient solutions to specific types of problems. Instead of reinventing the wheel for each new problem, developers can leverage existing patterns to solve similar issues.

  • Accelerated Development: Using design patterns can accelerate the development process. Developers can focus on higher-level architectural decisions, confident they are implementing well-established and efficient solutions to common challenges.

  • Enhanced Collaboration: Design patterns facilitate collaboration within development teams. When team members are familiar with common design patterns, it streamlines communication and ensures a collective understanding of the software architecture.


Factory Method

The Factory Method pattern is a creational design pattern, where a single abstract factory class/interface declares a method for creating products. Subclasses of the factory provide a concrete implementation of the factory method to create specific product instances. The factory method is usually overridden by the subclasses to instantiate and return the appropriate product.

The Factory Method pattern primarily focuses on creating a single product type. It allows for flexibility in creating different variations of that product by providing different factory subclasses.

General Example

Imagine we have a computer store app that initially only sells desktop computers. Later, we added laptops and tablets. To handle this variety, we introduced the Factory Method.

Now, we have a ComputerFactory. Its job is to handle all machine selling based on user preferences. If a user wants a desktop computer, the factory creates and sells a desktop computer, and for any other specific requests, it handles those too. The beauty of using the Factory Method here is that it makes the app scalable. If we want to add more computer categories, we only need to update the factory, keeping our app organised and easily expandable.

Key Components

Product:

  • Represents the objects created by the factory.

  • Could be an interface, an abstract class, or a concrete class.

  • For instance, if the factory produces different types of vehicles, ‘Vehicle’ would be the product.

Factory:

  • Encapsulates the object creation logic.

  • It contains methods responsible for creating instances of products.

  • It separates the client code from the object instantiation.

  • Provides flexibility by allowing subclasses to alter the way objects are created without changing the client code.

Concrete Products:

  • Actual implementations of products created by the factory.

  • Each concrete product is a specific type derived from the product interface or abstract class.

  • For example, ‘Car’ and ‘Motorcycle’ could be concrete products derived from the ‘Vehicle’ product.

Client:

  • Utilises the factory to create instances of products.

  • Doesn’t directly instantiate products but relies on the factory to do so.

Implementation Examples:

When to use?

  • When we have runtime requirements

  • We need encapsulation

  • To increase the reusability of the code and remove duplicates

  • To avoid the tight coupling

Usage Scenarios

  • Creating different types of products (e.g., phones, laptops) with varying specifications in an e-commerce application.

  • Generating multiple types of notifications (e.g., email, SMS, push) based on user preferences in a messaging app.

Best Practices

  • Use Descriptive Naming:

    • Choose meaningful names for factories and methods to ensure clarity in object creation.

  • Keep Factories Simple:

    • Ensure that factory classes focus solely on object creation and do not contain unrelated logic.

  • Follow the Open/Closed Principle:

    • Design factories in a way that allows easy extension (open for extension) without modifying existing code (closed for modification).

  • Encapsulate Object Creation Logic:

    • Centralise object creation logic within the factory, abstracting it from the client code.

  • Apply Single Responsibility Principle (SRP):

    • Each factory method should have a single responsibility: creating a specific type of object.

Pros

  • Empowers subclasses to determine the specific types of objects they create.

  • Encourages code reuse by centralising creation logic

  • Supports easy extension with new product types

Cons

  • Increased complexity with more classes/interfaces

  • Adds an extra level of abstraction


Abstract Factory 

On the other hand, the Abstract Factory Method is designed to create families of related products. It defines multiple abstract factory classes/interfaces, each with its own set of methods for creating different types of products. Concrete factory implementations provide the specific implementation for creating products within a particular family/variant.

The Abstract Factory Method allows you to create multiple types of products that are designed to work together or belong to a specific variant. It provides a way to create families of related products without directly specifying their concrete classes.

General Example

Imagine we have a postcard office that initially only allowed ships the posts via sea and land routes. Later, we expanded to include sailboats and bikes in the sea and land shipping category. To manage this diversity, we introduced the Abstract Factory.

Now, we have an abstract ParcelFactory interface. We’ve implemented SeaFrieghtFactory and RoadFrieghtFactory classes, each responsible for shipping the parcels by motorboat, sailboat, and bike, respectively.

Our ParcelFactory uses these concrete factories. Based on the user’s preference, if they want to ship via motorboat, it utilises the SeaFreightFactory to ship by sea using a motorboat, and for truck shipping, it employs the RoadFrieghtFactory to handle the thorough road shipping.

The Abstract Factory Method allows our app to be flexible and scalable. If we decide to add more shipping categories in the future, we can create new factories that follow the ParcelFactory interface, keeping our app structured and easily adaptable to changes.

Key Components

Abstract Factory:

  • Declares an interface for creating families of related or dependent objects without specifying their concrete classes.

  • It contains a set of factory methods, each responsible for creating a different kind of product.

  • Can be an interface or an abstract class.

Concrete Factory:

  • Implements the Abstract Factory interface.

  • Produces a family of related products.

  • Provides methods to create different types of products that are consistent across a product family.

  • For example, a ‘CarFactory’ could create ‘CarBody’, ‘CarEngine’, and ‘CarTire’ objects that are designed to work together.

Product Interfaces:

  • Define the interfaces of distinct products but do not specify their concrete classes.

  • Products within a family share a common theme or are designed to work together seamlessly.

  • For instance, ‘VehicleFactory’ might include interfaces for ‘Body’, ‘Engine’, and ‘Tire’.

Client:

  • Uses interfaces provided by the Abstract Factory to create families of related or dependent objects.

  • Remains independent of the specific classes of objects created.

Implementation Examples

When to use?

  • All use cases of Factory methods.

  • When working with families of related objects.

  • To ensure consistency and compatibility.

Usage Scenarios

  • Generating UI elements for different device types (e.g., mobile, desktop) with specific layouts and styles.

  • Creating various types of reports (e.g., financial, statistical) with distinct data representations and formats in a reporting tool.

Best Practices

  • Define Clear Interfaces:

    • Design clear and concise interfaces for factories and related product families to ensure consistency.

  • Separate Creation Logic:

    • Abstract the creation of related object families from the client code by using factory methods.

  • Leverage Inheritance and Polymorphism:

    • Use inheritance and polymorphism to create families of related objects and ensure their interchangeability.

  • Avoid Violating the Single Responsibility Principle:

    • Ensure that each concrete factory maintains the creation of related objects and does not take on unrelated responsibilities.

  • Maintain Consistency Across Products:

    • Ensure that products created within the same family adhere to a common theme or interface, promoting consistency.

Pros

  • Creates related object families seamlessly.

  • Ensures consistency and compatibility within families.

  • Allows easy swapping of object families.

Cons

  • Extension Complexity: Adding new types might get complex.

  • Initial Effort: Requires effort to set up interfaces initially.


Comparison and Contrast

  • Number of Objects:

    • Factory Method: Creates individual objects of a single type.

    • Abstract Factory Method: Generates families of related objects with consistent themes or interfaces.

  • Responsibility:

    • Factory Method: Handles the creation of a single type of object.

    • Abstract Factory Method: Manages the creation of multiple related objects that work together.

  • Scalability:

    • Factory Method: Less scalable for managing families of related objects.

    • Abstract Factory Method: More scalable for creating families of cohesive objects.

  • Flexibility:

    • Factory Method: Provides less flexibility for creating complex object structures.

    • Abstract Factory Method: Offers higher flexibility in creating families of related objects, ensuring consistency and interchangeability among them.

  • Usage Scenario:

    • Factory Method: Best suited when dealing with the creation of individual, non-related objects with a single responsibility.

    • Abstract Factory Method: Ideal for scenarios where creating families of related objects with a consistent theme or interface is required.


Key Takeaways:

Scope and Responsibility:

  • Factory Method: Creates single objects with a specific responsibility.

  • Abstract Factory Method: Manages families of related objects, ensuring their cohesive interaction.

Flexibility and Scalability:

  • Factory Method: Less flexible for creating families of related objects.

  • Abstract Factory Method: More extensible and scalable, ideal for creating related object families.

Usage Scenarios:

  • Choose Factory Method for single object creation and simple scenarios.

  • Utilise Abstract Factory Methods for creating families of related objects with cohesive themes.


Conclusion

Design patterns play a crucial role in software engineering, offering reusable solutions to common design problems. Among these patterns, the Factory and Abstract Factory Methods focus on object creation but differ in their scopes and responsibilities.

Factory Method:

  • Provides an interface for creating individual objects without exposing their concrete classes.

  • Creates single types of objects based on specified conditions or types.

  • Encapsulates object creation logic, promoting loose coupling between clients and objects.

Abstract Factory Method:

  • Provides an interface for creating families of related or dependent objects without requiring the use of their concrete classes.

  • Generates cohesive families of objects adhering to a common theme or interface.

  • Ensures consistency and interchangeability among objects within a family, promoting scalability and flexibility.


What’s next?

Facade Design Pattern
The Facade design pattern is like a TV remote control. We use it without knowing how the TV works inside. Likewise, a facade design pattern is like that remote, it simplifies using complex things, giving us buttons to control it without needing to know all the complicated details like hardware and software working.

General Example
A call centre with different sales, technical support, and billing departments. The main menu we hear when we call (press 1 for sales, press 2 for technical support, etc.) acts as a Facade. Regardless of the option we choose, we eventually direct to one operator who then routes our call to the appropriate department behind the scenes. This simplified interface shields us from the complexity of navigating multiple departments, allowing us to interact with just one person who manages our query.

Here’s a Quick Guide to Facade Design Pattern

Simplify Your Code with Factory and Abstract Factory. Dive in for Dev Insights at VT Netzwelt. Level up your software skills today!


Sharing is Caring
Rinku Khatri
Author:
Rinku Khatri is a Senior Mobile App Software Engineer at VT Netzwelt who emphasises the importance of consistent effort. He possesses strong app development skills, actively shares educational content, and creates value for his followers.

Leave a Reply

Your email address will not be published. Required fields are marked *

Please fill in the details and our representative will be in touch with you shortly.
VT Netzwelt Close