Exemplo n.º 1
0
        static void Main(string[] args)
        {
            Console.WriteLine("欢迎大家来到软谋教育.net高级班公开课程");


            Human human2 = new Human();

            human2.ShowKing();
            //IRace human1 = new Human();

            IFactoryMethod humanFactory = new HumanFactory();
            IRace          human        = humanFactory.CreateInstance();

            human.ShowKing();


            //Five five = new Five();

            IFactoryMethod fiveFactory = new FiveFactory();
            IRace          five        = fiveFactory.CreateInstance();

            five.ShowKing();


            Console.Read();
        }
Exemplo n.º 2
0
 static void Main(string[] args)
 {
     try
     {
         {
             Human human = new Human();
             human.ShowKing();
         }
         {
             IRace iRace = new Human();//面向抽象
             iRace.ShowKing();
         }
         #region MyRegion
         {
             IRace iRace = SimpleFactory.CreateInstance(SimpleFactory.RaceType.Human);// new Human();//怎么样更面向抽象
             iRace.ShowKing();
         }
         {
             //可配置
             IRace iRace = SimpleFactory.CreateInstanceConfig();
             iRace.ShowKing();
         }
         {
             //可配置可扩展
             IRace iRace = SimpleFactory.CreateInstanceConfigReflection();
             iRace.ShowKing();
         }
         //走一步看一步
         {
             IRace iRace = SimpleFactory.CreateInstance(SimpleFactory.RaceType.Undead);
         }
         #endregion
         {
             IFactory factory = new HumanFactory();
             //就是为了扩展(mvc扩展IOC就知道了)  为了屏蔽细节
             IRace race = factory.CreateInstance();
         }
         {
             //工厂方法+ 抽象--是必须全部实现的:方便扩展种族 但是不能扩展产品簇--倾斜性可扩展性设计
             AbstractFactoryBase factory = new HumanFactoryAbstract();
             IRace     race     = factory.CreatRace();
             IArmy     army     = factory.CreateArmy();
             IResource resource = factory.CreateResource();
         }
         {
             AbstractFactoryBase factory = new UndeadFactoryAbstract();
             IRace     race     = factory.CreatRace();
             IArmy     army     = factory.CreateArmy();
             IResource resource = factory.CreateResource();
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
     }
     Console.ReadLine();
 }
        // GET: AbstractFactory
        public ActionResult Index()
        {
            //ForzeThroneFactory factory = new ForzeThroneFactory();
            //var human = factory.CreateHuman();//创建对象在工厂里面
            //human.ShowKing();
            ForzeThroneFactory factory = new ForzeThroneFactory();
            IRace huam = factory.CreateFirst();

            huam.ShowKing();
            return(View());
        }
Exemplo n.º 4
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.º 5
0
        static void Main(string[] args)
        {
            Console.WriteLine("欢迎大家来到.net高级班公开课程");

            BaseFactory factory = new FrozenThroneFactory();
            IRace       human   = factory.CreateFirst();

            human.ShowKing();

            BaseFactory sanguoshaFactory = new SanguoshaFactory();
            IRace       qun = sanguoshaFactory.CreateFourth();

            qun.ShowKing();

            Console.ReadKey();
        }
Exemplo n.º 6
0
        static void Main(string[] args)
        {
            //上端直接调用Human,是一一调用,叫紧密耦合,面向细节的
            Human human = new Human();

            human.ShowKing();

            Orc orc = new Orc();

            orc.ShowKing();


            //我们实际上要面向抽象,就是用接口或者抽象类
            // IRace human1 = new Human(); 还是细节的

            //建一个工厂来负责对象的创建
            //好处:在上端是稳定的,上端和下端是通过接口依赖的,不用处理细节,不用创建对象
            //缺点:耦合移到了工厂,增加时还是要修改工厂,工厂还是高度耦合的,矛盾转移了,严重违背了设计模式的单一职责
            //上端还是指定了参数
            IRace human2 = Factory.CreateInstance(Factory.RaceType.Human);

            human2.ShowKing();



            IRace undead = Factory.CreateInstance(Factory.RaceType.Undead);

            undead.ShowKing();


            //简单工厂的进阶+配置文件 实现程序的可配置
            Console.WriteLine("*********************CreateInstanceConfig**********************");
            IRace iRace = Factory.CreateInstanceConfig();

            iRace.ShowKing();

            //简单工厂的进阶+反射,实现程序的可配置,可扩展  在上端和工厂里都没有细节,可扩展的如果加一个新的类,把新的项目生成的dll放进此项目,就可以用了
            Console.WriteLine("*********************CreateInstanceReflect**********************");
            IRace iRace2 = Factory.CreateInstanceReflect();

            iRace2.ShowKing();

            Console.Read();
        }
Exemplo n.º 7
0
        static void Main(string[] args)
        {
            Console.WriteLine("这里是魔兽世界");

            #region 原始创建法
            Console.WriteLine("****************原始创建法*****************");
            //创建人类对象
            Human original_human = new Human();
            original_human.ShowKing();
            //创建兽类对象
            Orc original_orc = new Orc();
            original_orc.ShowKing();
            #endregion

            #region 接口创建法
            Console.WriteLine("****************接口创建法*****************");
            //创建人类对象
            IRace interface_human = new Human();
            interface_human.ShowKing();
            #endregion

            #region 工厂参数创建法
            Console.WriteLine("****************工厂参数创建法*****************");
            //创建人类对象
            IRace factory_human = Factory.CreateInstance(Factory.RaceType.Human);
            factory_human.ShowKing();
            #endregion

            #region 工厂配置文件创建法
            Console.WriteLine("****************工厂配置文件创建法*****************");
            IRace factory_config_human = Factory.CreateInstanceConfig();
            factory_config_human.ShowKing();
            #endregion

            #region 工厂配置文件反射创建法
            Console.WriteLine("****************工厂配置文件反射创建法*****************");
            IRace factory_reflect_human = Factory.CreateInstanceReflect();
            factory_reflect_human.ShowKing();
            #endregion

            Console.Read();
        }
Exemplo n.º 8
0
        static void Main(string[] args)
        {
            //HumanFactory humanFanctory = new HumanFactory();
            //IRace human = humanFanctory.CreateInstance();
            // human.ShowKing();
            //抽象是稳定的
            //解决了简单模式中,下端和工厂的高度耦合,任何一个类变化都要去修改工厂,严重违反了单一职责,工厂类就变得不稳定
            //humanFactory==IFactoryMethod
            //human==IRace
            //工厂方法就是把简单工厂类的东西拆开,一个对象造一个工厂用来创建对象,也可以进行扩展
            //开闭原则:对扩展开放,对修改封闭
            //要求我们的上端不出现Human 这种细节,才是面向抽象的,才是可扩展的
            //缺陷:会让代码使用起来很麻烦
            IFactoryMethod humanFanctory = new HumanFactory();
            IRace          human         = humanFanctory.CreateInstance();

            human.ShowKing();

            Console.Read();
            //如果第五种族,只需要增加新的类,新的类的工厂,只要是增加,就是对扩展开放
        }
Exemplo n.º 9
0
        static void Main(string[] args)
        {
            Console.WriteLine("欢迎大家来到软谋教育.net高级班公开课程");

            Human human2 = new Human();

            human2.ShowKing();

            IRace human1 = new Human();

            IRace human = Factory.CreateInstance(Factory.RaceType.Human);

            human.ShowKing();

            //Orc orc = new Orc();
            //IRace orc = new Orc();
            IRace orc = Factory.CreateInstance(Factory.RaceType.Orc);

            orc.ShowKing();

            IRace undead = Factory.CreateInstance(Factory.RaceType.Undead);

            undead.ShowKing();

            Console.WriteLine("*********************CreateInstanceConfig**********************");
            IRace iRace = Factory.CreateInstanceConfig();

            iRace.ShowKing();



            Console.WriteLine("*********************CreateInstanceReflect**********************");
            IRace iRace2 = Factory.CreateInstanceReflect();

            iRace2.ShowKing();
            Console.Read();
        }
Exemplo n.º 10
0
        /// <summary>
        /// 简单工厂
        /// 设计模式是为了解决一类的问题,不同的设计模式解决不同的问题
        /// </summary>
        /// <returns></returns>
        // GET: SimpleFactory
        public ActionResult Index()
        {
            ///前端可以输入 “fistname lastname”或者“lastname,firstname”
            string  name     = "";//模拟前端输入的姓名
            GetName getname  = NameFactory.CreateNameFactory(name);
            var     firtName = getname.getFirstName();
            var     lastName = getname.getLastName();

            //1、这种写法 上下端耦合 面向细节
            Human human = new Human();

            human.ShowKing();
            Orc orc = new Orc();

            orc.ShowKing();
            //目标面向抽象 屏蔽细节
            //这种方式还有细节
            IRace ihuman = new Human();

            ihuman.ShowKing();

            //这种属于简单工厂
            IRace humanFactory = Factory.CreateFactory(RaceType.Human);

            humanFactory.ShowKing();

            //web.cong指令
            IRace iRace = Factory.CreateFactoryConfig();

            iRace.ShowKing();
            //反射
            IRace Irace = Factory.CreateInstance();

            Irace.ShowKing();
            return(View());
        }
Exemplo n.º 11
0
        static void Main(string[] args)
        {
            //简单工厂,创建实例的方法有很多职责,违背了单一原则
            //于是我们拆成了工厂方法,每个类一个工厂
            //拆成每个方法

            //FrozenThroneFactory factory = new FrozenThroneFactory();
            //IRace human = factory.CreateHuman();
            //human.ShowKing();


            //只有一个工厂的职责是稳定的时候才使用
            //缺陷:如果增加第五类,每个继承的工厂都要进行重构变化
            BaseFactory frozenFactory = new FrozenThroneFactory();
            IRace       human         = frozenFactory.CreateFirst();

            human.ShowKing();

            BaseFactory sanguoshaFactory = new SanGuoShaFactory();
            IRace       qun = sanguoshaFactory.CreateForth();

            qun.ShowKing();
            Console.Read();
        }
Exemplo n.º 12
0
 public void PlayWar3(IRace war3)
 {
     Console.WriteLine("{0} play wa3", this.Name);
     war3.ShowKing();
 }
Exemplo n.º 13
0
 /// <summary>
 /// 面向抽象
 /// </summary>
 /// <param name="race"></param>
 public void PlayWar3(IRace race)
 {
     Console.WriteLine("******************************");
     Console.WriteLine("This is {0} Play War3.{1}", this.Name, race.GetType().Name);
     race.ShowKing();
 }
Exemplo n.º 14
0
 public void PlayWar3(IRace race)
 {
     race.ShowKing();
     Console.WriteLine($"PlayWar3 is playing {nameof(race)}");
 }