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

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

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

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

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

设计模式:

建造者模式(Builder Pattern)

简单介绍:

建造者模式参与者:

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

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

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

    ° 提供一个检索Product的接口

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

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

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

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

建造者模式类图:

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

 Product类 产品类

 1 public class Product
 2     {
 3         private List<string> _parts = new List<string>();
 4 
 5         public void Add(string part)
 6         {
 7             _parts.Add(part);
 8         }
 9 
10         public void Show()
11         {
12             Console.WriteLine("Product Parts");
13             foreach (string part in _parts)
14             {
15                 Console.WriteLine(part);
16             }
17         }
18     }

Builder类 抽象接口类

1     /// <summary>
2     /// 为创建一个Product对象的各个部件指定抽象接口
3     /// </summary>
4     public abstract class Builder
5     {
6         public abstract void BuildPart();
7         public abstract Product GetResult();
8     }

ConcreteBuilder类 具体创建者类

 1  public class ConcreteBuilder : Builder
 2     {
 3         private Product _product = new Product();
 4 
 5         public override void BuildPart()
 6         {
 7             _product.Add("Part");
 8         }
 9 
10         public override Product GetResult()
11         {
12             return _product;
13         }
14     }

Director类 提供使用Builder类的接口

 1 public class Director  
 2     {  
 3         /// <summary>  
 4         /// 构造一个使用Builder接口的对象  
 5         /// </summary>  
 6         public void Construct(Builder builder)  
 7         {  
 8             builder.BuildPart();  
 9         }  
10     }  

 

测试代码:

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();
        }
    }

运行结果:

 

源代码工程文件下载

实例举例,手机生产举例  

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

 

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

MobilePhone类产品类 

 1 public class MobilePhone
 2     {
 3         private string _brand;
 4         private Dictionary<string, string> _Info = new Dictionary<string, string>();
 5 
 6         public string this[string key]
 7         {
 8             get { return _Info[key]; }
 9             set { _Info[key] = value; }
10         }
11         public MobilePhone(string brand)
12         {
13             this._brand = brand;
14         }
15         public void Show()
16         {
17             Console.WriteLine("\n-----Info--------");
18             Console.WriteLine(" BRAND: {0}", _brand);
19             Console.WriteLine(" CPU  : {0}", _Info["cpu"]);
20             Console.WriteLine(" RAM  : {0}", _Info["ram"]);
21             Console.WriteLine(" ROM  : {0}", _Info["rom"]);
22         }

MobilePhoneBuilder类 创建者抽象类

 1  public abstract class MobilePhoneBuilder
 2     {
 3         protected MobilePhone _mobilePhone;
 4 
 5        
 6         public MobilePhone mobilePhone
 7         {
 8             get { return _mobilePhone; }
 9         }
10 
11         // Abstract build methods
12         public abstract void BuildCPU();
13         public abstract void BuildRAM();
14         public abstract void BuildROM();
15         
16     }

XiaoMIBuilder类 小米手机创建者类

 1 public class XiaoMIBuilder:MobilePhoneBuilder
 2     {
 3         public XiaoMIBuilder()
 4         {
 5             _mobilePhone = new MobilePhone("小米5s");
 6         }
 7         public override void BuildCPU()
 8         {
 9             _mobilePhone["cpu"] = "骁龙821";
10         }
11 
12         public override void BuildRAM()
13         {
14             _mobilePhone["ram"] = "4G";
15         }
16 
17         public override void BuildROM()
18         {
19             _mobilePhone["rom"] = "32G";
20         }
21     }

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

 1  public class AppleBuilder:MobilePhoneBuilder
 2     {
 3         public AppleBuilder()
 4         {
 5             _mobilePhone = new MobilePhone("iphone7s plus");
 6         }
 7         public override void BuildCPU()
 8         {
 9             _mobilePhone["cpu"] = "A10";
10         }
11 
12         public override void BuildRAM()
13         {
14             _mobilePhone["ram"] = "2G";
15         }
16 
17         public override void BuildROM()
18         {
19             _mobilePhone["rom"] = "128G";
20         }
21     }

Factory类,提供创建者的接口

1 public class Factory
2     {
3         public void Construct(MobilePhoneBuilder mobilePhoneBuilder)
4         {
5             mobilePhoneBuilder.BuildCPU();
6             mobilePhoneBuilder.BuildRAM();
7             mobilePhoneBuilder.BuildROM();
8         }
9     }

测试代码:

 1 class Test
 2     {
 3         static void Main(string[] args)
 4         {
 5             MobilePhoneBuilder builder;
 6             Factory factory = new Factory();
 7 
 8             builder = new XiaoMIBuilder();
 9             factory.Construct(builder);
10             builder.mobilePhone.Show();
11 
12         
13             builder = new AppleBuilder();
14             factory.Construct(builder);
15             builder.mobilePhone.Show();
16 
17             Console.Read();
18         }
19     }

运行结果:

 

源代码工程文件下载

posted @ 2017-02-19 23:16  JiYF  阅读(1601)  评论(0编辑  收藏  举报