Saturday, April 26, 2025

Builder Design Pattern in C#

Builder Design Pattern in C#

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
    }
}

💡 Console Output

Product Parts:
- PartA
- PartB

Saturday, April 19, 2025

Builder Pattern in C++

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)

  1. Product (The complex object being built)
  2. Builder (Abstract builder interface)
  3. ConcreteBuilder (Implements construction steps)
  4. Director (Orchestrates the building process)
  5. Client Code (main.cpp)

🔧 1. Product.h

#ifndef PRODUCT_H
#define PRODUCT_H

#include <string>
#include <vector>

class Product {
private:
    std::vector<std::string> parts;

public:
    void addPart(const std::string& part);
    void show() const;
};

#endif // PRODUCT_H

🔧 Product.cpp

#include "Product.h"
#include <iostream>

void Product::addPart(const std::string& part) {
    parts.push_back(part);
}

void Product::show() const {
    std::cout << "Product parts: ";
    for (const auto& part : parts) {
        std::cout << part << " ";
    }
    std::cout << std::endl;
}

🧱 2. Builder.h

#ifndef BUILDER_H
#define BUILDER_H

#include "Product.h"

// Abstract Builder
class Builder {
public:
    virtual void buildPartA() = 0;
    virtual void buildPartB() = 0;
    virtual Product* getResult() = 0;
    virtual ~Builder() {}
};

#endif // BUILDER_H

🧱 3. ConcreteBuilder.h

#ifndef CONCRETEBUILDER_H
#define CONCRETEBUILDER_H

#include "Builder.h"

// Concrete Builder
class ConcreteBuilder : public Builder {
private:
    Product* product;

public:
    ConcreteBuilder();
    ~ConcreteBuilder();

    void buildPartA() override;
    void buildPartB() override;
    Product* getResult() override;
};

#endif // CONCRETEBUILDER_H

🧱 ConcreteBuilder.cpp

#include "ConcreteBuilder.h"

ConcreteBuilder::ConcreteBuilder() {
    product = new Product();
}

ConcreteBuilder::~ConcreteBuilder() {
    delete product;
}

void ConcreteBuilder::buildPartA() {
    product->addPart("PartA");
}

void ConcreteBuilder::buildPartB() {
    product->addPart("PartB");
}

Product* ConcreteBuilder::getResult() {
    return product;
}

🎯 4. Director.h

#ifndef DIRECTOR_H
#define DIRECTOR_H

#include "Builder.h"

// Director
class Director {
public:
    void construct(Builder* builder);
};

#endif // DIRECTOR_H

🎯 Director.cpp

#include "Director.h"

void Director::construct(Builder* builder) {
    builder->buildPartA();
    builder->buildPartB();
}

👨‍💻 5. main.cpp

#include <iostream>
#include "Director.h"
#include "ConcreteBuilder.h"

int main() {
    Director director;
    ConcreteBuilder builder;

    director.construct(&builder);
    Product* product = builder.getResult();

    product->show();

    return 0;
}

🧠 Summary & Explanation

Class/FileRole
ProductThe complex object that is built step by step.
BuilderAbstract interface that defines methods for building the parts.
ConcreteBuilderImplements the building steps and returns the final product.
DirectorControls the order of building steps, using a Builder.
main.cppThe client code that puts it all together.

✅ Benefits of the Builder Pattern in C++

  • Encapsulates complex construction logic.
  • Supports multiple representations of a product.
  • Promotes separation of concerns (product vs. construction steps).

Thursday, April 17, 2025

Abstract Factory Design Pattern in PHP 8.1

Abstract Factory Design Pattern in PHP

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

  1. Product Interfaces
    • AbstractProductA.php
    • AbstractProductB.php
  2. Concrete Products
    • ProductA1.php
    • ProductA2.php
    • ProductB1.php
    • ProductB2.php
  3. Abstract Factory Interface
    • AbstractFactory.php
  4. Concrete Factories
    • ConcreteFactory1.php
    • ConcreteFactory2.php
  5. Client
    • Client.php
  6. 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.

Builder Design Pattern in C#

Builder Design Pattern in C# This is a C# implementation of the Builder design pattern, based on the Gang of Four's structure. Each cl...