Exemplo n.º 1
0
        static void Main()
        {
            IHuman boy = HumanFactory.GetHuman(type: "boy");

            boy.Talk();
            boy.Walk();

            IHuman girl = HumanFactory.GetHuman(type: "girl");

            girl.Talk();
            girl.Walk();
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("欢迎来到.net高级班vip课程,今天是设计模式的学习");
                {
                    IRace race = ObjectFactory.CreateInstance(RaceType.Human);// new Human();
                    race.ShowKing();
                }
                {
                    IRace race = ObjectFactory.CreateInstance(RaceType.ORC);// new Human();
                    race.ShowKing();
                }
                {
                    IRace race = ObjectFactory.CreateInstance(RaceType.NE);// new Human();
                    race.ShowKing();
                }

                {
                    Human human = new Human("123");

                    IFactory facetory = new HumanFactory();
                    IRace    race     = facetory.CreateRace();
                    race.ShowKing();
                }
                {
                    //Human human = new Human();

                    IFactory facetory = new FiveFactory();
                    IRace    race     = facetory.CreateRace();
                    race.ShowKing();
                }
                {
                    AbstractFactory factory = new AHumanFactory();
                    IRace           race    = factory.CreateRace();
                    race.ShowKing();
                    IArmy army = factory.CreateArmy();
                    army.BuildArmy();
                }
                {
                    AbstractFactory factory = new AUndeadFactory();
                    IRace           race    = factory.CreateRace();
                    race.ShowKing();
                    IArmy army = factory.CreateArmy();
                    army.BuildArmy();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            #region 工厂模式
            Console.WriteLine("----------工厂模式---------");
            AbstractHumanFactory <IHuman> humanFactory = new HumanFactory <IHuman>();
            var chinese = humanFactory.CreateHuman("YellowMan");
            Console.WriteLine(chinese.GetColor());
            Console.WriteLine(chinese.Talk());

            var african = humanFactory.CreateHuman("BlackMan");
            Console.WriteLine(african.GetColor());
            Console.WriteLine(african.Talk());

            var american = humanFactory.CreateHuman("WhiteMan");
            Console.WriteLine(american.GetColor());
            Console.WriteLine(american.Talk());
            #endregion
            #region 简单工厂模式调用
            Console.WriteLine("----------简单工厂模式---------");
            chinese = SimpleHumanFactory <IHuman> .CreateHuman("YellowMan");

            Console.WriteLine(chinese.GetColor());
            Console.WriteLine(chinese.Talk());

            african = SimpleHumanFactory <IHuman> .CreateHuman("BlackMan");

            Console.WriteLine(african.GetColor());
            Console.WriteLine(african.Talk());

            american = SimpleHumanFactory <IHuman> .CreateHuman("WhiteMan");

            Console.WriteLine(american.GetColor());
            Console.WriteLine(american.Talk());
            #endregion
            Console.ReadKey();
        }