Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            #region old
            //LeiFeng xueleifeng = new Undergraduate();

            //xueleifeng.BuyRice();
            //xueleifeng.Sweep();
            //xueleifeng.Wash();

            //LeiFeng student1 = new Undergraduate();

            //student1.BuyRice();
            //student1.Sweep();
            //student1.Wash();
            #endregion

            IFactory factory = new UndergraduateFactory();

            LeiFeng student = factory.CreateLeiFeng();

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

            Console.ReadKey();
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            ILeiFengFactory factory = new StudentLeiFengFactory();
            LeiFeng         leifeng = factory.CreateLeiFeng();

            leifeng.WeiFan();
            Console.ReadKey();
        }
Ejemplo n.º 3
0
        private static void NewMethod2()
        {
            LeiFeng stuA = SimpleFactory.CreateLeiFeng("Undergraduate");

            stuA.Sweep();
            LeiFeng volA = SimpleFactory.CreateLeiFeng("Volunteer");

            volA.BuyRice();
            Console.Read();
        }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            IFactory factory = new UndergraduateFactory();
            LeiFeng  student = factory.CreateLeiFeng();

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

            Console.Read();
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 工厂方法模式:是对简单工厂的进一步抽象和推广,他克服了简单工厂违反OCP原则
        /// 工厂方法使得实例化延迟到了子类,
        /// 工厂方法就是把简单工厂的实例化业务放到了他的子类中
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            //如果这里想要用社区志愿者的话StudentFactory变成VolunteerFactory
            ILeiFeng student = new StudentFactory();

            LeiFeng lf = student.CreateLeiFeng();

            lf.Sweep();

            lf.Wash();

            Console.ReadKey();
        }
Ejemplo n.º 6
0
        static void Main(string[] args)
        {
            //NewMethod();
            //NewMethod1();
            //NewMethod2();
            IFactory factory  = new UndergraduateFactory();
            IFactory factory2 = new VolunteerFactory();
            LeiFeng  stu      = factory.CreateLeiFeng();
            LeiFeng  vol      = factory2.CreateLeiFeng();

            stu.Sweep();
            vol.BuyRice();
            Console.ReadLine();
        }
Ejemplo n.º 7
0
        static void Main(string[] args)
        {
            #region 计算器部分
            IFactory  operFactory = new AddFactory();
            Operation oper        = operFactory.CreateOperation();
            oper.NumberA = 1;
            oper.NumberB = 2;
            double result = oper.GetResult();

            Console.WriteLine(result);
            #endregion

            #region 基本方式:薛磊风代表大学生学习雷锋
            LeiFeng xueleifeng = new Undergraduate();

            xueleifeng.BuyRice();
            xueleifeng.Sweep();
            xueleifeng.Wash();

            LeiFeng student1 = new Undergraduate();
            student1.BuyRice();
            LeiFeng student2 = new Undergraduate();
            student2.Sweep();
            LeiFeng student3 = new Undergraduate();
            student3.Wash();
            #endregion

            #region 简单工厂模式
            LeiFeng studentA = SimpleFactory.CreateLeiFeng("学雷锋的大学生");
            studentA.BuyRice();
            LeiFeng studentB = SimpleFactory.CreateLeiFeng("学雷锋的大学生");
            studentB.Sweep();
            LeiFeng studentC = SimpleFactory.CreateLeiFeng("学雷锋的大学生");
            studentC.Wash();
            #endregion

            #region 工厂方法模式
            ILeiFengFactory factory = new UndergraduateFactory();
            LeiFeng         student = factory.CreateLeiFeng();

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

            Console.Read();
            #endregion
        }
Ejemplo n.º 8
0
        public static LeiFeng CreateLeiFeng(string type)
        {
            LeiFeng result = null;

            switch (type)
            {
            case "学雷锋的大学生":
                result = new Undergraduate();
                break;

            case "社区志愿者":
                result = new Volunteer();
                break;

            default:
                break;
            }
            return(result);
        }