C++设计模式之建造者模式

2020-01-06 13:01:08王旭

 
#include <iostream>
using namespace std;
 
typedef enum MANTYPETag
{
    kFatMan,
    kThinMan,
    kNormal
}MANTYPE;
 
class Man
{
public:
    void SetHead(MANTYPE type){ m_Type = type; }
    void SetBody(MANTYPE type){ m_Type = type; }
    void SetLeftHand(MANTYPE type){ m_Type = type; }
    void SetRightHand(MANTYPE type){ m_Type = type; }
    void SetLeftFoot(MANTYPE type){ m_Type = type; }
    void SetRightFoot(MANTYPE type){ m_Type = type; }
    void ShowMan()
    {
        switch (m_Type)
        {
        case kFatMan:
            cout<<"I'm a fat man"<<endl;
            return;
 
        case kThinMan:
            cout<<"I'm a thin man"<<endl;
            return;
 
        default:
            cout<<"I'm a normal man"<<endl;
            return;
        }
    }
 
private:
    MANTYPE m_Type;
};
 
// Builder
class Builder
{
public:
    virtual void BuildHead(){}
    virtual void BuildBody(){}
    virtual void BuildLeftHand(){}
    virtual void BuildRightHand(){}
    virtual void BuildLeftFoot(){}
    virtual void BuildRightFoot(){}
    virtual Man *GetMan(){ return NULL; }
};
 
// FatManBuilder
class FatManBuilder : public Builder
{
public:
    FatManBuilder(){ m_FatMan = new Man(); }
    void BuildHead(){ m_FatMan->SetHead(kFatMan); }
    void BuildBody(){ m_FatMan->SetBody(kFatMan); }
    void BuildLeftHand(){ m_FatMan->SetLeftHand(kFatMan); }
    void BuildRightHand(){ m_FatMan->SetRightHand(kFatMan); }
    void BuildLeftFoot(){ m_FatMan->SetLeftFoot(kFatMan); }