ProtoType 패턴
정의
|
상황
|
코드
#include <iostream> class Character { public: Character() {} Character(int hp, int mp):HP(hp), MP(mp), XPos(100), YPos(100) { } virtual Character* Clone() = 0; void SetPos(int xpos, int ypos) { XPos = xpos; YPos = ypos; } void SetState(float percent) { HP *= percent; MP *= percent; } protected: int HP, MP; int XPos, YPos; }; class NenMaster :public Character { public: NenMaster(int hp, int mp) :Character(hp,mp) { bunsinLev = 1; } NenMaster(NenMaster& rhs) { *this = rhs; } Character* Clone() { return new NenMaster(*this); } Character* Bunsinsul() { Character** bunsin = new Character*[bunsinLev]; for(int i = 0 ; i < bunsinLev ; i++) { bunsin[i] = this->Clone(); bunsin[i]->SetState(0.1 * bunsinLev); //분신 위치 조정 코드 } std::cout << "분신 생성" << std::endl; return *bunsin; } private: int bunsinLev; }; int main() { NenMaster* nm = new NenMaster(100,100); Character* bunsin = NULL; bunsin = nm->Bunsinsul(); delete nm; delete[] bunsin; return 0; } |
장점
|
단점
|
---------------------------------------------------------------------------------------------------------------------------------------------------
뭔가 또 예시를 잘못들었다.
적절한 예시라고 생각했는데, 막상 코드로 짜면 패턴의 용도를 소개에서 벗어나버리게 되는 느낌이란 말이야..ㅠㅠ....
Command 패턴 (1) (0) | 2018.01.17 |
---|---|
Singleton 패턴 (0) | 2016.10.22 |
Abstract Factory 패턴 (0) | 2016.10.15 |
Cookie, Session 그대의 의미는.. (0) | 2015.10.29 |
URL과 URI 차이점 (0) | 2015.10.27 |
댓글,
Lowpoly
게임 서버 프로그래머 지망생