Пример #1
0
 /// <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());
 }
Пример #2
0
 static void main(string[] args)
 {
     IFactory operFactory = new AddFactory();
     UserBase oper = operFactory.CreateUser();
     oper.NumberA = 1;
     oper.NumberB = 2;
     double result = oper.GetResult();
 }
Пример #3
0
        public string Add(double numberA, double numberB)
        {
            IOperatorFactory add         = new AddFactory();
            Operator         addOperator = add.CreareOperator();

            addOperator.NumberA = numberA;
            addOperator.NumberB = numberB;
            return(addOperator.GetResult().ToString());
        }
Пример #4
0
        public static double Add()
        {
            double           operand1    = ReciveOperands("Operand1");
            double           operand2    = ReciveOperands("Operand2");
            OperatorsFactory addFactory  = new AddFactory();
            Operator         addOperator = addFactory.Create();

            return(addOperator.Calculate(operand1, operand2));
        }
Пример #5
0
        static void main(string[] args)
        {
            IFactory operFactory = new AddFactory();
            UserBase oper        = operFactory.CreateUser();

            oper.NumberA = 1;
            oper.NumberB = 2;
            double result = oper.GetResult();
        }
Пример #6
0
    /// <summary>
    ///工厂模式  客户端实现代码
    /// </summary>
    void Factory_main()
    {
        /// 实现接口
        IFactory  OperaFatory = new AddFactory(); // 客户端 根据需要  选择相应的工厂
        Operation opera       = OperaFatory.CreateOperation();

        opera.NumberA = 1;
        opera.NumberB = 2;
        double result = opera.GetResult();  // 计算出结果
    }
Пример #7
0
        public void FactoryPatternCodeTest()
        {
            var factory    = new AddFactory();
            var opereation = factory.CreateOpereation();

            opereation.NumbleA = 100;
            opereation.NumbleB = 10.1;
            var result = opereation.GetResult();

            Console.WriteLine(result);
        }
        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);
        }
Пример #9
0
 protected override void CustomProcessRequest(HttpContext context)
 {
     try
     {
         AddFactory   factory     = new AddFactory(context);
         IAddBehavior addBehavior = factory.Create();
         addBehavior.Add();
     }
     catch (Exception ex)
     {
         ContextHelper contextHelper = new ContextHelper(context);
         contextHelper.ResponseError(ex.Message);
     }
 }
Пример #10
0
        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!!");
        }
Пример #11
0
    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();
    }
Пример #12
0
        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();
        }
Пример #13
0
        private bool CheckFactoryListExist(List <string> factoryList)
        {
            FactoryList = FactoryModule.GetFactoryIdentiys(factoryList);
            List <string> remainFactoryList = new List <string>();

            remainFactoryList = factoryList.Where(w => !FactoryList.ToList().Select(s => s.Name).Contains(w)).ToList();
            if (remainFactoryList.Count() > 0)
            {
                MessageBox.Show(string.Concat("以下工廠尚未存在於清單:\n", string.Join(",", remainFactoryList), "\n點選確認將跳轉至新增工廠頁面!!"));
                var addFactory = new AddFactory
                {
                    DataContext = new AddFactoryViewModel {
                        Name = remainFactoryList.First()
                    }
                };
                this.NavigationService.Navigate(addFactory);
                return(false);
            }
            return(true);
        }
Пример #14
0
        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就可以改變了。
             */
        }