码迷,mamicode.com
首页 > 其他好文 > 详细

设计模式之建造者模式(Builder)

时间:2016-05-07 10:37:15      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:

建造者模式:

优点:

1.隔离了构建的步骤和具体的实现,为产品的具体实现提供了灵活性

2.封装和抽象了每个步骤的实现,实现了依赖倒置原则

3.封装了具体的步骤,减少了代码冗余

缺点:

1.要求构建的产品不能剧烈变化


代码示例:

Builer.h

#ifndef BULIDER_H
#define BULIDER_H

class Builder
{
public:
    virtual void buildHead() = 0;
    virtual void buildBody() = 0;
    virtual void buildLeftArm() = 0;
    virtual void buildRightArm() = 0;
    virtual void buildLeftLeg() = 0;
    virtual void buildRightLeg() = 0;
};

class ThinBuilder : public Builder
{
public:
    void buildHead();
    void buildBody();
    void buildLeftArm();
    void buildRightArm();
    void buildLeftLeg();
    void buildRightLeg();
};

class FatBuilder : public Builder
{
public:
    void buildHead();
    void buildBody();
    void buildLeftArm();
    void buildRightArm();
    void buildLeftLeg();
    void buildRightLeg();
};



class Director
{
public:
    Director(Builder *builder);
    void setBuilder(Builder *builder);
    void creadte();
private:
    Builder *m_builder;
};


#endif // BULIDER_H


Builder.cpp
#include "bulider.h"

#include <iostream>

void ThinBuilder::buildHead()
{
    std::cout << "build thin Head" << std::endl;
}

void ThinBuilder::buildBody()
{
    std::cout << "build thin Body" << std::endl;
}

void ThinBuilder::buildLeftArm()
{
    std::cout << "build thin LeftArm" << std::endl;
}

void ThinBuilder::buildRightArm()
{
    std::cout << "build thin RightArm" << std::endl;
}

void ThinBuilder::buildLeftLeg()
{
    std::cout << "build thin LeftLeg" << std::endl;
}

void ThinBuilder::buildRightLeg()
{
    std::cout << "build thin RightLeg" << std::endl;
}


void FatBuilder::buildHead()
{
    std::cout << "build fat Head" << std::endl;
}

void FatBuilder::buildBody()
{
    std::cout << "build fat Body" << std::endl;
}

void FatBuilder::buildLeftArm()
{
    std::cout << "build fat LeftArm" << std::endl;
}

void FatBuilder::buildRightArm()
{
    std::cout << "build fat RightArm" << std::endl;
}

void FatBuilder::buildLeftLeg()
{
    std::cout << "build fat LeftLeg" << std::endl;
}

void FatBuilder::buildRightLeg()
{
    std::cout << "build fat RightLeg" << std::endl;
}


Director::Director(Builder *builder)
    :m_builder(builder)
{
}

void Director::setBuilder(Builder *builder)
{
    m_builder = builder;
}

void Director::creadte()
{
    m_builder->buildHead();
    m_builder->buildBody();
    m_builder->buildLeftArm();
    m_builder->buildRightArm();
    m_builder->buildLeftLeg();
    m_builder->buildRightLeg();
}

main.cpp

#include <iostream>
#include "bulider.h"

int main()
{
    FatBuilder fatBuilder;
    Director director(&fatBuilder);
    director.creadte();

    std::cout << std::endl;

    ThinBuilder thisBuilder;
    director.setBuilder(&thisBuilder);
    director.creadte();

    return 0;
}


设计模式之建造者模式(Builder)

标签:

原文地址:http://blog.csdn.net/fu1201520/article/details/51330581

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!