首页 > 代码库 > 【2016-10-16】【坚持学习】【Day7】【建造者模式】
【2016-10-16】【坚持学习】【Day7】【建造者模式】
建造者模式:
一个复杂的对象由多个分部件组成。一个复杂对象
class Product { private string partA; //定义部件,部件可以是任意类型,包括值类型和引用类型 private string partB; private string partC; public string PartA { get { return partA; } set { partA = value; } } public string PartB { get { return partB; } set { partB = value; } } public string PartC { get { return partC; } set { partC = value; } } }
abstract class Builder { //创建产品对象 protected Product product = new Product(); public abstract void BuildPartA(); public abstract void BuildPartB(); public abstract void BuildPartC(); //返回产品对象 public Product GetResult() { return product; } }
class ConcreteBuilder1 : Builder { public override void BuildPartA() { product.PartA = "A1"; } public override void BuildPartB() { product.PartB = "B1"; } public override void BuildPartC() { product.PartC = "C1"; } }
class Director { private Builder builder; public Director(Builder builder) { this.builder = builder; } public void SetBuilder(Builder builder) { this.builder = builder; } //产品构建与组装方法 public Product Construct() { builder.BuildPartA(); builder.BuildPartB(); builder.BuildPartC(); return builder.GetResult(); } }
…… Builder builder = new ConcreteBuilder1(); //可通过配置文件实现 Director director = new Director(builder); Product product = director.Construct(); ……
【2016-10-16】【坚持学习】【Day7】【建造者模式】
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。