Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- 싱글톤
- 디자인패턴
- 브릿지
- 팩터리 패턴
- 함수형 팩터리
- 싱글톤 패턴
- 프로토타입 중복처리
- 팩터리
- 흐름식 빌더
- 디자인 패턴
- 컴포지트 빌더
- 모던C++디자인패턴
- 내부 팩터리
- 팩터리 메서드
- 그루비 스타일 빌더
- 추상 팩터리
- 데커레이터 패턴
- 프로토타입
- 프로토타입 패턴
- 싱글턴 패턴
- 컴포지트 패턴
- 데커레이터
- 빌더
- 단순한 빌더
- 컴포지트
- 빌더 패턴
- 동적 데커레이터
- 싱글턴
- 브릿지 패턴
- 동적 데코레이터
Archives
- Today
- Total
GGym's Practice Notes
5장. 프로토타입 패턴 본문
책에선 클래스, 스폰 함수, 템플릿을 사용하여 프로토타입 패턴을 작성했다.
막상 작성하고 나니 프로토타입 패턴이라기 보단 Spawner를 만드는 것 같았지만 뒤에 코드없이 설명만 해둬서 그냥 내 입맛에 맞게 수정하였다.
수정할때 이전에 C++디자인패턴에서 배웠던 프로토타입 팩토리를 추가해서 프로토타입을 만들도록 하였다.
생성하는 과정이 복잡한데, 나중에 빌더 패턴하고 합쳐서 예쁘게 사용하면 좋을 듯 싶다.
- 클래스 기반 프로토타입 생성
더보기
class Spawner {
public:
virtual ~Spawner(){}
Spawner(Monster* prototype) : prototype_(prototype){}
Monster* spawnMonster(){
return prototype_->clone();
};
private:
Monster* prototype_;
};
Monster* GhostPrototype = new Ghost(50, 1);
Spawner* GhostSpawner = new Spawner(GhostPrototype);
- 스폰 함수로 생성
더보기
Monster* spawnGhost(){
return new Ghost(50, 1);
}
typedef Monster* (*SpawnCallback)();
class Spawner {
public:
Spawner (SpawnCallback spawn) : spawn_(spawn){}
Monster* spawnMonster(){
return spawn_();
}
private:
SpawnCallback spawn_;
};
Spawner* ghostSpawner = new Spawner(spawnGhost);
- 템플릿으로 생성
더보기
class Spawner{
public:
virtual ~Spawner(){}
virtual Monster* spawnMonster() = 0;
};
template <typename T>
class SpawnFor : public Spawner {
public:
virtual Monster* spawnMonster() {
return new T();
}
};
Spawner* ghostSpawner = new SpawnFor<Ghost>();
- 만든 전체 코드
#include<iostream>
#include<vector>
using namespace std;
class GameObject{
public:
GameObject(){}
};
class Monster {
public:
virtual ~Monster() {
delete gameObject_;
}
Monster(int health, int speed, GameObject* gameObject)
: health_(health), speed_(speed), gameObject_(gameObject){
cout << " 몬스터 생성" << endl;
}
protected:
int health_;
int speed_;
GameObject* gameObject_;
};
class Ghost : public Monster {
public:
Ghost(Ghost& ghost)
: Monster(ghost.health_, ghost.speed_, ghost.gameObject_){
}
Ghost(int health, int speed, GameObject* gameObject)
: Monster(health, speed, gameObject){
}
};
class Demon : public Monster {};
class Sorcerer : public Monster {};
/////////////////////////////////////////////////////////////////
class Spawner{
public:
virtual ~Spawner(){}
virtual Monster* spawnMonster() = 0;
};
template<typename T>
class SpawnFor : public Spawner {
public:
T* prototype_;
~SpawnFor(){
delete prototype_;
}
SpawnFor(T* prototype) : prototype_(prototype){}
virtual T* spawnMonster() {
return newMonster(*prototype_);
}
private:
T* newMonster(T& proto){
T* monster = new T(proto);
//
return monster;
}
};
int main(){
Ghost* originalGhost = new Ghost(50, 1, new GameObject());
SpawnFor<Ghost>* ghostSpawner = new SpawnFor<Ghost>(originalGhost);
vector<Monster*> monsters;
for(int i=0; i<3; i++)
monsters.push_back(ghostSpawner->spawnMonster());
for(auto i : monsters){
delete i;
}
return 0;
}
'Design Pattern > 게임 디자인패턴' 카테고리의 다른 글
2장. 명령 패턴 (0) | 2020.05.07 |
---|---|
6장. 싱글턴 패턴 (0) | 2020.05.07 |
4장. 관찰자 패턴 (0) | 2020.04.17 |
11장. 바이트코드 패턴 (0) | 2020.03.25 |
11장. 바이트코드(인터프리터 패턴) (0) | 2020.03.25 |