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

//abstract product A
interface IProductA {
    testA(): void
}

//abstract product B
interface IProductB {
    testB(): void
}

//concrete product A1
class ProductA1 implements IProductA {
    testA() {
        console.log("test A1")
    }
}

//concrete product A2
class ProductA2 implements IProductA {
    testA() {
        console.log("test A2")
    }
}

//concrete product B1
class ProductB1 implements IProductB {
    testB() {
        console.log("test B1")
    }
}

//concrete product B2
class ProductB2 implements IProductB {
    testB() {
        console.log("test B2")
    }
}

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

//concrete factory 1
class Factory1 implements IFactory {
    createA() {
        return new ProductA1()
    }
    createB() {
        return new ProductB1()
    }
}

//concrete factory 2
class Factory2 implements IFactory {
    createA() {
        return new ProductA2()
    }
    createB() {
        return new ProductB2()
    }
}

//client code
function testFactory(factory: IFactory) {
    let productA = factory.createA()
    let productB = factory.createB()
    productA.testA()
    productB.testB()
}

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