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

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

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

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

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

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

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

//client code
function testFactory(factory) {
    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