Factory Method¶
The Factory Method Pattern is a creational design pattern that defines an interface for creating an object but allows subclasses to decide which concrete class to instantiate.
Instead of instantiating objects directly, the creation logic is delegated to factories, making the system easier to extend without modifying existing code.
Structure¶
| Role | Example | Responsibility |
|---|---|---|
| Product | Parser |
Defines the interface for objects created by the factory |
| Concrete Product | CSVParser, JSONParser, XMLParser |
Implements the product interface |
| Creator | ParserFactory |
Declares the factory method |
| Concrete Creator | CSVParserFactory, JSONParserFactory, XMLParserFactory |
Implements the factory method to create a specific product |
Steps¶
- Create a Product interface
- Implement Concrete Products
- Create a Creator interface with a factory method
- Implement Concrete Creators that return specific products
classDiagram
class Parser {
<<interface>>
+parse(data)
}
class ParserFactory {
<<interface>>
+createParser()
}
class CSVParserFactory {
+createParser()
}
class JSONParserFactory {
+createParser()
}
class XMLParserFactory {
+createParser()
}
class CSVParser {
+parse(data)
}
class JSONParser {
+parse(data)
}
class XMLParser {
+parse(data)
}
CSVParserFactory ..|> ParserFactory
JSONParserFactory ..|> ParserFactory
XMLParserFactory ..|> ParserFactory
CSVParser ..|> Parser
JSONParser ..|> Parser
XMLParser ..|> Parser
CSVParserFactory ..> CSVParser : creates
JSONParserFactory ..> JSONParser : creates
XMLParserFactory ..> XMLParser : creates
The client interacts only with the factory interface (
ParserFactory) and product interface (Parser), while concrete factories (CSVParserFactory) decide which concrete product (CSVParser) to instantiate.
Example: Data Parsers¶
Each concrete factory decides which specific Parser implementation to create,
while the client code remains decoupled from the concrete classes.
Product Interface¶
| Parser.php | |
|---|---|
Creator Interface¶
| ParserFactory.php | |
|---|---|