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();
};