Prototype_Pattern - 8BitsCoding/RobotMentor GitHub Wiki
์ ์
์์ฐ ๋น์ฉ์ด ๋์ ์ธ์คํด์ค๋ฅผ ๋ณต์ฌ๋ฅผ ํตํด์ ์ฝ๊ฒ ์์ฑ ํ ์ ์๋๋ก ํ๋ ํจํด
์์ฐ ๋น์ฉ์ด ๋์ ์ธ์คํด์ค? :
- ์ข ๋ฅ๊ฐ ๋๋ฌด ๋ง์์ ํด๋์ค๋ก ์ ๋ฆฌ๋์ง ์๋ ๊ฒฝ์ฐ
- ํด๋์ค๋ก๋ถํฐ ์ธ์คํด์ค ์์ฑ์ด ์ด๋ ค์ด ๊ฒฝ์ฐ
์ญ์ ๋๋ฌด ์ด๋ ต๊ตฐ .. ์ฝ๋๋ก ๋ณด์
์์๋ก ์ค๋ช
์๊ตฌ์ฌํญ :
์ผ๋ฌ์คํธ๋ ์ดํฐ์ ๊ฐ์ ๊ทธ๋ฆผ ๊ทธ๋ฆฌ๊ธฐ ํด์ ๊ฐ๋ฐ ์ค ์ ๋๋ค. ์ด๋ค ๋ชจ์(shape)๋ฅผ ๊ทธ๋ฆด ์ ์๋๋ก ํ๊ณ ๋ณต์ฌ ๋ถ์ฌ๋ฃ๊ธฐ ๊ธฐ๋ฅ์ ๊ตฌํํด์ฃผ์ธ์.
class Shape {
private:
string id;
public:
void setId(string id) {
this->id = id;
}
string getId() {
return id;
}
virtual Shape* clone();
};
class Circle : public Shape {
private:
int x, y, r;
public:
Circle(int x, int y, int r) {
this->x = x;
this->y = y;
this->r = r;
}
int getX() {
return x;
}
int getY() {
return y;
}
int getR() {
return r;
}
void setX(int x) {
this->x = x;
}
void setY(int y) {
this->y = y;
}
void setR(int r) {
this->r = r;
}
Circle clone(){
return new Circle(x, y, z);
}
};
int main() {
Circle * circle1 = new Circle();
// ๋ค์๊ณผ ๊ฐ์ ๋ฐฉ๋ฒ์ผ๋ก ๊ฐ๋จํ๊ฒ ์ค๋ธ์ ํธ ๋ณต์ฌ๊ฐ ๊ฐ๋ฅ
Circle * circle2 = circle->clone();
}