Decorator_Pattern - 8BitsCoding/RobotMentor GitHub Wiki
์ ์
๋์ ์ผ๋ก ์ฑ ์ ์ถ๊ฐ๊ฐ ํ์ํ ๋ ๋ฐ์ฝ๋ ์ดํฐ ํจํด์ ์ฌ์ฉํ ์ ์๋ค.
์์๋ก ์ค๋ช
์์ - ์ปคํผ ์ ์กฐ ๋ฐฉ๋ฒ
์ปคํผ์ ๊ฐ์ ์ธก์ ํ๊ณ ์ถ๋ค.
์์คํ๋ ์ : ์ปคํผ์ ๊ธฐ๋ณธ ์๋ฉ๋ฆฌ์นด๋ ธ : ์์คํ๋ ์ + ๋ฌผ ์นดํ๋ผ๋ผ : ์์คํ๋ ์ + ์คํ๋ฐํฌ ํค์ด์ฆ๋ : ์๋ฉ๋ฆฌ์นด๋ ธ + ํค์ด์ฆ๋ ์๋ฝ ์นดํ๋ชจ์นด : ์นดํ๋ผ๋ผ + ์ด์ฝ๋ฆฟ ์บฌ๋ผ๋ฉ ๋ง๋ผ์๋ : ์นดํ๋ผ๋ผ + ์นด๋ผ๋ฉ ์๋ฝ
void main() {
Scanner * sc = new Scanner(); // ์ฌ์ฉ์ input์ ๋ฐ์
IBeverage * beverage = new Base();
bool done = false;
while(!done) {
printf("ํ์ฌ ์๋ฃ์ ๊ฐ๊ฒฉ : %d", beverage->getTotalPrice());
printf("์ ํ : 1 ์ท ์ถ๊ฐ / 2 ์ฐ์ ์ถ๊ฐ");
switch(sc->input()) {
case 0:
done = true;
break;
case 1:
beverage = new Espresso(beverage);
break;
case 2:
beverage = new Milk(beverage);
break;
default:
break;
}
}
}
๋ค์๊ณผ ๊ฐ์ด ํ ์ค๋ธ์ ํธ์ ์ง์์ ์ผ๋ก ๋ฉ๋ด๋ฅผ ์ถ๊ฐํ๊ณ ์ถ๋ค.
class IBeverage {
public:
virtual int getTotalPrice();
};
class AbstAdding : public IBeverage {
private:
IBeverage* base;
public:
AbstAdding(IBeverage* _base) { this->base = _base; }
int getTotalPrice() {
return base->getTotalPrice();
}
protected:
IBeverage* getBase() {
return base;
}
};
class Espresso : public AbstAdding {
protected:
int espressoCount = 0;
public:
Espresso(IBeverage * base) : AbstAdding(base) {}
private:
int getAddPrice() {
espressoCount += 1;
int addPrice = 100;
if(espressoCount > 1) {
addPrice = 70;
}
return addPrice;
}
};
class Milk : public AbstAdding {
public:
Milk(IBeverage* material) : AbstAdding(material) {}
};