程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 設計模式(7)-建造模式(Build)

設計模式(7)-建造模式(Build)

編輯:C++入門知識

【描述】建造模式定義了處理其他對象的復雜構建的對象設計。

【UML圖】

 \

圖1

1 BuildClient類提供了setBuilder()方法,設置一個建造者;

2 Builder類提供了一組方法接口,BuildA()、BuildB()、BuildC();

3 BuildClient類提供了build()方法,調用Builder對象的一組方法。

 

【示例代碼】

builder.h

[html]
#ifndef BUILDER_H 
#define BUILDER_H 
 
class Builder 

public: 
    Builder(); 
 
public: 
    virtual void BuildA(); 
    virtual void BuildB(); 
    virtual void BuildC(); 
 
}; 
 
#endif // BUILDER_H 

#ifndef BUILDER_H
#define BUILDER_H

class Builder
{
public:
    Builder();

public:
    virtual void BuildA();
    virtual void BuildB();
    virtual void BuildC();

};

#endif // BUILDER_H
builder.cpp

[html]
#include <QDebug> 
#include "builder.h" 
 
Builder::Builder() 

    qDebug()<<"construct Builder"; 

 
void Builder::BuildA() 

 

 
void Builder::BuildB() 

 

 
void Builder::BuildC() 

 

#include <QDebug>
#include "builder.h"

Builder::Builder()
{
    qDebug()<<"construct Builder";
}

void Builder::BuildA()
{

}

void Builder::BuildB()
{

}

void Builder::BuildC()
{

}
xmlbuilder.h

[html]
#ifndef XMLBUILDER_H 
#define XMLBUILDER_H 
 
#include "builder.h" 
 
class XMLBuilder : public Builder 

public: 
    XMLBuilder(); 
 
public: 
    void BuildA(); 
    void BuildB(); 
    void BuildC(); 
}; 
 
#endif // XMLBUILDER_H 

#ifndef XMLBUILDER_H
#define XMLBUILDER_H

#include "builder.h"

class XMLBuilder : public Builder
{
public:
    XMLBuilder();

public:
    void BuildA();
    void BuildB();
    void BuildC();
};

#endif // XMLBUILDER_H

xmlbuilder.cpp

[html]
#include <QDebug> 
#include "xmlbuilder.h" 
 
XMLBuilder::XMLBuilder() 

    qDebug()<<"construct XMLBuilder"; 

 
void XMLBuilder::BuildA() 

    qDebug()<<"XMLBuilder BuildA"; 

 
void XMLBuilder::BuildB() 

    qDebug()<<"XMLBuilder BuildB"; 

 
void XMLBuilder::BuildC() 

    qDebug()<<"XMLBuilder BuildC"; 

#include <QDebug>
#include "xmlbuilder.h"

XMLBuilder::XMLBuilder()
{
    qDebug()<<"construct XMLBuilder";
}

void XMLBuilder::BuildA()
{
    qDebug()<<"XMLBuilder BuildA";
}

void XMLBuilder::BuildB()
{
    qDebug()<<"XMLBuilder BuildB";
}

void XMLBuilder::BuildC()
{
    qDebug()<<"XMLBuilder BuildC";
}

textbuilder.h

[html]
#ifndef TEXTBUILDER_H 
#define TEXTBUILDER_H 
 
#include "builder.h" 
 
class TextBuilder : public Builder 

public: 
    TextBuilder(); 
 
public: 
    void BuildA(); 
    void BuildB(); 
    void BuildC(); 
}; 
 
#endif // TEXTBUILDER_H 

#ifndef TEXTBUILDER_H
#define TEXTBUILDER_H

#include "builder.h"

class TextBuilder : public Builder
{
public:
    TextBuilder();

public:
    void BuildA();
    void BuildB();
    void BuildC();
};

#endif // TEXTBUILDER_H

textbuilder.cpp

[html]
#include <QDebug> 
#include "textbuilder.h" 
 
TextBuilder::TextBuilder() 

     qDebug()<<"construct TextBuilder"; 

 
void TextBuilder::BuildA() 

    qDebug()<<"TextBuilder BuildA"; 

 
void TextBuilder::BuildB() 

    qDebug()<<"TextBuilder BuildB"; 

 
void TextBuilder::BuildC() 

    qDebug()<<"TextBuilder BuildC"; 

#include <QDebug>
#include "textbuilder.h"

TextBuilder::TextBuilder()
{
     qDebug()<<"construct TextBuilder";
}

void TextBuilder::BuildA()
{
    qDebug()<<"TextBuilder BuildA";
}

void TextBuilder::BuildB()
{
    qDebug()<<"TextBuilder BuildB";
}

void TextBuilder::BuildC()
{
    qDebug()<<"TextBuilder BuildC";
}
 

buildclient.h

[html]
#ifndef BUILDCLIENT_H 
#define BUILDCLIENT_H 
 
#include "builder.h" 
 
class BuildClient 

public: 
    BuildClient(); 
 
private: 
    Builder *builder; 
 
public: 
    void setBuilder(Builder *builder); 
    void build(); 
}; 
 
#endif // BUILDCLIENT_H 

#ifndef BUILDCLIENT_H
#define BUILDCLIENT_H

#include "builder.h"

class BuildClient
{
public:
    BuildClient();

private:
    Builder *builder;

public:
    void setBuilder(Builder *builder);
    void build();
};

#endif // BUILDCLIENT_H

buildclient.cpp

[html]
#include <QDebug> 
#include "buildclient.h" 
 
BuildClient::BuildClient() 


 
void BuildClient::setBuilder(Builder *builder) 

    this->builder = builder; 

 
void BuildClient::build() 

    builder->BuildA(); 
    builder->BuildB(); 
    builder->BuildC(); 

#include <QDebug>
#include "buildclient.h"

BuildClient::BuildClient()
{
}

void BuildClient::setBuilder(Builder *builder)
{
    this->builder = builder;
}

void BuildClient::build()
{
    builder->BuildA();
    builder->BuildB();
    builder->BuildC();
}
main.cpp

[html]
#include <QDebug> 
#include "buildclient.h" 
#include "xmlbuilder.h" 
#include "textbuilder.h" 
 
int main(int argc, char **argv) 

    BuildClient client; 
    client.setBuilder(new XMLBuilder); 
    client.build(); 
    return 0; 

#include <QDebug>
#include "buildclient.h"
#include "xmlbuilder.h"
#include "textbuilder.h"

int main(int argc, char **argv)
{
    BuildClient client;
    client.setBuilder(new XMLBuilder);
    client.build();
    return 0;
}

【運行結果】

[html]
construct Builder  
construct XMLBuilder  
XMLBuilder BuildA  
XMLBuilder BuildB  
XMLBuilder BuildC  

construct Builder
construct XMLBuilder
XMLBuilder BuildA
XMLBuilder BuildB
XMLBuilder BuildC 

【對比】建造模式和策略模式

設計模式(3)-策略模式(Strategy)一文中,示例代碼UML圖,如圖2所示:

 \

圖2

對比策略模式的UML圖,不難發現策略模式與建造模式極為相似。他們之間有何區別呢?

區別:策略模式提供的是一個方法,而建造模式提供的是一組方法。

 

對於建造模式和策略模式的區別,有不同看法,歡迎提出寶貴意見,共同學習!

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved