• 设计模式——9.建造者模式


    建造者模式(Builder)

    Builder模式简介:

    将一个复杂对象的构建其表示相分离,使得同样的构建过程(稳定部分)可以创建不同的表示(变化)

    Builder模式结构:

    Builder

    Builder模式要点:

    • Builder模式主要用于分步骤构建一个复杂对象,其中的构建步骤是稳定的,而负责对象的各个部分则经常变化。

    • 要注意不同语言例如C++、C#中构造函数内调用虚函数机制的差别。

    C++代码

    具体产品类Product:

    #pragma once
    #include<string>
    #include<iostream>
    using namespace std;
    
    class Product
    {
    public:
    	Product();
    	virtual ~Product();
    	void setPartA(string pA);
    	void setPartB(string pB);
    	void setPartC(string pC);
    	void echo();
    
    private:
    	string m_pA;
    	string m_pB;
    	string m_pC;
    };
    
    
    //file: Product.cpp
    #include "pch.h"
    #include "Product.h"
    
    Product::Product() {}
    
    Product::~Product() {}
    
    void Product::setPartA(string pA)
    {
    	m_pA = pA;
    }
    
    void Product::setPartB(string pB)
    {
    	m_pB = pB;
    }
    
    void Product::setPartC(string pC)
    {
    	m_pC = pC;
    }
    
    void Product::echo()
    {
    	cout << "The Product's PartA name : " <<m_pA << " , PartB name : " << m_pB << " , PartC name : " << m_pC << " ." << endl;
    }
    

    创建具体产品各个部分的接口类Builder以及其具体实现类:

    //file: Builder.h
    #pragma once
    #include "Product.h"
    class Builder
    {
    public:
    	Builder();
    	virtual ~Builder();
    	virtual void buildPartA() =0;
    	virtual void buildPartB() = 0;
    	virtual void buildPartC() = 0;
    	virtual Product* getProduct();
    
    protected:
    	Product* m_prod;
    };
    
    class ConcreteBuilder1 : public Builder
    {
    public:
    	ConcreteBuilder1();
    	virtual ~ConcreteBuilder1();
    	virtual void buildPartA();
    	virtual void buildPartB();
    	virtual void buildPartC();
    };
    
    //file: Builder.cpp
    #include "pch.h"
    #include "Builder.h"
    
    Builder::Builder() 
    {
    	m_prod = new Product();
    }
    
    Builder::~Builder() {}
    
    Product* Builder::getProduct()
    {
    	return m_prod;
    }
    
    ConcreteBuilder1::ConcreteBuilder1() {}
    
    ConcreteBuilder1::~ConcreteBuilder1() {}
    
    void ConcreteBuilder1::buildPartA()
    {
    	m_prod->setPartA("PartA 1");
    }
    
    void ConcreteBuilder1::buildPartB()
    {
    	m_prod->setPartB("PartB 1");
    }
    
    void ConcreteBuilder1::buildPartC()
    {
    	m_prod->setPartC("PartC 1");
    }
    

    Director指挥者类:

    //Director.h
    #pragma once
    #include "Builder.h"
    class Director
    {
    public:
    	Director();
    	virtual ~Director();
    	void setBuilder(Builder* pB);
    	Product* construct();
    protected:
    	Builder* m_builder;
    };
    
    //file:Director.cpp
    #include "pch.h"
    #include "Director.h"
    
    Director::Director() {}
    
    Director::~Director() {}
    
    void Director::setBuilder(Builder* pB)
    {
    	m_builder = pB;
    }
    
    Product* Director::construct()
    {
    	m_builder->buildPartA();
    	m_builder->buildPartB();
    	m_builder->buildPartC();
    	return m_builder->getProduct();
    }
    

    客户端实例代码:

    // BuilderPattern.cpp : This file contains the 'main' function. Program execution begins and ends there.
    #include "pch.h"
    #include <iostream>
    #include "Director.h"
    
    int main()
    {
    	Builder* builder1 = new ConcreteBuilder1();
    	Director* director = new Director();
    	director->setBuilder(builder1);
    	Product* p1 = director->construct();
    	p1->echo();
    
    	delete p1, builder1 ,director;
    	return 0;
    }
    

    输出实例:

    BuilderRes2

    C#代码

    具体产品类Product:

    public class Product
    {
    	public string PartA { get; set; }
    	public string PartB { get; set; }
    	public string PartC { get; set; }
    
    	public void Echo()
    	{
    		Console.WriteLine("The Product's PartA name :{0} , PartB name : {1} , PartC name : {2} .",PartA,PartB,PartC);
    	}
    }
    

    创建具体产品各个部分的接口类Builder以及其具体实现类:

    public abstract class Builder
    {
    	public Builder()
    	{
    		product = new Product();
    	}
    
    	public abstract void BuildPartA();
    	public abstract void BuildPartB();
    	public abstract void BuildPartC();
    
    	public virtual Product GetProduct()
    	{
    		return product;
    	}
    
    	protected Product product;
    }
    
    public class ConcreteBuilder1 : Builder
    {
    	public override void BuildPartA()
    	{
    		product.PartA = "PartA 1";
    	}
    
    	public override void BuildPartB()
    	{
    		product.PartB = "PartB 1";
    	}
    
    	public override void BuildPartC()
    	{
    		product.PartC = "PartC 1";
    	}
    }
    
    public class ConcreteBuilder2 : Builder
    {
    	public override void BuildPartA()
    	{
    		product.PartA = "PartA 2";
    	}
    
    	public override void BuildPartB()
    	{
    		product.PartB = "PartB 2";
    	}
    
    	public override void BuildPartC()
    	{
    		product.PartC = "PartC 2";
    	}
    }
    

    Director指挥者类:

    public class Director
    {
    	Builder builder;
    
    	public Director() { }
    
    	public Director(Builder pBuilder)
    	{
    		builder = pBuilder;
    	}
    
    	public void SetBuilder(Builder pBuilder)
    	{
    		builder = pBuilder;
    	}
    
    	public Product Construct()
    	{
    		builder.BuildPartA();
    		builder.BuildPartB();
    		builder.BuildPartC();
    		return builder.GetProduct();
    	}
    }
    

    客户端实例代码:

    class Program
    {
    	static void Main(string[] args)
    	{
    		Builder builder1 = new ConcreteBuilder1();
    		Director director = new Director(builder1);
    		Product p1 = director.Construct();
    		p1.Echo();
    
    		Builder builder2 = new ConcreteBuilder2();
    		director.SetBuilder(builder2);
    		Product p2 = director.Construct();
    		p2.Echo();
    
    		Console.ReadKey();
    	}
    }
    

    实例输出:

    BuilderRes

    REF

    书籍:

    设计模式与游戏开发、大话设计模式

    GitHub:

    https://github.com/me115/design_patterns

  • 相关阅读:
    winform treeview checkbox递归算法利用
    转载10种代码风格
    Canvas没有添加Background,点击就不在Canvas上?
    数据库安装文件制作(C#)
    Panel的公有属性Background
    Teechar 常用属性
    NHibernate
    解决:请购买WinRAR许可,您注册还剩下40天(WinRAR老是弹出烦人的对话框)
    在WinForm中用Visifire实现数据统计转自蓝月天南
    【转】TeeChart的用法
  • 原文地址:https://www.cnblogs.com/sylvan/p/9978981.html
Copyright © 2020-2023  润新知