Prototype_Pattern - 8BitsCoding/RobotMentor GitHub Wiki

์ •์˜

์ƒ์‚ฐ ๋น„์šฉ์ด ๋†’์€ ์ธ์Šคํ„ด์Šค๋ฅผ ๋ณต์‚ฌ๋ฅผ ํ†ตํ•ด์„œ ์‰ฝ๊ฒŒ ์ƒ์„ฑ ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ํŒจํ„ด

์ƒ์‚ฐ ๋น„์šฉ์ด ๋†’์€ ์ธ์Šคํ„ด์Šค? :

  1. ์ข…๋ฅ˜๊ฐ€ ๋„ˆ๋ฌด ๋งŽ์•„์„œ ํด๋ž˜์Šค๋กœ ์ •๋ฆฌ๋˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ
  2. ํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ์ด ์–ด๋ ค์šด ๊ฒฝ์šฐ

์—ญ์‹œ ๋„ˆ๋ฌด ์–ด๋ ต๊ตฐ .. ์ฝ”๋“œ๋กœ ๋ณด์ž


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

์š”๊ตฌ์‚ฌํ•ญ :

์ผ๋Ÿฌ์ŠคํŠธ๋ ˆ์ดํ„ฐ์™€ ๊ฐ™์€ ๊ทธ๋ฆผ ๊ทธ๋ฆฌ๊ธฐ ํˆด์„ ๊ฐœ๋ฐœ ์ค‘ ์ž…๋‹ˆ๋‹ค. ์–ด๋–ค ๋ชจ์–‘(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();
}

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