/// <summary> /// 工厂模式方法 /// </summary> static void TestFactoryFunction() { FactoryFunction.IFactory operFactory = new AddFactory(); FactoryFunction.Operation oper = operFactory.CreateOperation(); oper.NumA = 1; oper.NumB = 2; Console.WriteLine(oper.GetResult()); }
/// <summary> ///工厂模式 客户端实现代码 /// </summary> void Factory_main() { /// 实现接口 IFactory OperaFatory = new AddFactory(); // 客户端 根据需要 选择相应的工厂 Operation opera = OperaFatory.CreateOperation(); opera.NumberA = 1; opera.NumberB = 2; double result = opera.GetResult(); // 计算出结果 }
public void OperationAddTestMethod() { IFactory factory = new AddFactory(); var operation = factory.CreateOperation(); operation.NumberA = 99; operation.NumberB = 10; var result = operation.GetResult(); Assert.AreEqual(99 + 10, result); }
static void Main(string[] args) { //观察者模式 Blog xmf = new MyBlog("苏翅", $"发表了一张照片,点击链接查看!"); SubScriber wnn = new SubScriber("王妮娜"); SubScriber tmr = new SubScriber("唐马儒"); SubScriber wmt = new SubScriber("王蜜桃"); SubScriber anm = new SubScriber("敖尼玛"); // 添加订阅者 xmf.AddObserver(new NotifyEventHandler(wnn.Receive)); xmf.AddObserver(new NotifyEventHandler(tmr.Receive)); xmf.AddObserver(new NotifyEventHandler(wmt.Receive)); xmf.AddObserver(new NotifyEventHandler(anm.Receive)); xmf.Update(); Console.WriteLine(); Console.WriteLine(); Console.WriteLine(); Console.WriteLine(); xmf.RemoveObserver(new NotifyEventHandler(wnn.Receive)); Console.WriteLine($"移除订阅者{wnn.Name}"); xmf.Update(); Console.ReadKey(); return; IOprFactory oprsub = new SubFactory(); IOprFactory opradd = new AddFactory(); Opreator otsub = oprsub.CreateOperation(); otsub.A = 33; otsub.B = 22; Opreator otadd = opradd.CreateOperation(); otadd.A = 8; otadd.B = 16; Trace.WriteLine($"SUB:{otsub.A}-{otsub.B}={otsub.GetResult()}"); Trace.WriteLine($"ADD:{otadd.A}+{otadd.B}={otadd.GetResult()}"); //A a = new A("1"); //A aa = new A() { b = "1" }; //Console.WriteLine(aa.b + a.b); //Console.ReadKey(); //return; IDoctor d = new DoctorProvider(); Trace.WriteLine($"old driver name is { d.GetDoctorName()} and {d.GetDoctorAge()} age now!!"); }
static void Main(string[] args) { // 获取具体方法接口,并实例化具体运算方法 IFactory operFactory = new AddFactory(); Operation oper = operFactory.CreateOperation(); // 具体运算 oper.NumberA = 1; oper.NumberB = 2; double result = oper.GetResult(); Console.WriteLine(result); Console.Read(); }
public static void SimpleFactoryPattern() { Operation oper; oper = OperationFactory.createOperate("+"); oper.NumberA = 1; oper.NumberB = 2; double result = oper.GetResult(); Console.WriteLine(result); SimpleFactoryPattern.IFactory opearFactory = new AddFactory(); Operation opesr = opearFactory.CreateOperation(); opesr.NumberA = 1; opesr.NumberB = 2; double results = opesr.GetResult(); }
static void Main(string[] args) { IFactory operFactory = new AddFactory(); Operation oper = operFactory.CreateOperation(); oper.NumberA = 1; oper.NumberB = 2; double result = oper.GetResult(); Console.WriteLine(result); /* * 目前看不出 Factory Method Pattern 的好處在哪, * 因為沒有了 Simple Factory Pattern 把邏輯判斷給包裝起來, * 但好像有 Strategy Pattern 的切換性,只要加上條件判斷就可以切換了, * 因為已經有 Operation 這個父類別,但具體差異在哪還說不出來 * [實際上 Strategy Pattern 對於使用者來說會更方便, * 因為使用者之需要用context class 去進行宣告(new出底層的物件給context)與操作] * 而回過頭看, Simple Factory Pattern 每次新增條件都必須在內部的判斷式中加入新的case判斷條件, * 會違反開放閉合原則,所以才會有 Factory Method Pattern 的出現 * 往後有新增新的類別,只需要做出對應的Factory Class, * 並改動 IFactory operFactory = new AddFactory(); 這行的new就可以改變了。 */ }