说明:构建一个大对象时,可以分解成一个部分一个部分的构建,比如一台电脑由CUP、内存、主板、屏幕等,这些配件本身就是一个复杂的制造过程,一个一个构建后然后才组装成一台新的电脑。 步骤 1、定义要构建的对象——电脑 2、定义一个抽象的构建器,内部的每个抽象方法构建一个配件并返回构建器本身,目的是为了链式调用 3、构建器具体实现,如:三星构建器、戴尔构建器, 4、构建复杂对象是需要有顺序的,比如有些配件必须先构建,这时需要一个导演来指挥整个构建顺序 5、客户端使用构建器。创建一个导演实例和一个具体构建器实例如三星,然后把构建实例对象丢给导演去构建出电脑。
1 var director = new Director(); 2 var sanxingBuilder = new SanxingBuilder(); 3 director.Build(sanxingBuilder); 4 var sanxingComputer = sanxingBuilder.GetComputer(); 5 Console.WriteLine(sanxingComputer); 6 7 var dellBuilder = new DellBuilder(); 8 director.Build(dellBuilder); 9 var dellComputer = dellBuilder.GetComputer(); 10 Console.WriteLine(dellComputer); 11 12 //1、定义要构建的对象 电脑 13 public class Computer 14 { 15 public string Cup { get; set; } = string.Empty; 16 public string Memory { get; set; } = string.Empty; 17 public string Mainbord { get; set; } = string.Empty; 18 public string Screen { get; set; } = string.Empty; 19 20 public override string ToString() 21 { 22 return $"Cup:{Cup},Memory:{Memory},Mainbord:{Mainbord},Screen:{Screen}"; 23 } 24 } 25 26 //2、定义一个抽象构建器 27 public abstract class Builder 28 { 29 public abstract Builder BuildCpu();//返回类型是自己(Builder),这样就支持链式调用 30 public abstract Builder BuildMemory(); 31 public abstract Builder BuildMainbord(); 32 public abstract Builder BuildScreen(); 33 34 public abstract Computer GetComputer(); 35 } 36 37 //3、实现构建起 三星 38 public class SanxingBuilder : Builder 39 { 40 private readonly Computer _computer = new Computer(); 41 42 public override Builder BuildCpu() 43 { 44 _computer.Cup = "三星 CUP"; 45 Console.WriteLine(_computer.Cup); 46 return this; 47 } 48 49 public override Builder BuildMainbord() 50 { 51 _computer.Mainbord = "三星 CUP"; 52 Console.WriteLine(_computer.Mainbord); 53 return this; 54 } 55 56 public override Builder BuildMemory() 57 { 58 _computer.Memory = "三星 Memory"; 59 Console.WriteLine(_computer.Memory); 60 return this; 61 } 62 63 public override Builder BuildScreen() 64 { 65 _computer.Screen = "三星 Screen"; 66 Console.WriteLine(_computer.Screen); 67 return this; 68 } 69 70 public override Computer GetComputer() 71 { 72 return _computer; 73 } 74 } 75 76 //3实现 构建者 dell 77 public class DellBuilder : Builder 78 { 79 private readonly Computer _computer = new Computer(); 80 public override Builder BuildCpu() 81 { 82 _computer.Cup = "戴尔 CUP"; 83 Console.WriteLine(_computer.Cup); 84 return this; 85 } 86 87 public override Builder BuildMainbord() 88 { 89 _computer.Mainbord = "戴尔 Mainbord"; 90 Console.WriteLine(_computer.Mainbord); 91 return this; 92 } 93 94 public override Builder BuildMemory() 95 { 96 _computer.Memory = "戴尔 Memory"; 97 Console.WriteLine(_computer.Memory); 98 return this; 99 } 100 101 public override Builder BuildScreen() 102 { 103 _computer.Screen = "戴尔 Screen"; 104 Console.WriteLine(_computer.Screen); 105 return this; 106 } 107 108 public override Computer GetComputer() 109 { 110 return _computer; 111 } 112 } 113 114 //4、导演角色、编排构建顺序 115 public class Director 116 { 117 public void Build(Builder builder) 118 { 119 builder 120 .BuildCpu() 121 .BuildMainbord() 122 .BuildMemory() 123 .BuildScreen(); 124 } 125 }
标签:return,C#,Builder,computer,构建,override,设计模式,public From: https://www.cnblogs.com/wanghaibin/p/17342783.html