标签:public OLE code 解耦 设计 cli 总结 span 目的
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace FactoryMode 8 { 9 // 建造者设计模式 10 11 12 /// <summary> 13 /// 建造者 14 /// </summary> 15 class Institution 16 { 17 18 public void Initiale(AdLinkMotion adLinkMotion) 19 { 20 adLinkMotion.InitialeParameter(); 21 adLinkMotion.Run(); 22 System.Threading.Thread.Sleep(100); 23 adLinkMotion.Stop(); 24 } 25 } 26 27 28 /// <summary> 29 /// 建造者1 30 /// </summary> 31 class InstitutionAGF 32 { 33 34 public void Initiale(AdLinkMotion adLinkMotion) 35 { 36 adLinkMotion.InitialeParameter(); 37 38 39 adLinkMotion.Run(); 40 adLinkMotion.Stop(); 41 System.Threading.Thread.Sleep(100); 42 adLinkMotion.Run(); 43 adLinkMotion.Stop(); 44 } 45 } 46 /// <summary> 47 /// 动作抽象类 48 /// </summary> 49 public abstract class AdLinkMotion 50 { 51 public abstract void InitialeParameter(); 52 public abstract void Run(); 53 public abstract void Stop(); 54 } 55 56 /// <summary> 57 /// 上料机构 58 /// </summary> 59 public class Rack : AdLinkMotion 60 { 61 62 string _strMotionName = ""; 63 public override void InitialeParameter() 64 { 65 _strMotionName = "Rack"; 66 } 67 68 public override void Run() 69 { 70 Console.WriteLine("RackInstruct Runing "); 71 } 72 73 public override void Stop() 74 { 75 Console.WriteLine("RackInstruct Stop "); 76 } 77 } 78 79 /// <summary> 80 /// 搬运机构 81 /// </summary> 82 public class Transter:AdLinkMotion 83 { 84 string _strMotionName = ""; 85 public override void InitialeParameter() 86 { 87 _strMotionName = "Transter"; 88 } 89 90 public override void Run() 91 { 92 Console.WriteLine("TransterInstruct Runing "); 93 } 94 95 public override void Stop() 96 { 97 Console.WriteLine("TransterInstruct Stop "); 98 } 99 } 100 101 /// <summary> 102 /// 旋转机构 103 /// </summary> 104 public class Rotate : AdLinkMotion 105 { 106 string _strMotionName = ""; 107 public override void InitialeParameter() 108 { 109 _strMotionName = "Rotate"; 110 } 111 112 public override void Run() 113 { 114 Console.WriteLine("RotateInstruct Runing "); 115 } 116 117 public override void Stop() 118 { 119 Console.WriteLine("RotateInstruct Stop "); 120 } 121 } 122 } 123 124 Client 使用: 125 126 127 Institution institution = new Institution(); 128 Rack rack = new Rack(); 129 Transter transter = new Transter(); 130 Rotate rotate = new Rotate(); 131 institution.Initiale(rack); 132 institution.Initiale(transter); 133 institution.Initiale(rotate); 134 rack.Run(); 135 transter.Run(); 136 rotate.Run(); 137 138 139 //建造者2 使用 140 InstitutionAGF institutionAGF = new InstitutionAGF(); 141 institutionAGF.Initiale(rack); 142 institutionAGF.Initiale(transter); 143 institutionAGF.Initiale(rotate); 144 Console.ReadKey();
总结:
建造者模式的好处在于:
客户端不需要知道相关类的内部是如何进行何种操作的,只需关心结果或者逻辑的基本流程即可;
如果需要更改逻辑流程,只需在建设者(如代码的institution)内部修改或者新建一个类似的建设者类(institutionAGF)即可实现;
缺点:
如果修改了子类部件的相关,需注意相关的建设者类的内部流程是否符合要求,需跟进实际需求进行更改;
建造者模式的本质是使组装过程(用指挥者类进行封装,从而达到解耦的目的)和创建具体产品解耦,使我们不用去关心每个功能块是如何运行的。
标签:public OLE code 解耦 设计 cli 总结 span 目的
原文地址:https://www.cnblogs.com/SoftZoro20181229/p/12831474.html