Паттерны / Порождающие паттерны
image

<?php

//abstract factory
interface IFactory {
    function createA(): IProductA;
    function createB(): IProductB;
}

//concrete factory 1
class Factory1 implements IFactory {
    function createA(): IProductA {
        return new ProductA1();
    }
    function createB(): IProductB {
        return new ProductB1();
    }
}

//concrete factory 2
class Factory2 implements IFactory {
    function createA(): IProductA {
        return new ProductA2();
    }
    function createB(): IProductB {
        return new ProductB2();
    }
}

//abstract product A
interface IProductA {
    function testA();
}

//abstract product B
interface IProductB {
    function testB();
}

//concrete product A1
class ProductA1 implements IProductA {
    function testA() {
        echo "test A1""\n";
    }
}

//concrete product A2
class ProductA2 implements IProductA {
    function testA() {
        echo "test A2""\n";
    }
}

//concrete product B1
class ProductB1 implements IProductB {
    function testB() {
        echo "test B1""\n";
    }
}

//concrete product B2
class ProductB2 implements IProductB {
    function testB() {
        echo "test B2""\n";
    }
}

//client code
function testFactory(IFactory $factory) {
    $productA = $factory->createA();
    $productB = $factory->createB();
    $productA->testA();
    $productB->testB();
}

testFactory(new Factory1());
//printed: test A1
//         test B1
testFactory(new Factory2());
//printed: test A2
//         test B2