#include <iostream>

using namespace std;

 

// FlyWeight 디자인패턴: 작은객체를 공유하는 기법

//               동일한 상태를 가지는 객체는 메모리에 한번만 놓이게 한다.

//               Factory를 사용하게 된다. 또한 Factory는 주로 Sigletone이 된다.

// 객체를 만드는 클래스를 만들어 주자.(생성자를 private로 숨기는 대신)

 

class BigChar

{

       char c;

       BigChar( char a ) : c (a) { }

public:

       void Show() const { cout << "[[[[ " << c << " ]]]]" << endl; }

      

       friend class BigCharFactory;

};

 

#include <map>

 

// BigChar를 생성하는 공장을 만들자.(오직 한 개.싱글톤)

class BigCharFactory

{

       map<char, BigChar*> pool;

       BigCharFactory() {}

 

public:

       static BigCharFactory& GetInstance()

       {

             static BigCharFactory _instance;

             return _instance;

       }

 

       BigChar* CreateBigChar( char c )

       {

             if ( pool[c] == 0 )

                    pool[c] = new BigChar(c);

 

             return pool[c];

       }

 

       void Reset() // 또는 Clean()

       {

             // map에 있는 모든 객체를 제거한다.

       }

 

       void Remove( char c )

       {

       }

};

// Helper Macro 함수

BigChar* CreateChar( char c )

{

       return BigCharFactory::GetInstance().CreateBigChar(c);

}

 

void foo()

{

       BigChar*  p = CreateChar('c');

 

       cout << p << endl;

}

 

void main()

{

       BigChar* p = CreateChar('c');

       cout << p << endl;

 

       foo();

}

Tag | ,

8.9(목) C++ - 예외이야기

from Study/C++ 2007/08/12 21:19 view 25522

#include <iostream>

using namespace std;

 

// 예외이야기- 진짜 예외는 객체를(클래스) 만들어서 계층화 해야 한다.

// 예상치 않은 에러가 난다면 설계가 잘못된 것.(사용자가 지정하지 않은 에러)

// 롤백- 예외 발생시 다시 안전된 상태로 돌려놔야 한다.

 

class Exception {};

class StackException : public Exception {};

class StackUnderflowException : public StackException {};

 

// 예외 안전성의 개념

// 1. 기본보장: 예외 처리후 메모리등의 리소스낭비는 없지만 프로그램의 상태를 알
                        수는 없다
.

// 2. 강력보장: 예외를 처리한 후 프로그램의 상태가 예외발생 이전으로 복구된다.

// 3. 완전보장: 예외가없다. int a = 10;

 

// 예외중립( Exception neutral )

// exceptional c++
http://cafe.naver.com/cppmaster/1516


class
Test

{

       int buf[100];

public:

       Test() { throw 1; }

};

 

void main()

{

       try

       {

       Test* p = new Test; // 1. 메모리할당

                           // 2. 생성자호출- 그런데예외가나왔다.

       // (1)에서 할당한 메모리는 해지되었을까?? 낭비인가??

       // new 자체에서 에러 발생시 낭비를 방지하는 코드가 있다.

       }

       catch( int )

       {

       }

}

 

void foo() // 어떠한 예외라도 나올 수 있다. 예외가 없다는 throw()를 붙여줘야 한다.

{

       try

       {

             char* p = new char;

       }

       catch( std::bad_alloc e )

       {

             // 어떻게 처리할까?

             // 1. 메모리를 재할당해서 성공할 수 있다면 ok.

             // 2. 그렇지 않다면 호출자에게 다시 전달.. - 예외중립

             throw;

       }

}

 

//////////////////////////////////////////////////////////////////

// 아래 대입 연산자가 예외안전성을 지원하는가?

class String

{

       char* buff;

       int sz;

public:

 

       String& operator=( const String& s )

       {

             if ( &s == this ) return *this;

            

             char* temp = new char[ s.sz+1 ]; // 예외가 발생 할 수도 있다.

 

             strcpy( buff, s.buff );

 

             // 예외가 발생하지 않는 코드(강력보장)는 나중에 수행하자.

             delete[] buff;      // 예외가 없다.

             buff = temp;

             sz = s.sz;          // 예외가 없다.

 

             return *this;

       }

};

 

void main()

{

       String s1 = "hello";

       String s2 = "A";

 

       try

       {

             s1 = s2;

       }

       catch ( ... )

       {

       }

 

       cout << s1 << endl;   // 'A' 또는'hello" 가나와야한다.

}

 

class Stack

{

       int buf[10];

       int top;

public:

 

       int Pop() throw( StackUnderflowException )

       {

             if ( top < 0 )

             {

                    throw StackUnderflowException();

             }

             return buf[--top];

       }

};

 

void main()

{

       Stack s;

       try

       {

             int n = s.Pop();

       }

       catch( StackUnderflowException e )

       {

             cout << "예외처리" << endl;

       }

       // 예외를 잡아서 문제를 처리했다. 이제 stack을 계속 사용할 수 있을까?

       s.Push(10);

}

 

Tag |

// 방문자( Visitor )를 적용한 버전.

// Accepter가 있어야 한다.

 

#include <iostream>

#include <conio.h>

#include <string>

#include <vector>

using namespace std;

 

#define clrscr()    system("cls")

 

// Acceptor 인터페이스- 모든 방문자를 받아 들일수 있어야 한다.

class IVisitor;

 

struct IAcceptor

{

       virtual void accept( IVisitor* p ) = 0;

};

 

// Visitor 인터페이스- 방문 할수 있어야 한다.

struct IVisitor

{

       virtual void visit( IAcceptor* p ) = 0;

};

 

class AbstractMenu : public IAcceptor

{

       string title;

public:

       AbstractMenu( string s ) : title(s) { }

 

       virtual string GetTitle() const { return title; }

 

       virtual void SetTitle( string s ) { title = s; }

 

       virtual void Command() = 0; // 순수가상함수- 자식은 꼭 만들어야 한다.

 

       virtual void accept( IVisitor* p )

       {

             p->visit( this );          // Visitor 설계의 핵심

       }

};

 

class PopupMenu : public AbstractMenu

{

       vector<AbstractMenu*> menu_list; // Composite

public:

       PopupMenu(string s) : AbstractMenu(s) { }

 

       void Add( AbstractMenu* p ) { menu_list.push_back( p ); }

       void Remove() { }

 

       virtual string GetTitle() const

       {

             return string("[") + AbstractMenu::GetTitle() + string("]");

       }

 

       // 팝업 메뉴가 선택되었을때를 처리한다.

       virtual void Command()

       {

             while(1)

             {

                    // 화면을지우고자신의하위메뉴를열거해준다.

                    clrscr();

 

                    for( int i = 0; i < menu_list.size(); ++i )

                    {

                        cout << i+1 <<"."<< menu_list[i]->GetTitle() <<endl;

                    }

 

                    cout << menu_list.size() + 1 << ". 이전메뉴로" << endl;

                    //------------------------------------------------

                    int cmd;

                    cout << "메뉴를생성하세요>> ";

                    cin >> cmd;

 

                    if ( cmd == menu_list.size()+1 ) // 이전메뉴로 선택

                           break;  // 루프 탈출해서 자신을 이 함수를 마치고

                                   // 자신을 호출한 부분으로 돌아간다.

 

                    // 잘못 선택한 경우
                   
if ( cmd <= 0 || cmd > menu_list.size() )    

                           continue;

 

                    // 해당 메뉴를 실행한다.

                    menu_list[cmd-1]->Command();

             }

       }

 

       // 팝업 메뉴의 경우 모든 항목에 대해 visit를 호출해야 한다.

       virtual void accept( IVisitor* p )

       {

             p->visit( this );   // 먼저 자신을 보내고

            

             for( int i = 0; i < menu_list.size(); ++i )

                    menu_list[i]->accept( p );

       }

};

 

// 메뉴를 처리하고 싶은 모든 클래스는 아래 인터페이스를 구현해야 한다.

struct IMenuHandler

{

       virtual void MenuCommand( int id ) = 0;

};

 

class MenuItem : public AbstractMenu

{

       int id;

       IMenuHandler* pMenuHandler;

public:

       // 부모의 디폴트 생성자가 없으므로 여기서 초기화 해준다.

       MenuItem( string s, int n, IMenuHandler* p )

             : AbstractMenu(s), id(n), pMenuHandler(p) { }

 

       // 메뉴 선택시 처리

       virtual void Command()

       {

             if( pMenuHandler != 0 ) pMenuHandler->MenuCommand( id );

       }

};

 

// 메뉴의 각 항목의 색상을 변경하는 visitor

class RedVisitor : public IVisitor

{

public:

       virtual void visit( IAcceptor* p )

       {

             AbstractMenu* pMenu = (AbstractMenu*)p;

 

             string s = pMenu->GetTitle();

 

             s = s + string("-Red");

 

             pMenu->SetTitle( s );

       }

};

 

//------------------------------------------------------

class VideoShop : public IMenuHandler

{

public:

       void Init()

       {

             PopupMenu* p1 = new PopupMenu("ROOT");

 

             PopupMenu* p2 = new PopupMenu("고객관리");

             PopupMenu* p3 = new PopupMenu("Tape관리");

             PopupMenu* p4 = new PopupMenu("기타");

 

             p1->Add( p2 );

             p1->Add( p3 );

             p1->Add( p4 );

 

             p2->Add( new MenuItem("신규고객등록", 1, this) );

             p2->Add( new MenuItem("고객삭제", 2, this) );

             p2->Add( new MenuItem("기타", 3, this) );

 

             p3->Add( new MenuItem("신규Tape등록", 4, this) );

             p3->Add( new MenuItem("Tape삭제", 5, this) );

             p3->Add( new MenuItem("기타", 6, this) );

 

             RedVisitor r;

             p1->accept(&r);

 

             p1->Command();

       }

 

       void AddCustomer()

       {

             cout << "신규고객등록처리" << endl;

       }

 

       virtual void MenuCommand( int id )

       {

             // 메뉴 ID를 가지고 각 함수로 라우팅 한다.

             if ( id == 1 ) AddCustomer();

 

             cout << "다른ID처리" << endl;

             getch();

       }

};

 

int main()

{

       VideoShop v;

       v.Init();

}

Tag | ,

// 책임의 전가- Chain of reponsibility

// MFCVIEW- DOC - FRAME - APP 로 명령을 넘겨주는 것과 같은 원리..

 

#include <iostream>

#include <conio.h>

#include <string>

#include <vector>

using namespace std;

 

// 메뉴를 처리하고 싶은 모든 클래스는 아래 추상클래스에서 상속되어야 한다.

class IMenuHandler

{

       IMenuHandler* next;

public:

       IMenuHandler() : next(0) {}

 

       // 다음 처리할 객체를 지정한다.

       void SetNext( IMenuHandler* p ) { next = p; }

 

    void MenuCommand( int id )

       {

             // 먼저 자신이 처리를 시동한다.

             if ( Resolve( id ) == false ) // 처리를 못한 경우

             {

                    // 다음으로전달
                   
if ( next != 0 ) next->MenuCommand( id );

             }

       }

protected:

       virtual bool Resolve( int id ) = 0;

};

//-------------------------------------------------------

 

#define clrscr()    system("cls")

 

class AbstractMenu

{

       string title;

public:

       AbstractMenu( string s ) : title(s) { }

 

       virtual string GetTitle() const { return title; }

 

       virtual void Command() = 0;   // 순수가상함수- 자식은 꼭 만들어야 한다.

};

 

class PopupMenu : public AbstractMenu

{

       vector<AbstractMenu*> menu_list; // Composite

public:

       PopupMenu(string s) : AbstractMenu(s) { }

 

       void Add( AbstractMenu* p ) { menu_list.push_back( p ); }

       void Remove() { }
 

       virtual string GetTitle() const

       {

             return string("[") + AbstractMenu::GetTitle() + string("]");

       }

 

       // 팝업메뉴가 선택 되었을때를 처리한다.

       virtual void Command()

       {

             while(1)

             {

                    // 화면을 지우고 자신의 하위메뉴를 열거해준다.

                    clrscr();

 

                    for( int i = 0; i < menu_list.size(); ++i )

                    {

                         cout<<i + 1<<"."<< menu_list[i]->GetTitle() <<endl;

                    }

 

                    cout << menu_list.size() + 1 << ". 이전메뉴로" << endl;

                    //-----------------------------------------------

                    int cmd;

                    cout << "메뉴를 생성하세요>> ";

                    cin >> cmd;

 

                    if ( cmd == menu_list.size()+1 ) // 이전메뉴로 선택

                           break;  // 루프탈출해서 자신을 이 함수를  마치고

                                   // 자신을 호출한 부분으로 돌아간다.

                    // 잘못선택한경우

                    if ( cmd <= 0 || cmd > menu_list.size() )    

                           continue;

 

                    // 해당메뉴를 실행한다.

                    menu_list[cmd-1]->Command();

             }

       }

};

 

// 메뉴를 처리하고 싶은 모든 클래스는 아래 인터페이스를 구현해야 한다.

class MenuItem : public AbstractMenu

{

       int id;

       IMenuHandler* pMenuHandler;

public:

       // 부모의 디폴트 생성자가 없으므로 여기서 초기화 해준다.

       MenuItem( string s, int n, IMenuHandler* p )

             : AbstractMenu(s), id(n), pMenuHandler(p) { }

 

       // 메뉴선택시 처리

       virtual void Command()

       {

             if( pMenuHandler != 0 ) pMenuHandler->MenuCommand( id );

       }

};

//------------------------------------------------------

class TapeList : public IMenuHandler

{

public:

       virtual bool Resolve( int id )

       {

             switch( id )

             {

             case 1: foo(); getch(); return true;

             case 2: foo(); getch(); return true;

             }

             return false;

       }

 

       void foo()

       {

             cout << "TapeList::foo" << endl;

       }

};

 

//-----------------------------------------------------

class VideoShop : public IMenuHandler

{

       TapeList tp_list;

 

public:

       void Init()

       {

             PopupMenu* p1 = new PopupMenu("ROOT");

 

             PopupMenu* p2 = new PopupMenu("고객관리");

             PopupMenu* p3 = new PopupMenu("Tape관리");

             PopupMenu* p4 = new PopupMenu("기타");

 

             p1->Add( p2 );

             p1->Add( p3 );

             p1->Add( p4 );

 

             p2->Add( new MenuItem("신규고객등록", 1, this) );

             p2->Add( new MenuItem("고객삭제", 2, this) );

             p2->Add( new MenuItem("기타", 3, this) );

 

             p3->Add( new MenuItem("신규Tape등록", 4, this) );

             p3->Add( new MenuItem("Tape삭제", 5, this) );

             p3->Add( new MenuItem("기타", 6, this) );

 

             // 메뉴를 처리하고 싶은 다음 객체를 등록시킨다.

             this->SetNext( &tp_list );

 

             p1->Command();

       }

 

       void AddCustomer()

       {

             cout << "신규고객등록처리" << endl;

       }

 

       virtual void MenuCommand( int id )

       {

             // 메뉴ID를 가지고 각 함수로 라우팅한다.

             if ( id == 1 ) AddCustomer();

 

             cout << "다른ID처리" << endl;

             getch();

       }

 

       virtual bool Resolve( int id )

       {

             if ( id == 1 )

             {

                    AddCustomer();

                    return true;

             }

             return false;

       }

};

 

 

int main()

{

       VideoShop v;

       v.Init();

}

Tag | ,

#include <iostream>

#include <conio.h>

#include <string>

#include <vector>

using namespace std;

 

#define clrscr()    system("cls")

 

class AbstractMenu

{

       string title;

public:

       AbstractMenu( string s ) : title(s) { }

 

       virtual string GetTitle() const { return title; }

 

       virtual void Command() = 0; // 순수가상함수- 자식은 꼭 만들어야 한다.

};

 

class PopupMenu : public AbstractMenu

{

       vector<AbstractMenu*> menu_list; // Composite

public:

       PopupMenu(string s) : AbstractMenu(s) { }

 

       void Add( AbstractMenu* p ) { menu_list.push_back( p ); }

       void Remove() {}

 

       virtual string GetTitle() const

       {

             return string("[") + AbstractMenu::GetTitle() + string("]");

       }

 

       // 팝업메뉴가 선택 되었을때를 처리한다.

       virtual void Command()

       {

             while(1)

             {

                    // 화면을 지우고 자신의 하위메뉴를 열거 해준다.

                    clrscr();

 

                    for( int i = 0; i < menu_list.size(); ++i )

                    {

                           cout << i + 1 << "." << menu_list[i]->GetTitle() << endl;

                    }

 

                    cout << menu_list.size() + 1 << ". 이전메뉴로" << endl;

                    //---------------------------------------------------------

                    int cmd;

                    cout << "메뉴를생성하세요>> ";

                    cin >> cmd;

 

                    if ( cmd == menu_list.size()+1 ) // 이전메뉴로 선택

                           break;  // 루프 탈출해서 자신을 이함수를 마치고

                                   // 자신을 호출한 부분으로 돌아간다.

                    // 잘못 선택한 경우

                    if ( cmd <= 0 || cmd > menu_list.size() )
                           continue;

 

                    // 해당메뉴를 실행한다.;;

                    menu_list[cmd-1]->Command();

             }

       }

};

 

// 메뉴를 처리하고 싶은 모든 클래스는 아래 인터페이스를 구현해야 한다.

struct IMenuHandler

{

       virtual void MenuCommand( int id ) = 0;

};

 

class MenuItem : public AbstractMenu

{

       int id;

       IMenuHandler* pMenuHandler;

public:

       // 부모의 디폴트 생성자가 없으므로 여기서 초기화 해준다.

       MenuItem( string s, int n, IMenuHandler* p )

             : AbstractMenu(s), id(n), pMenuHandler(p) { }

 

       // 메뉴선택시 처리

       virtual void Command()

       {

             if( pMenuHandler != 0 ) pMenuHandler->MenuCommand( id );

       }

};

