public void OperationSubTestMethod() { IFactory factory = new SubFactory(); var operation = factory.CreateOperation(); operation.NumberA = 99; operation.NumberB = 10; var result = operation.GetResult(); Assert.AreEqual(99 - 10, result); }
static void FactoryPattern() { Console.WriteLine($"==========FactoryPattern========="); IFactory factory = new SubFactory(); Operator operat = factory.CreaterOperator(); operat.FirstNumber = 10; operat.SecondNumber = 30; Console.WriteLine(operat.GetResult()); }
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 plus = new PlusFactory(); Operation opera = plus.Init(); Console.WriteLine(opera.GetResult(10, 20.2)); plus = new SubFactory(); opera = plus.Init(); Console.WriteLine(opera.GetResult(100, 20.2)); Console.WriteLine("--------------------- 以下是一个N次方功能的扩展 -----------------"); Console.WriteLine("添加一个继承Operation抽象类的NPowerOperation子类并实现求N次方的功能"); Console.WriteLine("再添加一个实现IFactory接口的NPowerFactory工厂类,其目的是实例化NPowerOperation类"); plus = new NPowerFactory(); opera = plus.Init(); Console.WriteLine("效果:2的4次方减1是 {0}", opera.GetResult(2, 4)); Console.Read(); }