Factory Design Pattern in PHP
Here's the detailed breakdown and implementation of the Factory Design Pattern in PHP following the Gang of Four's approach.
Order of Class Creation
- Product Interface (
Product.php
) – Defines the interface that all concrete products must implement. - Concrete Products (
ConcreteProductA.php
andConcreteProductB.php
) – Implement the product interface. - Creator (Factory) Interface (
Creator.php
) – Declares the factory method. - Concrete Factories (
ConcreteCreatorA.php
andConcreteCreatorB.php
) – Implement the factory method to create specific product instances. - Client Code (
index.php
) – Uses the factory to create objects.
Factory Design Pattern in PHP
I'll generate the code in individual files, following the correct sequence.
1. Product Interface (Product.php
)
<?php
// Product.php
// The Product interface declares the operations that all concrete products must implement.
interface Product {
public function operation(): string;
}
?>
Explanation:
This interface ensures that all products created by the factory implement a common method operation()
.
2. Concrete Products (ConcreteProductA.php
and ConcreteProductB.php
)
Concrete Product A (ConcreteProductA.php
)
<?php
// ConcreteProductA.php
// Implements the Product interface.
require_once "Product.php";
class ConcreteProductA implements Product {
public function operation(): string {
return "Result from ConcreteProductA";
}
}
?>
Concrete Product B (ConcreteProductB.php
)
<?php
// ConcreteProductB.php
// Implements the Product interface.
require_once "Product.php";
class ConcreteProductB implements Product {
public function operation(): string {
return "Result from ConcreteProductB";
}
}
?>
Explanation:
Both ConcreteProductA
and ConcreteProductB
implement the Product
interface.
Each class defines the operation()
method, returning a string to identify itself.
3. Factory Interface (Creator.php
)
<?php
// Creator.php
// The Creator class declares the factory method.
interface Creator {
public function factoryMethod(): Product;
}
?>
Explanation:
This interface defines a method factoryMethod()
that must be implemented by all concrete factories to produce a Product
.
4. Concrete Factories (ConcreteCreatorA.php
and ConcreteCreatorB.php
)
Concrete Factory A (ConcreteCreatorA.php
)
<?php
// ConcreteCreatorA.php
// Implements the Creator interface and produces ConcreteProductA.
require_once "Creator.php";
require_once "ConcreteProductA.php";
class ConcreteCreatorA implements Creator {
public function factoryMethod(): Product {
return new ConcreteProductA();
}
}
?>
Concrete Factory B (ConcreteCreatorB.php
)
<?php
// ConcreteCreatorB.php
// Implements the Creator interface and produces ConcreteProductB.
require_once "Creator.php";
require_once "ConcreteProductB.php";
class ConcreteCreatorB implements Creator {
public function factoryMethod(): Product {
return new ConcreteProductB();
}
}
?>
Explanation:
ConcreteCreatorA
and ConcreteCreatorB
each implement Creator
.
They override factoryMethod()
to return an instance of a specific Product
(ConcreteProductA
or ConcreteProductB
).
5. Client Code (index.php
)
<?php
// index.php
// The client code calls the factory method to create objects.
require_once "ConcreteCreatorA.php";
require_once "ConcreteCreatorB.php";
/**
* Function to test the factory pattern
*/
function clientCode(Creator $creator) {
echo "Client: I'm working with " . get_class($creator) . "<br>";
$product = $creator->factoryMethod();
echo "Product says: " . $product->operation() . "<br><br>";
}
// Testing both factories
echo "<h2>Factory Method Pattern in PHP</h2>";
echo "<h3>Using ConcreteCreatorA:</h3>";
clientCode(new ConcreteCreatorA());
echo "<h3>Using ConcreteCreatorB:</h3>";
clientCode(new ConcreteCreatorB());
?>
Explanation:
The clientCode()
function expects an object that implements Creator
.
It calls the factoryMethod()
to create a product and prints the result.
The index.php
script tests both ConcreteCreatorA
and ConcreteCreatorB
, demonstrating the Factory Pattern in action.
Final Thoughts
This structured approach ensures:
- Clear separation of concerns (each class has a single responsibility).
- Encapsulation of object creation in the factory classes.
- Scalability (new products can be introduced without altering existing code).
No comments:
Post a Comment