Cover of Design Patterns by Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides - Business and Economics Book

From "Design Patterns"

Author: Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides
Publisher: Pearson Education
Year: 1994
Category: Computers

🎧 Free Preview Complete

You've listened to your free 10-minute preview.
Sign up free to continue listening to the full summary.

🎧 Listen to Summary

Free 10-min Preview
0:00
Speed:
10:00 free remaining
Chapter 3: Creational Patterns
Key Insight 2 from this chapter

Abstract Factory Pattern

Key Insight

The Abstract Factory pattern provides an interface for creating families of related or dependent objects without explicitly specifying their concrete classes, often referred to as 'Kit'. This pattern addresses challenges in systems requiring portability across multiple standards, such as a user interface toolkit supporting various look-and-feel standards like Motif and Presentation Manager. By defining an abstract 'WidgetFactory' interface for creating each basic widget type, with concrete subclasses implementing widgets for specific standards, applications avoid hard-coding look-and-feel-specific classes, making it easy to change the overall theme later. Clients interact only with the abstract factory interface, maintaining independence from the concrete widget implementations and ensuring consistency within a product family.

Key applications for the Abstract Factory pattern include scenarios where a system needs independence from its product's creation, composition, and representation, or when it must be configurable with one of multiple product families. It is particularly useful for enforcing constraints that a family of related product objects must be used together, and for providing a class library where only interfaces, not implementations, are revealed. The pattern's structure involves 'AbstractFactory' (declares creation operations), 'ConcreteFactory' (implements them), 'AbstractProduct' (interface for product types), 'ConcreteProduct' (defines/implements products), and the 'Client' (uses abstract interfaces).

The pattern offers significant benefits by isolating clients from concrete implementation classes, as product class names are confined to the concrete factory's implementation. It facilitates easy exchange of entire product families, as the concrete factory is instantiated only once, allowing simple configuration changes. Moreover, it promotes consistency by ensuring that all product objects used together come from the same family. However, a notable liability is the difficulty in supporting new kinds of products, as extending the 'AbstractFactory' interface typically necessitates modifying the abstract class and all its subclasses, thus fixing the set of products that can be created. Implementations often involve concrete factories as Singletons and use factory methods or even the Prototype pattern for creating products. For instance, a 'MazeFactory' can be a concrete Abstract Factory providing methods to create specific 'Room', 'Wall', or 'Door' types, with subclasses like 'BombedMazeFactory' overriding these to produce specialized components.

📚 Continue Your Learning Journey — No Payment Required

Access the complete Design Patterns summary with audio narration, key takeaways, and actionable insights from Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides.