본문 바로가기
C++/0x01-design pattern

design pattern ][ Abstract Factory pattern

by SpeeDr00t 2016. 7. 14.
반응형

design pattern ][ Abstract Factory pattern

1. 예제소스1

#include <iostream> 
 
using namespace std; 
 
#define DEFAULT 0 
#define GET 1  
#define PUT 2 
 
/* Interface */ 
class Handler 
{ 
    public: 
        virtual void execute(char *instr)=0; 
        virtual int getID()=0; 
}; 
 
/* Factory Class */ 
class HandlerFactory  
{ 
    public: 
        Handler *instance(){} 
}; 
 
class GetHandler : public Handler 
{ 
    public : 
        void execute(char *str) 
        { 
            cout << "Get Handler : " << str << endl; 
        } 
        int getID(){return GET;} 
}; 
class PutHandler : public Handler 
{ 
    public : 
        void execute(char *str) 
        { 
            cout << "Put Handler : " << str << endl; 
        } 
        int getID(){return PUT;} 
}; 
 
class DefaultHandler : public Handler 
{ 
    public : 
        void execute(char *str) 
        { 
            cout << "DefaultHandler : " << str << endl; 
        } 
        int getID(){return DEFAULT;} 
}; 
 
class GetDataHandler : public HandlerFactory 
{ 
    public :   
        Handler * instance(int type) 
        { 
            if (type == GET) 
                return new GetHandler; 
            if (type == PUT) 
                return new PutHandler; 
            return new DefaultHandler; 
        } 
}; 
 
int main() 
{ 
    GetDataHandler *dh = new GetDataHandler;  
    Handler *mh = dh->instance(PUT); 
 
    mh->execute((char *)"ok"); 
    cout << "ID : " << mh->getID() << endl; 
} 
 
 

결과
hacker@HACKER:~/cpp$ g++ -o afp1 afp1.cpp

hacker@HACKER:~/cpp$ ./afp1
Put Handler : ok
ID : 2

2. 예제소스2
 
#include <iostream> 
using namespace std; 
 
class Lock 
{ 
    public: 
        virtual void lock()=0; 
        virtual void unlock()=0; 
}; 
 
class LockFactory 
{ 
    public: 
        Lock *getInstance(int type); 
}; 
 
class FileLock : public Lock 
{ 
    public: 
        void lock(){cout << "File Record LOCK " << endl;} 
        void unlock(){cout << "File Record UN Lock " << endl;} 
}; 
 
class MutexLock : public Lock 
{ 
    public: 
        void lock(){cout << "Mutex LOCK " << endl;} 
        void unlock(){cout << "Mutex UN Lock " << endl;} 
}; 
 
class DefaultLock : public Lock 
{ 
    public: 
        void lock(){cout << "LOCK " << endl;} 
        void unlock(){cout << "UN Lock " << endl;} 
}; 
 
 
class GetLockHandler : public LockFactory 
{ 
    public: 
        Lock *gethandler(int type)     
        { 
            if (type == 1) 
            { 
                return new MutexLock; 
            } 
            if (type == 2) 
            { 
                return new FileLock; 
            } 
            return new DefaultLock;  
        } 
}; 
 
int main(int argc, char ** argv) 
{ 
    GetLockHandler *LockHandler = new GetLockHandler;  
    Lock *myLock = LockHandler->gethandler(2); 
    myLock->lock(); 
    myLock->unlock(); 
    return 0; 
} 
 
 

결과
hacker@HACKER:~/cpp$ g++ -o afp2 afp2.cpp

hacker@HACKER:~/cpp$ ./afp2
File Record LOCK
File Record UN Lock


반응형

'C++ > 0x01-design pattern' 카테고리의 다른 글

decorator pattern.  (0) 2016.07.14
composite pattern  (0) 2016.07.14
Observer pattern  (0) 2016.07.14
state 패턴  (0) 2016.07.14
design pattern ][ SingleTon  (0) 2016.07.14