Lesson 3
Abstract Factory Pattern
Welcome to the Abstract Factory Pattern

Welcome back! After exploring the Factory Method pattern and how it allows you to create different types of objects through a common interface, we’re now ready to move on to the Abstract Factory pattern. This lesson will guide you through understanding the Abstract Factory pattern — a design pattern that provides an interface for creating families of related or dependent objects without specifying their concrete classes.

What You'll Learn

In this lesson, you’ll delve into the details of the Abstract Factory pattern. You will learn:

  • What the Abstract Factory pattern is and its purpose.
  • How to implement the Abstract Factory pattern to create different types of documents.
  • The distinction between Abstract Factory and Factory Method patterns.
Understanding the Abstract Factory

The Abstract Factory pattern is a creational design pattern that provides an interface for creating families of related or dependent objects without specifying their exact classes. This pattern encapsulates a group of individual factories that have a common theme and can be used to create objects that are designed to work together. By abstracting the creation process, the Abstract Factory pattern ensures that the client code remains unaware of the specific classes being instantiated, promoting loose coupling and enhancing code scalability and maintainability.

We'll explore a comprehensive code example featuring various types of documents such as WordDocument, ExcelDocument, GoogleDoc, and GoogleSheet. These document types will be instantiated using different concrete factories (WordDocumentFactory, ExcelDocumentFactory, etc.) managed by an abstract interface (DocumentAbstractFactory). This approach will give you a more flexible and scalable way to handle complex object structures.

Step 1: Define Abstract Products
Java
1// Abstract Products 2interface Document { 3 void open(); 4} 5 6class WordDocument implements Document { 7 public void open() { 8 System.out.println("Opening Word Document"); 9 } 10} 11 12class ExcelDocument implements Document { 13 public void open() { 14 System.out.println("Opening Excel Document"); 15 } 16}

Here, we define the abstract product Document and its concrete implementations WordDocument and ExcelDocument. These classes implement the open method in their own way.

Step 2: Define Abstract Factory Interface
Java
1// Abstract Factory Interface 2interface DocumentAbstractFactory { 3 Document createDocument(); 4}

The DocumentAbstractFactory interface declares the createDocument method that returns an object of type Document.

Step 3: Implement Concrete Factories
Java
1// Concrete Factories 2class WordDocumentFactory implements DocumentAbstractFactory { 3 public Document createDocument() { 4 return new WordDocument(); 5 } 6} 7 8class ExcelDocumentFactory implements DocumentAbstractFactory { 9 public Document createDocument() { 10 return new ExcelDocument(); 11 } 12}

The concrete factories like WordDocumentFactory and ExcelDocumentFactory implement the DocumentAbstractFactory interface and provide the implementation for createDocument method, returning respective concrete document objects.

Step 4: Using the Abstract Factory
Java
1public class Client { 2 private Document document; 3 4 public Client(DocumentAbstractFactory factory) { 5 document = factory.createDocument(); 6 } 7 8 public void openDocument() { 9 document.open(); 10 } 11 12 public static void main(String[] args) { 13 DocumentAbstractFactory wordFactory = new WordDocumentFactory(); 14 Client client1 = new Client(wordFactory); 15 client1.openDocument(); // Outputs: Opening Word Document 16 17 DocumentAbstractFactory excelFactory = new ExcelDocumentFactory(); 18 Client client2 = new Client(excelFactory); 19 client2.openDocument(); // Outputs: Opening Excel Document 20 } 21}

The Client class takes a DocumentAbstractFactory in its constructor and uses it to create and open a document. In the main method, we show how to use the Client class with different factories (WordDocumentFactory and ExcelDocumentFactory).

Why It Matters

While the Factory Method pattern deals with creating one type of object through inheritance, the Abstract Factory pattern focuses on creating families of related objects. The key difference is that Abstract Factory uses composition to delegate the instantiation process to specific factory objects, whereas Factory Method typically relies on subclassing. Understanding the Abstract Factory pattern is crucial because it helps you manage and create groups of related objects. Imagine working on a project that needs to support multiple document formats across different platforms. The Abstract Factory pattern will ensure that your codebase remains organized, scalable, and easy to maintain.

The advantages include:

  1. Separation of Concerns: Reduces the dependency between the client and the concrete classes, promoting loose coupling.
  2. Scalability: Allows extending the families of related products easily without modifying existing code.
  3. Consistency: Ensures that products created in a family are compatible and consistent with each other.
  4. Code Reusability: Promotes the reuse of code by defining families of objects in one place.

Ready to move on to the practice sections to see how this powerful pattern works in real-world scenarios?

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.