Here's a detailed example of the Abstract Factory Design Pattern in PHP 8.1 with explanations for each class, method, and variable. The code is divided into multiple files, each representing a class, and an index.php
file to demonstrate the usage of the pattern.
Step-by-Step Class Creation Order
- Product Interfaces
AbstractProductA.php
AbstractProductB.php
- Concrete Products
ProductA1.php
ProductA2.php
ProductB1.php
ProductB2.php
- Abstract Factory Interface
AbstractFactory.php
- Concrete Factories
ConcreteFactory1.php
ConcreteFactory2.php
- Client
Client.php
- Index File
index.php
Product Interfaces
AbstractProductA.php
<?php interface AbstractProductA { public function usefulFunctionA(): string; } ?>
AbstractProductA: Defines an interface for a type of product object.
AbstractProductB.php
<?php interface AbstractProductB { public function usefulFunctionB(): string; public function anotherUsefulFunctionB(AbstractProductA $collaborator): string; } ?>
AbstractProductB: Defines an interface for another type of product object and a method that collaborates with AbstractProductA
.
Concrete Products
ProductA1.php
<?php require_once 'AbstractProductA.php'; class ProductA1 implements AbstractProductA { public function usefulFunctionA(): string { return "The result of the product A1."; } } ?>
ProductA1: Concrete implementation of AbstractProductA
.
ProductA2.php
<?php require_once 'AbstractProductA.php'; class ProductA2 implements AbstractProductA { public function usefulFunctionA(): string { return "The result of the product A2."; } } ?>
ProductA2: Another concrete implementation of AbstractProductA
.
ProductB1.php
<?php require_once 'AbstractProductB.php'; require_once 'AbstractProductA.php'; class ProductB1 implements AbstractProductB { public function usefulFunctionB(): string { return "The result of the product B1."; } public function anotherUsefulFunctionB(AbstractProductA $collaborator): string { $result = $collaborator->usefulFunctionA(); return "The result of the B1 collaborating with ({$result})"; } } ?>
ProductB1: Concrete implementation of AbstractProductB
.
ProductB2.php
<?php require_once 'AbstractProductB.php'; require_once 'AbstractProductA.php'; class ProductB2 implements AbstractProductB { public function usefulFunctionB(): string { return "The result of the product B2."; } public function anotherUsefulFunctionB(AbstractProductA $collaborator): string { $result = $collaborator->usefulFunctionA(); return "The result of the B2 collaborating with ({$result})"; } } ?>
ProductB2: Another concrete implementation of AbstractProductB
.
Abstract Factory Interface
AbstractFactory.php
<?php interface AbstractFactory { public function createProductA(): AbstractProductA; public function createProductB(): AbstractProductB; } ?>
AbstractFactory: Defines an interface for creating abstract product objects.
Concrete Factories
ConcreteFactory1.php
<?php require_once 'AbstractFactory.php'; require_once 'ProductA1.php'; require_once 'ProductB1.php'; class ConcreteFactory1 implements AbstractFactory { public function createProductA(): AbstractProductA { return new ProductA1(); } public function createProductB(): AbstractProductB { return new ProductB1(); } } ?>
ConcreteFactory1: Concrete implementation of AbstractFactory
to create ProductA1
and ProductB1
.
ConcreteFactory2.php
<?php require_once 'AbstractFactory.php'; require_once 'ProductA2.php'; require_once 'ProductB2.php'; class ConcreteFactory2 implements AbstractFactory { public function createProductA(): AbstractProductA { return new ProductA2(); } public function createProductB(): AbstractProductB { return new ProductB2(); } } ?>
ConcreteFactory2: Concrete implementation of AbstractFactory
to create ProductA2
and ProductB2
.
Client
Client.php
<?php require_once 'AbstractFactory.php'; class Client { private AbstractProductA $productA; private AbstractProductB $productB; public function __construct(AbstractFactory $factory) { $this->productA = $factory->createProductA(); $this->productB = $factory->createProductB(); } public function run(): void { echo $this->productB->usefulFunctionB() . "\n"; echo $this->productB->anotherUsefulFunctionB($this->productA) . "\n"; } } ?>
Client: Uses the abstract factory to create and use the product objects. Demonstrates interaction between products.
Index File
index.php
<?php require_once 'ConcreteFactory1.php'; require_once 'ConcreteFactory2.php'; require_once 'Client.php'; function main() { echo "Testing with ConcreteFactory1:\n"; $factory1 = new ConcreteFactory1(); $client1 = new Client($factory1); $client1->run(); echo "\nTesting with ConcreteFactory2:\n"; $factory2 = new ConcreteFactory2(); $client2 = new Client($factory2); $client2->run(); } main(); ?>
index.php: Demonstrates the usage of the Abstract Factory pattern by creating clients with different factories and running their methods.
What You Should See When Running the Code
Testing with ConcreteFactory1: The result of the product B1. The result of the B1 collaborating with (The result of the product A1.) Testing with ConcreteFactory2: The result of the product B2. The result of the B2 collaborating with (The result of the product A2.)
This output demonstrates that the Client
can work with any factory, creating different products and using their methods interchangeably.
No comments:
Post a Comment