C#设计模式--建造者模式

0.C#设计模式--简单工厂模式

1.C#设计模式--工厂方法模式

2.C#设计模式--抽象工厂模式

3.C#设计模式--单例模式

设计模式:

建造者模式(Builder Pattern)

简单介绍:

建造者模式参与者:

  • Builder:为创建一个Product对象的各个部件指定抽象接口;
  •  ConcreteBuilder

    ° 实现Builder的接口以构造和装配该产品的各个部件

    ° 定义并明确它所创建的表示

    ° 提供一个检索Product的接口

  •  Director:构造一个使用Builder接口的对象;
  • Product

    ° 表示被构造的复杂对象。ConcreteBuilder创建该产品的内部表示并定义它的装配过程

    ° 包含定义组成部件的类,包括将这些部件装配成最终产品的接口

  在建造者模式中,Director规定了创建一个对象所需要的步骤和次序,Builder则提供了一些列完成这些步骤的方法,ConcreteBuilder给出了这些方法的具体实现,是对象的直接创建者。

建造者模式类图:

C#设计模式--建造者模式_第1张图片

 建造者模式c#代码举例:

 Product类 产品类

public class Product
    {
        private List _parts = new List();

        public void Add(string part)
        {
            _parts.Add(part);
        }

        public void Show()
        {
            Console.WriteLine("Product Parts");
            foreach (string part in _parts)
            {
                Console.WriteLine(part);
            }
        }
    }

Builder类 抽象接口类

/// 
    /// 为创建一个Product对象的各个部件指定抽象接口
    /// 
    public abstract class Builder
    {
        public abstract void BuildPart();
        public abstract Product GetResult();
    }

ConcreteBuilder类 具体创建者类

    public class ConcreteBuilder : Builder
    {
        private Product _product = new Product();

        public override void BuildPart()
        {
            _product.Add("Part");
        }

        public override Product GetResult()
        {
            return _product;
        }
    }

Director类 提供使用Builder类的接口

public class Director
    {
        /// 
        /// 构造一个使用Builder接口的对象
        /// 
        public void Construct(Builder builder)
        {
            builder.BuildPart();
        }
    }

测试代码:

    class Test
    {
        static void Main(string[] args)
        {
            // Create director and builders
            Director director = new Director();

            Builder b1 = new ConcreteBuilder();

            // Construct two products
            director.Construct(b1);
            Product p1 = b1.GetResult();
            p1.Show();

            Console.Read();
        }
    }

运行结果:

C#设计模式--建造者模式_第2张图片

源代码工程文件下载


实例举例,手机生产举例  

  • Builder-->MobilePhoneBuilder
  •  ConcreteBuilder-->XiaoMIBuilder和AppleBuilder
  •  Director-->Factory
  • Product -->MobilePhone

分别有:手机品牌(brand),处理器(cup),运行内存(ram),外置内存(rom)信息

MobilePhone类产品类 

public class MobilePhone
    {
        private string _brand;
        private Dictionary _Info = new Dictionary();

        public string this[string key]
        {
            get { return _Info[key]; }
            set { _Info[key] = value; }
        }
        public MobilePhone(string brand)
        {
            this._brand = brand;
        }
        public void Show()
        {
            Console.WriteLine("\n-----Info--------");
            Console.WriteLine(" BRAND: {0}", _brand);
            Console.WriteLine(" CPU  : {0}", _Info["cpu"]);
            Console.WriteLine(" RAM  : {0}", _Info["ram"]);
            Console.WriteLine(" ROM  : {0}", _Info["rom"]);
        }
    }

MobilePhoneBuilder类 创建者抽象类

 public abstract class MobilePhoneBuilder
    {
        protected MobilePhone _mobilePhone;

       
        public MobilePhone mobilePhone
        {
            get { return _mobilePhone; }
        }

        // Abstract build methods
        public abstract void BuildCPU();
        public abstract void BuildRAM();
        public abstract void BuildROM();
        
    }

XiaoMIBuilder类 小米手机创建者类

public class XiaoMIBuilder:MobilePhoneBuilder
    {
        public XiaoMIBuilder()
        {
            _mobilePhone = new MobilePhone("小米5s");
        }
        public override void BuildCPU()
        {
            _mobilePhone["cpu"] = "骁龙821";
        }

        public override void BuildRAM()
        {
            _mobilePhone["ram"] = "4G";
        }

        public override void BuildROM()
        {
            _mobilePhone["rom"] = "32G";
        }
    }

AppleBuilder类 苹果手机的具体创建类

public class AppleBuilder:MobilePhoneBuilder
    {
        public AppleBuilder()
        {
            _mobilePhone = new MobilePhone("iphone7s plus");
        }
        public override void BuildCPU()
        {
            _mobilePhone["cpu"] = "A10";
        }

        public override void BuildRAM()
        {
            _mobilePhone["ram"] = "2G";
        }

        public override void BuildROM()
        {
            _mobilePhone["rom"] = "128G";
        }
    }

Factory类,提供创建者的接口

    public class Factory
    {
        public void Construct(MobilePhoneBuilder mobilePhoneBuilder)
        {
            mobilePhoneBuilder.BuildCPU();
            mobilePhoneBuilder.BuildRAM();
            mobilePhoneBuilder.BuildROM();
        }
    }

测试代码:

    class Test
    {
        static void Main(string[] args)
        {
            MobilePhoneBuilder builder;
            Factory factory = new Factory();

            builder = new XiaoMIBuilder();
            factory.Construct(builder);
            builder.mobilePhone.Show();

        
            builder = new AppleBuilder();
            factory.Construct(builder);
            builder.mobilePhone.Show();

            Console.Read();
        }
    }

运行结果:

C#设计模式--建造者模式_第3张图片

 

源代码工程文件下载

你可能感兴趣的:(C#程序设计及相关技术,.Net开发及相关技术,设计模式)