Abstract_Factory_Pattern - 8BitsCoding/RobotMentor GitHub Wiki

์ •์˜

๊ด€๋ จ ์žˆ๋Š” ๊ฐ์ฒด์˜ ์ƒ์„ฑ์„ ๊ฐ€์ƒํ™” ํ•  ์ˆ˜ ์žˆ๋‹ค.


์˜ˆ์‹œ๋กœ ์„ค๋ช…

int main() {
    BikeFactory factory = new GtBikeFactory();
    // BikeFactory factory = new SamBikeFactory();
    // ํ•˜๊ณ  ์‹ถ์€๊ฒŒ ์ด๋Ÿฐ ๊ฒƒ factory๋ฅผ ๋‘๊ณ  ์›ํ•˜๋Š” ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์„ ์–ธํ•ด๊ฐ€๋ฉฐ ์˜ค๋ธŒ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์‹ถ๋‹ค

    Body body = factory->createBody();
    Wheel wheel = factory->creatWheel();
}
class BikeFactory {
public:
    virtual Body createBody();
    virtual Wheel createWheel();
};

class Body {
public:
    // interface๋กœ ๊ตฌํ˜„
};

class Wheel {
public:
    // interface๋กœ ๊ตฌํ˜„
};
class SamFactory : public BikeFactory{
public:
    Body createBody() {
        return new SamBody();
    }
    Wheel createWheel() {
        return new SamWheel();
    }
};

class SamBody : public Body {
    // ๊ตฌํ˜„
};

class SamWheel : public Wheel {
    // ๊ตฌํ˜„
};

์–ด๋–ป๊ฒŒ ์“ฐ๋‚˜?

int main() {
    GuiFac fac = FactoryInstance.getGuiFac();
    // ์–ด๋–ค OS์—์„œ๋„ ์ฝ”๋“œ์ˆ˜์ •์—†์ด ์‚ฌ์šฉ๊ฐ€๋Šฅ.

    Button button = fac->creatButton();
    TextArea area = fac->createTextArea();

    button.click();
    // ๋“ฑ๋“ฑ...
}
class FactoryInstance {
public:
    GuiFac getGuiFac() {
        switch(OS.ver) {
        case 0:
            return new MacGuiFac();
        case 1:
            return new LinuxGuiFac();
        case 2:
            return new WindowsGuiFac();
        }
    }
};

class MacGuiFac : public GuiFac {
    // ๊ตฌํ˜„
};

class LinuxGuiFac : public GuiFac {
    // ๊ตฌํ˜„
};

class WindowsGuiFac : public GuiFac {
    // ๊ตฌํ˜„
};
class GuiFac {
public:
    virtual Button createButton();
    virtual TextArea createTextArea();
};

์ฐธ๊ณ ์‚ฌ์ดํŠธ