zoukankan      html  css  js  c++  java
  • 建造者模式(C++)

      建造者模式:将复杂的对象的表示和它的实现相分离,使其在同样的构建下可以构建不通的表示。

      上面的话可能不是很明白,举个例子就明白了。假设一个公司的软件项目流程是固定的,都需要经过需求理解、需求设计、需求审核、编码、测试这个五个步骤,但是这个公司是一个一级供应商,在拿到项目后,有些模块需要承包给二级供应商,如HMI、Media两个模块需求外包出去,但是根据项目要求,所有模块(包括外包出去的模块)必须要实施需求理解、需求设计、需求审核、编码、测试这个基本步骤。

      如果这个用面向对象表示出来,那我们可以这样设计,设计一个项目类,包含需求理解、需求设计、需求审核、编码、测试这5个接口;其次设计一个抽象的供应商类,提供5个实现需求理解、需求设计、需求审核、编码、测试的方法,再设计一个HMI类和一个media类,并继承自供应商类,分别实现需求理解、需求设计、需求审核、编码、测试这5方法;最后定义一个公司类,公司类里面包含一个供应商类的变量,在公司类里定义一个做项目的方法,在这个方法里分别调用供应商类的需求理解、需求设计、需求审核、编码、测试就实现了。毕竟语言有点苍白,来看看UML类图吧:

    再来看看代码就更明白:

    #include <iostream>
    #include <string>
    using namespace std;
    
    class Project  //项目类
    {
    public:
        Project():m_requirement(""), m_detailedDes(""), m_designRev(""), m_coding(""), m_test(""){}
    
        void requirementUnderstand(string str)//需求理解
        {
            cout<<" Project requirementUnderstand()"<<endl;
            m_requirement = str;
        }
        void detailedDesign(string str)//详细设计
        {
            cout<<" Project detailedDesign()"<<endl;
            m_detailedDes = str;
        }
        void designReview(string str)//设计核对
        {
            cout<<" Project designReview()"<<endl;
            m_designRev = str;
        }
        void coding(string str) //编码
        {
            cout<<" Project coding()"<<endl;
            m_designRev = str;
        }
        void test(string str) //测试
        {
            cout<<" Project test()"<<endl;
            m_designRev = str;
        }
    
    private:
        string m_requirement;
        string m_detailedDes;
        string m_designRev;
        string m_coding;
        string m_test;
    
    };
    
    
    class Supplier//供应商基类,即二级供应商
    {
    public:
        virtual void implentRequirementUnderstand() = 0;//实现需求理解
        virtual void implentDetailedDesign() = 0;        //实现详细设计
        virtual void implentDeignReview() = 0;            //实现设计核对
        virtual void implentCoding() = 0;                //实现编码
        virtual void implentTest() = 0;                    //实现测试
        virtual ~Supplier(){}
    };
    
    class SupplierHMI : public Supplier//HMI供应商
    {
    public:
        SupplierHMI()
        {
            m_pro = new Project();
        }
        ~SupplierHMI()
        {
            if(NULL != m_pro)
            {
                delete m_pro;
                m_pro = NULL;
            }
        }
        virtual void implentRequirementUnderstand(void)
        {
            cout<<"SupplierHMI implentRequirementUnderstand()"<<endl;
            m_pro->requirementUnderstand("HMI requirementUnderstand");
        }
        virtual void implentDetailedDesign(void)
        {
    
            cout<<"SupplierHMI implentDetailedDesign() "<<endl;
            m_pro->detailedDesign("HMI detailedDesign" );
        }
        virtual void implentDeignReview(void)
        {
            cout<<"SupplierHMI implentDeignReview()"<<endl;
            m_pro->designReview("HMI designReview" );
    
        }
        virtual void implentCoding(void)
        {
            cout<<"SupplierHMIProject implentCoding()"<<endl;
            m_pro->coding("HMI coding");
        }
        virtual void implentTest(void)
        {
            cout<<"SupplierHMI implentTest()"<<endl;
            m_pro->test("HMI test");
        }
    private:
        Project* m_pro;
    };
    
    class SupplierMedia : public Supplier//Media供应商
    {
    public:
        SupplierMedia()
        {
            m_pro = new Project();
        }
        ~SupplierMedia()
        {
            if(NULL != m_pro)
            {
                delete m_pro;
                m_pro = NULL;
            }
        }
        virtual void implentRequirementUnderstand(void)
        {
            cout<<"SupplierMedia implentRequirementUnderstand()"<<endl;
            m_pro->requirementUnderstand("Media requirementUnderstand");
        }
        virtual void implentDetailedDesign(void)
        {
            cout<<"SupplierMedia implentDetailedDesign() "<<endl;
            m_pro->detailedDesign("Media detailedDesign");
        }
        virtual void implentDeignReview(void)
        {
            cout<<"SupplierMedia implentDeignReview()"<<endl;
            m_pro->designReview("Media designReview");
        }
        virtual void implentCoding(void)
        {
            cout<<"SupplierMedia implentCoding()"<<endl;
            m_pro->coding("Media coding");
        }
        virtual void implentTest(void)
        {
            cout<<"SupplierMedia implentTest()"<<endl;
            m_pro->test("Media test");
        }
    private:
        Project* m_pro;
    };
    
    class Company//公司,即一级供应商
    {
    private:
        Supplier* m_supplier;
    public:
        Company(Supplier* sup):m_supplier(sup)
        {
        }
        ~Company()
        {
            if(NULL != m_supplier)
            {
                delete m_supplier;
                m_supplier = NULL;
            }
        }
        void doProject()//做这个项目
        {
            m_supplier->implentRequirementUnderstand();
            m_supplier->implentDetailedDesign();
            m_supplier->implentDeignReview();
            m_supplier->implentCoding();
            m_supplier->implentTest();
        }
    
    };
    
    int main(int argc, char** argv)
    {
        Supplier* supplier = new SupplierHMI();
        Company company(supplier);
        company.doProject();
        return 0;
    }

    输出如下:

  • 相关阅读:
    如何将asp.net MVC2项目升级为MVC3项目(微软官方自动升级工具:ASP.NET MVC 3 Application Upgrader )
    扩展Html Helper类,ASP.NET MVC框架提供了一个帮助我们构造Html元素的类:TagBuilder
    详解ASP.NET MVC2项目升级到MVC 3 RC
    NHibernate学习
    ASP.MVCNOTE
    MVC问题反馈页面代码
    Silverlightnote
    jqGrid
    必须掌握的八个DOS命令
    分页且带条件的存储过程
  • 原文地址:https://www.cnblogs.com/huiz/p/8380029.html
Copyright © 2011-2022 走看看