设计模式(7)-建造模式(Build)
【描述】建造模式定义了处理其他对象的复杂构建的对象设计。
【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-
补充:软件开发 , C++ ,