This is a C# implementation of the Builder design pattern, based on the Gang of Four's structure. Each class is placed in a separate file, and all code lines are commented.
🔧 Product.cs
public class Product
{
private List<string> _parts = new List<string>(); // List to store product parts
public void Add(string part)
{
_parts.Add(part);
}
public void Show()
{
Console.WriteLine("Product Parts:");
foreach (string part in _parts)
{
Console.WriteLine("- " + part);
}
}
}
🔧 Builder.cs
public abstract class Builder
{
public abstract void BuildPartA(); // Step to build Part A
public abstract void BuildPartB(); // Step to build Part B
public abstract Product GetResult(); // Returns the final product
}
🔧 ConcreteBuilder.cs
public class ConcreteBuilder : Builder
{
private Product _product = new Product(); // The product instance being built
public override void BuildPartA()
{
_product.Add("PartA"); // Adds PartA to the product
}
public override void BuildPartB()
{
_product.Add("PartB"); // Adds PartB to the product
}
public override Product GetResult()
{
return _product; // Returns the fully built product
}
}
🔧 Director.cs
public class Director
{
public void Construct(Builder builder)
{
builder.BuildPartA(); // Instruct builder to build Part A
builder.BuildPartB(); // Instruct builder to build Part B
}
}
🔧 Program.cs
class Program
{
static void Main(string[] args)
{
Director director = new Director(); // Initializes the director
Builder builder = new ConcreteBuilder(); // Initializes a concrete builder
director.Construct(builder); // Directs the builder to build the product
Product product = builder.GetResult(); // Retrieves the final product
product.Show(); // Displays the product parts
}
}
Below is a C++ implementation of the Builder design pattern, a Creational pattern, following the Gang of Four (GoF) structure. Each class is placed in its own .h and .cpp files, with the order of creation designed to avoid dependency issues.
✅ Class Creation Order (to avoid dependency errors)
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.