建造者模式(C++)

#include <iostream>
using namespace std;

class buildcomputer
{
public:
    buildcomputer(){}
    virtual ~buildcomputer(){}
    virtual void buildmouse()=0;
    virtual void buildmointor()=0;
    virtual void buildkeyboard()=0;
    virtual void buildhost()=0;
};

class winbuildcomputer : public buildcomputer
{
public:
    winbuildcomputer(){}
    virtual ~winbuildcomputer(){}
    void buildmouse(){cout<<"build win mouse"<<endl;}
    void buildmointor(){cout<<"build win mointor"<<endl;}
    void buildkeyboard(){cout<<"build win keyboard"<<endl;}
    void buildhost(){cout<<"build win host"<<endl;}
};

class macbuildcomputer : public buildcomputer
{
public:
    macbuildcomputer(){}
    virtual ~macbuildcomputer(){}
    void buildmouse(){cout<<"build mac mouse"<<endl;}
    void buildmointor(){cout<<"build mac mointor"<<endl;}
    void buildkeyboard(){cout<<"build mac keyboard"<<endl;}
    void buildhost(){cout<<"build mac host"<<endl;}
};

class director
{
private:
    buildcomputer *instance;

public:
    director(buildcomputer *bd)
        :instance(bd){}
    virtual ~director(){}
    void creator()
    {
        instance->buildmouse();
        instance->buildmointor();
        instance->buildkeyboard();
        instance->buildhost();
    }
};

int main()
{
    buildcomputer *p=new macbuildcomputer;
    director *dc=new director(p);
    dc->creator();
    delete dc;
    delete p;

    p=new winbuildcomputer;
    dc=new director(p);            
    dc->creator();
    delete dc;
    delete p;

    system("pause");
    return 0;
}
posted @ 2012-06-24 20:16  Dsp Tian  阅读(663)  评论(0编辑  收藏  举报