Abstract factory pattern 2026-10-31
Provide an interface for creating a family of related products while hiding their concrete software classes. Unlike a single factory method pattern operation returning one product abstraction, an abstract factory typically exposes several related product-creation operations whose implementations coordinate a consistent family.
Creational patterns organize object creation; behavioral patterns organize interactions and responsibility for behavior. A creational pattern controls how concrete software objects are selected, instantiated, or shared, so clients need not hard-code the entire construction policy. Examples include the factory method pattern and the singleton pattern.
A behavioral pattern describes how objects collaborate, receive requests, or distribute an algorithm's behavior. For example the observer pattern lets a subject notify dependants after its state changes, and the strategy pattern supplies interchangeable implementations of an operation. This distinction concerns the pattern's principal responsibility; a system can use both kinds together.
The two patterns are the factory method pattern and the decorator pattern.
Manufacturer.build is the factory operation specialized by the two national manufacturer software classes. Option and its subclasses wrap a Car to add configurable behavior.For the factory method pattern,
Manufacturer is the abstract creator and declares build(carType): Car; UKManufacturer and USManufacturer are concrete creators. Car is the abstract product interface, and Convertible, Family, and Sports are concrete products. Each national creator selects one product software class from carType and configures the national settings. The diagram does not contain distinct families of national product interfaces or several abstract product-creation operations, so abstract factory pattern is not the most precise identification of this single-operation design.For the decorator pattern,
Car is also the component interface. Convertible, Family, and Sports are concrete components; Option is the abstract decorator, both a subtype of Car and associated with a wrapped Car. DieselEngine, AlarmSystem, and AutomaticTransmission are concrete decorators. Each delegates baseline behavior to its wrapped car, augments description, and adds its surcharge to price. National properties should delegate unchanged through decorators.A customer can compose options, for example
new AlarmSystem(new DieselEngine(baseCar)). Every layer remains usable through the Car interface; each layer adds its own description and price contribution. Factory methods choose/configure the base car, and decorators compose optional additions without requiring a subclass for every option combination. If applying each option at most once is required, enforce that as a separate ordering constraint; unrestricted decorator nesting alone permits repeated options.