寒假帝释天

代理模式

通过一个虚拟类,两个类,第一个类(里面有实用的方法),第二个类(通过实例化(第一个类,然后用实例化的对象调用第一个类中的方法),,,大概就是这么个意思 

工厂方法模式

通过一个接口(定义一个方法), 多个类继承该接口,这些类(调用实际方法类)

   interface IFactory      //////定义一个接口
    {
        Operation CreateOperation();
    }

 class AddFactory : IFactory  /////继承该接口,里面方法调用另一个类
    {
        public Operation CreateOperation()
        {
            return new OperationAdd();
        }
    } 

 class OperationAdd : Operation //////被调用的方法类
    {
        public override double GetResult()
        {
            double result = 0;
            result = NumberA + NumberB;
            return result;
        }
    } 

 class Operation
    {
        private double _numberA = 0;
        private double _numberB = 0;

        public double NumberA
        {
            get { return _numberA; }
            set { _numberA = value; }
        }

        public double NumberB
        {
            get { return _numberB; }
            set { _numberB = value; }
        }

        /// <summary>
        /// 得到运算结果
        /// </summary>
        /// <returns></returns>
        public virtual double GetResult()
        {
            double result = 0;
            return result;
        }
    }

///////////////////////main方法//////////////////////

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();
        }

 

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\雷锋工厂

 一个被继承的方法类

 class LeiFeng
    {
        public void Sweep()
        {
            Console.WriteLine("扫地");
        }

        public void Wash()
        {
            Console.WriteLine("洗衣");
        }

        public void BuyRice()
        {
            Console.WriteLine("买米");
        }
    }
 interface IFactory              //////////////接口,里面实例化一个leifeng的方法
    {
        LeiFeng CreateLeiFeng();
    }
class UndergraduateFactory : IFactory //////////////继承接口IFactory ,里面方法名和接口方法名一样
    {
        public LeiFeng CreateLeiFeng()
        {
            return new Undergraduate();  /////返回要实例化的方法
        }
    }

    //社区志愿者工厂
    class VolunteerFactory : IFactory
    {
        public LeiFeng CreateLeiFeng()
        {
            return new Volunteer();
        }
    }
 class Undergraduate : LeiFeng
    { }

    //社区志愿者
    class Volunteer : LeiFeng
    { }
   //工厂方法模式
            IFactory factory = new UndergraduateFactory();
            LeiFeng student = factory.CreateLeiFeng();

            student.BuyRice();
            student.Sweep();
            student.Wash();



 

 

 

 

你可能感兴趣的:(寒假帝释天)