static void MethodA()
        {
            ProductFactory productFactory = new ProductFactory();
            WrapFactory    wrapFactory    = new WrapFactory();

            // 直接调用
            Box boxA = wrapFactory.WrapBox(productFactory.MakePizza);
            Box boxB = wrapFactory.WrapBox(productFactory.MakePen);
            Box boxC = wrapFactory.WrapBox(productFactory.MakeToyCar);

            Console.WriteLine(boxA.Prouduct.Name);
            Console.WriteLine(boxB.Prouduct.Name);
            Console.WriteLine(boxC.Prouduct.Name);
        }
        static void MethodB()
        {
            ProductFactory productFactory = new ProductFactory();
            WrapFactory    wrapFactory    = new WrapFactory();

            // 间接调用
            Func <Product> makePizza  = new Func <Product>(productFactory.MakePizza);
            Func <Product> makePen    = new Func <Product>(productFactory.MakePen);
            Func <Product> makeToyCar = new Func <Product>(productFactory.MakeToyCar);

            Box boxA = wrapFactory.WrapBox(makePizza);
            Box boxB = wrapFactory.WrapBox(makePen);
            Box boxC = wrapFactory.WrapBox(makeToyCar);

            Console.WriteLine(boxA.Prouduct.Name);
            Console.WriteLine(boxB.Prouduct.Name);
            Console.WriteLine(boxC.Prouduct.Name);
        }
Esempio n. 3
0
        static void Main(string[] args)
        {
            Calculator calculator = new Calculator();
            Calc       calc1      = new Calc(calculator.Add);
            Calc       calc2      = new Calc(calculator.Sub);
            Calc       calc3      = new Calc(calculator.Mul);
            Calc       calc4      = new Calc(calculator.Div);

            double a = 100;
            double b = 200;
            double c = 0;

            c = calc1.Invoke(a, b);
            Console.WriteLine(c);
            c = calc2.Invoke(a, b);
            Console.WriteLine(c);
            c = calc3.Invoke(a, b);
            Console.WriteLine(c);
            c = calc4.Invoke(a, b);
            Console.WriteLine(c);


            //ProductFactory productFactory = new ProductFactory();
            IProductFactory pizzaFactory  = new PizzaFactory();
            IProductFactory toycarFactory = new ToyCarFactory();
            WrapFactory     wrapFactory   = new WrapFactory();

            //Func<Product> func1 = new Func<Product>(productFactory.MakePizza);
            //Func<Product> func2 = new Func<Product>(productFactory.MakeToyCar);

            Logger           logger = new Logger();
            Action <Product> log    = new Action <Product>(logger.Log);

            //Box box1 = wrapFactory.WrapProduct(func1, log);
            //Box box2 = wrapFactory.WrapProduct(func2, log);
            Box box1 = wrapFactory.WrapProduct(pizzaFactory, log);
            Box box2 = wrapFactory.WrapProduct(toycarFactory, log);

            Console.WriteLine(box1.Product.Name);
            Console.WriteLine(box2.Product.Name);
        }
        static void Main(string[] args)
        {
            ProductFactory productFactory = new ProductFactory();
            WrapFactory    wrapFactory    = new WrapFactory();

            // 间接调用
            Func <Product> makePizza  = new Func <Product>(productFactory.MakePizza);
            Func <Product> makePen    = new Func <Product>(productFactory.MakePen);
            Func <Product> makeToyCar = new Func <Product>(productFactory.MakeToyCar);

            Logger           logger = new Logger();
            Action <Product> log    = new Action <Product>(logger.Log);

            Box boxA = wrapFactory.WrapBox(makePizza, log);
            Box boxB = wrapFactory.WrapBox(makePen, log);
            Box boxC = wrapFactory.WrapBox(makeToyCar, log);

            Console.WriteLine(boxA.Prouduct.Name);
            Console.WriteLine(boxB.Prouduct.Name);
            Console.WriteLine(boxC.Prouduct.Name);

            Console.ReadKey();
        }