设计模式4-建造者模式

引用:4. 建造者模式 — Graphic Design Patterns

建造者模式包含如下角色:

  • Builder:抽象建造者
  • ConcreteBuilder:具体建造者
  • Director:指挥者
  • Product:产品角色
  • ../_images/Builder.jpg

     

  • 一、go语言版  

  • package main
    import "fmt"
    type Product struct {
        a int
        b int 
        c int
    }
    func (product *Product) setA(data int) {
        product.a = data
    }
    func (product *Product) setB(data int) {
        product.b = data
    }
    func (product *Product) setC(data int) {
        product.c = data
    }
    func (product *Product) show() {
        var sum int
        sum = product.a + product.b + product.c
        fmt.Printf("sum = %v\n",sum)
    }
    type Builder interface {
        BuildPartA()
        BuildPartB() 
        BuildPartC() 
        GetProduct() *Product
    }
    type ConcreteBuilder struct{
        product Product
    }
    func (conBuilder* ConcreteBuilder) BuildPartA() {
        fmt.Printf("BuildPartA construct\n")
        conBuilder.product.setA(6)
    }
    func (conBuilder* ConcreteBuilder) BuildPartB() {
        fmt.Printf("BuildPartB construct\n")
        conBuilder.product.setB(16)
    }
    func (conBuilder* ConcreteBuilder) BuildPartC() {
        fmt.Printf("BuildPartC construct\n")
        conBuilder.product.setC(26)
    }
    func (conBuilder* ConcreteBuilder) GetProduct() *Product {
        return &conBuilder.product
    }
    type Director struct {
        builder Builder
    }
    func NewDirector(builder Builder) *Director {
        return &Director{
            builder: builder,
        }
    }
    func (d *Director) Construct() {
        d.builder.BuildPartA()
        d.builder.BuildPartB()
        d.builder.BuildPartC()
    }
    func main() {
        builder := &ConcreteBuilder{}
        director := NewDirector(builder)
        director.Construct()
        builder.GetProduct().show()
    }

  • 二、c++语言版

  • #include <iostream>
    using namespace std;
    class Product
    {
    public:
        Product() {
            cout << "Product construct" << endl;
        }
        void setA(int a) {
            a_ = a;
        }
        void setB(int b) {
            b_ = b;
        }
        void setC(int c) {
            c_ = c;
        }
        void show() {
            int sum = a_ + b_ + c_;
            cout << "sum = " << sum << " " << endl;
        }
    private:
        int a_;
        int b_;
        int c_;
    };
    class Builder 
    {
    public:
        virtual void BuildPartA() = 0;
        virtual void BuildPartB() = 0;
        virtual void BuildPartC() = 0;
        virtual Product* GetProduct() = 0;
    };
    class ConcreteBuilder : public Builder 
    {
    public:
        ConcreteBuilder() : product_(nullptr) {
            product_ = new Product();
        }
        void BuildPartA() {
             cout << "BuildPartA construct" << endl;
             product_->setA(6);
        }
        void BuildPartB() {
             cout << "BuildPartB construct" << endl;
             product_->setB(10);
        }
        void BuildPartC() {
             cout << "BuildPartC construct" << endl;
             product_->setC(11);
        }
        Product* GetProduct() {
            return product_;
        }
    private:
        Product *product_;
    };
    class Director {
    public:
        Director(Builder *bld) {
            bld_ = bld;
        }
        void Construct() {
            bld_->BuildPartA();
            bld_->BuildPartB();
            bld_->BuildPartC();
        }
    private:
        Builder *bld_;
    };
    int main() 
    {
        ConcreteBuilder *builder = new ConcreteBuilder;
        Director *director = new Director(builder);
        director->Construct();
        builder->GetProduct()->show();
    }

  • 三、c语言版

  • #include <stdio.h>
    typedef struct _Product {
        int a_;
        int b_;
        int c_;
        void (*setA)(struct _Product *product, int data);
        void (*setB)(struct _Product *product, int data);
        void (*setC)(struct _Product *product, int data);
        void (*show)(struct _Product *product);
    }Product;
    void setA(struct _Product *product, int data) 
    {
        product->a_ = data;
    }
    void setB(struct _Product *product, int data) 
    {
        product->b_ = data;
    }
    void setC(struct _Product *product, int data) 
    {
        product->c_ = data;
    }
    void show(struct _Product *product) 
    {
        int sum;
        sum = product->a_ + product->b_ + product->c_;
        printf("sum = %d\n", sum);
    }
    Product product = {
        .setA = setA,
        .setB = setB,
        .setC = setC,
        .show = show,
    };
    typedef struct _Builder {
        void (*BuildPartA)(Product *product);
        void (*BuildPartB)(Product *product);
        void (*BuildPartC)(Product *product);
        Product *product;
    }Builder;
     void BuildPartA(Product *product)
     {
        product->setA(product, 6);
     }
    void BuildPartB(Product *product)
     {
        product->setB(product, 26);
     }
     void BuildPartC(Product *product)
     {
        product->setC(product, 16);
     }
    Builder ConcreteBuilder = {
        .BuildPartA = BuildPartA,
        .BuildPartB = BuildPartB,
        .BuildPartC = BuildPartC,
    };
    typedef struct _Director {
        void (*Construct)(Builder *builder);
    }Director;
    void Construct(Builder *builder)
    {
        builder->BuildPartA(builder->product);
        builder->BuildPartB(builder->product);
        builder->BuildPartC(builder->product);
    }
    Director director = {
        .Construct = Construct,
    };
    int main()
    {
        ConcreteBuilder.product = &product;
        director.Construct(&ConcreteBuilder);
        product.show(&product);
        return 0;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值