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.
🚀 **Master the Abstract Factory Design Pattern in Java!** 🎥
Abstract Factory Pattern in Java
Pattern Category: Creational Pattern Name: Abstract Factory Programming Language: Java Target Audience: First-year college students
Purpose
The Abstract Factory pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes.
Class/File Creation Order (to avoid dependency errors):
AbstractProductA.java
AbstractProductB.java
ProductA1.java
ProductA2.java
ProductB1.java
ProductB2.java
AbstractFactory.java
ConcreteFactory1.java
ConcreteFactory2.java
Client.java
Main.java
Class Descriptions and Code
1. AbstractProductA.java
// Abstract product A declares an interface for a type of product object
public interface AbstractProductA {
void interact();
}
Interface: AbstractProductA — base for all ProductA types.
Method: interact() — interaction behavior; defined later in concrete classes.
2. AbstractProductB.java
// Abstract product B declares an interface for a type of product object
public interface AbstractProductB {
void interactWith(AbstractProductA a);
}
Interface: AbstractProductB — base for all ProductB types.
Method: interactWith(AbstractProductA a) — defines dependency on ProductA.
3. ProductA1.java
public class ProductA1 implements AbstractProductA {
@Override
public void interact() {
System.out.println("ProductA1 interacting");
}
}
Concrete Product: Implements AbstractProductA.
Method: Implements interaction logic for variant A1.
4. ProductA2.java
public class ProductA2 implements AbstractProductA {
@Override
public void interact() {
System.out.println("ProductA2 interacting");
}
}
Concrete Product: Implements AbstractProductA.
Method: Implements interaction logic for variant A2.
5. ProductB1.java
public class ProductB1 implements AbstractProductB {
@Override
public void interactWith(AbstractProductA a) {
System.out.print("ProductB1 interacting with ");
a.interact();
}
}
public class ProductB2 implements AbstractProductB {
@Override
public void interactWith(AbstractProductA a) {
System.out.print("ProductB2 interacting with ");
a.interact();
}
}
Concrete Product: Implements AbstractProductB.
Method: Similar to ProductB1 but represents a different product variant.
7. AbstractFactory.java
// Abstract Factory declares an interface for creating abstract products A and B
public interface AbstractFactory {
AbstractProductA createProductA();
AbstractProductB createProductB();
}
Interface: Factory for creating families of products A and B.
Methods:
createProductA() — produces AbstractProductA
createProductB() — produces AbstractProductB
8. ConcreteFactory1.java
public class ConcreteFactory1 implements AbstractFactory {
@Override
public AbstractProductA createProductA() {
return new ProductA1();
}
@Override
public AbstractProductB createProductB() {
return new ProductB1();
}
}
Concrete Factory: Implements creation of ProductA1 and ProductB1.
9. ConcreteFactory2.java
public class ConcreteFactory2 implements AbstractFactory {
@Override
public AbstractProductA createProductA() {
return new ProductA2();
}
@Override
public AbstractProductB createProductB() {
return new ProductB2();
}
}
Concrete Factory: Implements creation of ProductA2 and ProductB2.
10. Client.java
// Client uses only interfaces declared by AbstractFactory and products
public class Client {
private AbstractProductA productA;
private AbstractProductB productB;
public Client(AbstractFactory factory) {
// The factory creates the concrete products
productA = factory.createProductA();
productB = factory.createProductB();
}
public void run() {
productB.interactWith(productA);
}
}
Class: Depends only on abstract interfaces.
Constructor: Receives a factory to instantiate related products.
Method: run() demonstrates interaction between products.
11. Main.java
public class Main {
public static void main(String[] args) {
System.out.println("Using ConcreteFactory1:");
AbstractFactory factory1 = new ConcreteFactory1();
Client client1 = new Client(factory1);
client1.run();
System.out.println("\nUsing ConcreteFactory2:");
AbstractFactory factory2 = new ConcreteFactory2();
Client client2 = new Client(factory2);
client2.run();
}
}
Entry Point: Demonstrates how the Abstract Factory is used at runtime.
Switching Factories: Shows that client code remains unchanged when using different product families.
Summary
The Abstract Factory pattern allows you to create families of related objects without binding your code to their concrete classes. This example uses the exact naming and structure from the Gang of Four book, making it ideal for educational purposes.
Below is a C# implementation of the Abstract Factory design pattern, a Creational pattern, following the Gang of Four (GoF) structure. Each class is placed in its own file and created in an order that avoids dependency issues.
Class Creation Order
To avoid dependency issues, create the classes in this order:
namespace AbstractFactoryPattern
{
// Abstract Product A
public interface IAbstractProductA
{
void Use();
}
}
IAbstractProductB.cs
namespace AbstractFactoryPattern
{
// Abstract Product B
public interface IAbstractProductB
{
void Use();
}
}
Explanation
IAbstractProductA and IAbstractProductB define the interfaces for two types of products.
Each product type has a Use() method, which will be implemented by concrete products.
2. Concrete Products
ConcreteProductA1.cs
using System;
namespace AbstractFactoryPattern
{
// Concrete Product A1
public class ConcreteProductA1 : IAbstractProductA
{
public void Use()
{
Console.WriteLine("Using ConcreteProductA1");
}
}
}
ConcreteProductA2.cs
using System;
namespace AbstractFactoryPattern
{
// Concrete Product A2
public class ConcreteProductA2 : IAbstractProductA
{
public void Use()
{
Console.WriteLine("Using ConcreteProductA2");
}
}
}
ConcreteProductB1.cs
using System;
namespace AbstractFactoryPattern
{
// Concrete Product B1
public class ConcreteProductB1 : IAbstractProductB
{
public void Use()
{
Console.WriteLine("Using ConcreteProductB1");
}
}
}
ConcreteProductB2.cs
using System;
namespace AbstractFactoryPattern
{
// Concrete Product B2
public class ConcreteProductB2 : IAbstractProductB
{
public void Use()
{
Console.WriteLine("Using ConcreteProductB2");
}
}
}
Explanation
ConcreteProductA1, ConcreteProductA2 implement IAbstractProductA. ConcreteProductB1, ConcreteProductB2 implement IAbstractProductB.
These represent different product variations.
Explanation:
Defines CreateProductA() and CreateProductB(), which will be implemented by concrete factories.
4. Concrete Factories
ConcreteFactory1.cs
namespace AbstractFactoryPattern
{
// Concrete Factory 1
public class ConcreteFactory1 : IAbstractFactory
{
public IAbstractProductA CreateProductA()
{
return new ConcreteProductA1();
}
public IAbstractProductB CreateProductB()
{
return new ConcreteProductB1();
}
}
}
ConcreteFactory2.cs
namespace AbstractFactoryPattern
{
// Concrete Factory 2
public class ConcreteFactory2 : IAbstractFactory
{
public IAbstractProductA CreateProductA()
{
return new ConcreteProductA2();
}
public IAbstractProductB CreateProductB()
{
return new ConcreteProductB2();
}
}
}
Explanation: ConcreteFactory1 creates ConcreteProductA1 and ConcreteProductB1. ConcreteFactory2 creates ConcreteProductA2 and ConcreteProductB2.
5. Client Code (Program.cs)
using System;
namespace AbstractFactoryPattern
{
class Program
{
static void Main(string[] args)
{
// Create a factory of type 1
IAbstractFactory factory1 = new ConcreteFactory1();
IAbstractProductA productA1 = factory1.CreateProductA();
IAbstractProductB productB1 = factory1.CreateProductB();
productA1.Use(); // Output: Using ConcreteProductA1
productB1.Use(); // Output: Using ConcreteProductB1
// Create a factory of type 2
IAbstractFactory factory2 = new ConcreteFactory2();
IAbstractProductA productA2 = factory2.CreateProductA();
IAbstractProductB productB2 = factory2.CreateProductB();
productA2.Use(); // Output: Using ConcreteProductA2
productB2.Use(); // Output: Using ConcreteProductB2
}
}
}
Final Explanation
Abstract Products (IAbstractProductA, IAbstractProductB): Define interfaces for different types of products.
Concrete Products: Implement the abstract product interfaces.
Abstract Factory: Declares factory methods for creating products.
Concrete Factories: Implement factory methods to produce different products.
Client Code: Uses the abstract factory to create product families.
Key Takeaways
✅ Encapsulation: The client is independent of concrete product classes.
✅ Scalability: Easily add new product families without modifying existing code.
✅ Consistency: Ensures related products are created together.
Would you like a downloadable version of this file or to have it combined with the C++/PHP HTML examples into a single learning package?
Tuesday, March 11, 2025
Below is a C++ implementation of the Abstract Factory design pattern, which is a Creational pattern, following the Gang of Four (GoF) structure. Each class is placed in its own file and created in an order that avoids dependency issues.
Class Creation Order
To avoid dependency errors, create the classes in this order:
AbstractProductA and AbstractProductB define the interfaces for the two types of products. Each abstract product contains a use() method, which will be implemented by concrete products.
Explanation: ConcreteFactory1 creates ConcreteProductA1 and ConcreteProductB1. ConcreteFactory2 creates ConcreteProductA2 and ConcreteProductB2.
5. main.cpp (Client Code)
#include <iostream>
#include "ConcreteFactory1.h"
#include "ConcreteFactory2.h"
int main() {
// Create a factory of type 1
AbstractFactory* factory1 = new ConcreteFactory1();
AbstractProductA* productA1 = factory1->createProductA();
AbstractProductB* productB1 = factory1->createProductB();
productA1->use(); // Output: Using ConcreteProductA1
productB1->use(); // Output: Using ConcreteProductB1
// Create a factory of type 2
AbstractFactory* factory2 = new ConcreteFactory2();
AbstractProductA* productA2 = factory2->createProductA();
AbstractProductB* productB2 = factory2->createProductB();
productA2->use(); // Output: Using ConcreteProductA2
productB2->use(); // Output: Using ConcreteProductB2
// Cleanup
delete productA1;
delete productB1;
delete factory1;
delete productA2;
delete productB2;
delete factory2;
return 0;
}
Final Explanation
Abstract Products (AbstractProductA & AbstractProductB): Define interfaces for different types of products.
Concrete Products: Implement the abstract products.
Abstract Factory: Declares factory methods for creating products.
Concrete Factories: Implement factory methods to produce different products.
Client Code: Uses the abstract factory to create product families.
Key Takeaways
✅ Encapsulation: The client is independent of concrete product classes.
✅ Scalability: Easily add new product families without modifying existing code.
✅ Consistency: Ensures related products are created together.
Monday, March 03, 2025
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 and ConcreteProductB.php) – Implement the product interface.
Creator (Factory) Interface (Creator.php) – Declares the factory method.
Concrete Factories (ConcreteCreatorA.php and ConcreteCreatorB.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).