티스토리 뷰

#include <iostream>

#include <stdio.h>


class cComputer // abstract class (추상클래스)

{

public:

enum{

NOTEBOOK = 1,

DESCTOP

};


virtual void getCategory() = 0;

};



class cNotebookComputer : public cComputer

{

public:

void getCategory()

{

printf("Notebook Compuer\n");

}

};



class cDesctopComputer : public cComputer

{

public:

void getCategory()

{

printf("Desctop Computer\n");

}

};



class cComputerManager                                                    // Factory Method

{

public:

static cComputer* getInstance( int _category )

{

cComputer *computer = NULL;


if( _category == computer->NOTEBOOK )

computer = new cNotebookComputer();

else if ( _category == computer->DESCTOP )

computer = new cDesctopComputer();


return computer;

}

};


void main()

{

cComputer *computer;

int input = NULL;


printf("1. Notebook Computer\n2. Desctop Compuer\n");

scanf("%d", &input);


/*****************************

Template Method Pattern

*****************************/

if( input == 1 )

computer = new cNotebookComputer();

else if( input == 2 )

computer = new cDesctopComputer();


/*****************************

Factory Method Pattern

=> Factory Method : 객체를 생성하는 역할을 하는 메소드

이제부터 이 메소드를 호출하는 것으로 객체를 생성할 수 있으며, 만일 추가적인 객체가 발생한다면

해당 메소드를 수정하는 것으로 작업이 끝남. (ex)TabletComputer의 추가

*****************************/

computer = cComputerManager::getInstance(input);


computer->getCategory();


delete computer;

}

'Programming > Design Pattern' 카테고리의 다른 글

Proxy Pattern  (0) 2015.02.11
Singleton pattern  (0) 2015.01.25
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/05   »
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 31
글 보관함