//------------------------------------------------------

class VideoShop : public IMenuHandler

{

public:

       void Init()

       {

             PopupMenu* p1 = new PopupMenu("ROOT");

 

             PopupMenu* p2 = new PopupMenu("고객관리");

             PopupMenu* p3 = new PopupMenu("Tape관리");

             PopupMenu* p4 = new PopupMenu("기타");

 

             p1->Add( p2 );

             p1->Add( p3 );

             p1->Add( p4 );

 

             p2->Add( new MenuItem("신규고객등록", 1, this) );

             p2->Add( new MenuItem("고객삭제", 2, this) );

             p2->Add( new MenuItem("기타", 3, this) );

 

             p3->Add( new MenuItem("신규Tape등록", 4, this) );

             p3->Add( new MenuItem("Tape삭제", 5, this) );

             p3->Add( new MenuItem("기타", 6, this) );

 

             p1->Command();

       }

 

       void AddCustomer()

       {

             cout << "신규고객등록처리" << endl;

       }

 

       virtual void MenuCommand( int id )

       {

             // 메뉴ID를 가지고 각 함수로 라우팅한다.

             if ( id == 1 ) AddCustomer();

 

             cout << "다른ID처리" << endl;

             getch();

       }

};

 

 

int main()

{

       VideoShop v;

       v.Init();

}

 

Tag | ,

8.8(수) C++ 디자인패턴 - Callback

from Study/C++ 2007/08/12 19:53 view 29654

// Callback의 개념.


class Car

{

       typedef void(*SIGNAL)();

       SIGNAL signal;

public:

       Car(SIGNAL s = 0) : signal(s) { }

 

       void SpeedUp( int speed )

       {

             cout << "Speed : " << speed << endl;

 

             if( speed > 100 && signal != 0 )

                    signal();    // 외부에알린다.

       }

};

 

void Police()

{

       cout << "Police Start.." << endl;

}

 

void main()

{

       Car c(Police);

 

       c.SpeedUp(100);

       c.SpeedUp(200);

}

/////////////////////////////////////////////////////////////

// 1. callback을 담당 할 클래스를 만들자. - 함수포인터를 관리하는 객체

class delegate

{

       typedef void(*SIGNAL)();

 

       SIGNAL signal;

       vector<delegate*> del_list;

public:

       delegate( SIGNAL s ) : signal(s) {}

 

       void Add( delegate* p ) { del_list.push_back( p ); }

       void Remove( delegate* p ) { }

      

       void Invoke()

       {

             if( signal != 0 ) signal();

 

             for( int i = 0; i < del_list.size(); ++i )

                    del_list[i]->Invoke();

       }

       // 연산자 재정의

       delegate* operator+=(delegate* p)

       {

             Add(p);

             return this;

       }

 

       void operator()()

       {

             Invoke();

       }

};

//----------------------------------------------------------

 

void foo() { cout << "foo" << endl; }

 

void main()

{

       delegate d1(foo);

       delegate d2(foo);

       delegate d3(foo);

 

       d3 += new delegate(foo);

       d3.Add( new delegate(foo) );

 

       d2 += &d3;

       d1.Add( &d2 );

 

       d1.Invoke();

       cout << endl;

       d1();

}

/////////////////////////////////////////////////////////////////
// 2. callback을 담당 할 클래스를 만들자. - 함수포인터를 관리하는 객체

class delegate

{

       typedef void(*SIGNAL)();

 

       SIGNAL signal;

       vector<delegate*> del_list;

public:

       delegate( SIGNAL s ) : signal(s) {}

 

       void Add( delegate* p ) { del_list.push_back( p ); }

       void Remove( delegate* p ) { }

      

       void Invoke()

       {

             if( signal != 0 ) signal();

 

             for( int i = 0; i < del_list.size(); ++i )

                    del_list[i]->Invoke();

       }

       // 연산자재정의

       delegate* operator+=(delegate* p)

       {

             Add(p);

             return this;

       }

 

       void operator()()

       {

             Invoke();

       }

};

//----------------------------------------------------------

class Car

{

public:

       delegate del;

public:

       Car() : del(0) { }

 

       void SpeedUp( int speed )

       {

             cout << "Speed : " << speed << endl;

 

             if( speed > 100  /*&& del != 0*/ )

                    del(); // 외부에알린다.

       }

};

 

void Police()

{

       cout << "Police Start.." << endl;

}

 

void main()

{

       Car c;

 

       c.del += new delegate(Police);

       c.del += new delegate(Police);

 

       c.SpeedUp(100);

       c.SpeedUp(200);

}

 

 

 

 

 

Tag | ,