The Abstract Factory Design Pattern is a creational pattern that provides an interface for creating families of related or dependent objects without specifying their concrete classes. It allows a system to be independent of how its objects are created, composed, or represented.
-
Abstract Factory Definition: An interface or abstract class that declares the methods for creating abstract products. It defines a set of related products (or objects) that the factory can create. Role: The abstract factory is responsible for producing families of related objects (like creating a set of UI elements for a theme or specific components for an operating system) without specifying their concrete types.
-
Concrete Factory Definition: A class that implements the abstract factory interface. Each concrete factory is responsible for creating a specific set of related objects (i.e., a specific "family"). Role: Produces objects that belong to the same product family. For example, a concrete factory might create "Windows" components or "Linux" components, ensuring that all created products are compatible.
-
Abstract Product Definition: An interface or abstract class that declares the behaviors (methods) that products in the family must implement. Role: Defines the common interface for a family of products. For instance, in a UI system, a "Button" or "TextField" could be abstract products with shared behaviors like
render()oronClick(). -
Concrete Product Definition: A class that implements the abstract product interface. Each product is a specific implementation that belongs to a particular family created by a concrete factory. Role: The actual products produced by a concrete factory. For example, a WindowsButton or LinuxButton would implement the Button abstract product interface.
-
Client Definition: The part of the system that interacts with the abstract factory and abstract products. Role: The client uses only interfaces defined by the abstract factory and abstract products. It doesn't know or depend on the specific concrete classes, which allows flexibility in changing the underlying product family (e.g., switching from a "Windows" UI to a "Linux" UI) without changing the client code.
-
Interface IOperatingSystemFactory: This interface is responsible for producing families of related objects, in this case, different operating systems. It abstracts the creation process, allowing the client to use any specific factory without knowing the details of the underlying implementations.
-
Concrete Factory WindowsOSFactory, LinuxOSFactory: These classes are concrete implementations of the
IOperatingSystemFactoryinterface. Each factory is responsible for creating a specific family of products, in this case, either a Windows or Linux operating system. This ensures that all products created by a factory are compatible with each other. -
Abstract Class IOperatingSystem: This interface defines the common behaviors for all operating systems. It acts as a contract that all concrete operating systems (like WindowsOS and LinuxOS) must fulfill, ensuring that they implement the specified methods.
-
Concrete Class WindowsOS, LinuxOS: These classes are concrete implementations of the
IOperatingSysteminterface. They represent specific products produced by their respective factories. Each class implements the behaviors defined in the abstract product interface, providing the necessary functionality specific to each operating system.
class Program
{
static void Main(string[] args)
{
IOperatingSystemFactory windowsFactory = new WindowsOSFactory();
Client windowsClient = new Client(windowsFactory);
windowsClient.SetupOperatingSystem();
IOperatingSystemFactory linuxFactory = new LinuxOSFactory();
Client linuxClient = new Client(linuxFactory);
linuxClient.SetupOperatingSystem();
}
}| Aspect | Abstract Factory Pattern | Factory Pattern |
|---|---|---|
| Purpose | Creates individual objects based on input | Creates families of related objects |
| Complexity | Simpler, lower level | More complex, higher level |
| Use Cases | Single object creation with flexibility | Cross-platform or themed systems needing cohesive components |
| Flexibility | Limited to single object creation | Scalable to families of objects |
| Structure | Often uses a single factory class | Typically uses a factory interface with multiple implementations for families |