工厂要解决的问题就是对象的创建问题。

对象创建有以下几个问题:

1、对象与对象之间的引用,都是用NEW来完成。这是一个多对多的关系,后期如果某一个对象有变化,就需要找到所有引用过该对象的类进行修改。这是一个复杂的工作。

2、对象与对象之间直接引用,就是一种强耦合的关系。当我们增加一种对象(产品),或者是更改一种产品都需要全部引用都要修改。

 

工厂就是把多对象的创建工作集中管理(Factory),这样做有很多好处。可以减少耦合性,类与类之间的引用不再是强依赖关系。

好处:

1、更好管理,(因为对象创建任务都做到一类里面。这样就不用到处找)

2、解除耦合。(通过反射、接口、抽像等解除耦合),使用类与供给类他们之间互不关心。引用类只需要供给类的方法。根本不需要知道,供给者是谁。

3、升级维护。系统要增加一种新的产品、过去需要是改代码,现在只需要复制DLL进去,配置一下就OK了。

代码如下:

使用:

        static void Main(string[] args)
        {
            Demacia demacia = new Demacia();
            demacia.Show();
            Console.WriteLine("-----------------------------");
            IHero ashe = new Ashe();
            ashe.Show();
            Console.WriteLine("-----------------------------");
            IHero tryndamere = Factory.GetInstance(HeroType.Tryndamere);
            tryndamere.Show();

            Console.WriteLine("-----------------------------");
            IHero master = Factory.GetInstanceByConfig();
            master.Show();

            Console.WriteLine("-----------------------------");
            IHero ezreal = Factory.GetInstanceByReflector();
            ezreal.Show();

            Console.ReadLine();

        }

工厂类:

    public class Factory
    {
        /// <summary>
        /// 用枚举来创建的方法,仍然有耦合,使用端必须知道引用哪个子类
        /// </summary>
        /// <param name="heroType"></param>
        /// <returns></returns>
        public static IHero GetInstance(HeroType heroType)
        {
            switch (heroType)
            {
                case HeroType.Demacia:
                    return new Demacia();
                case HeroType.Ashe:
                    return new Ashe();
                case HeroType.Tryndamere:
                    return new Tryndamere();
                case HeroType.Master:
                    return new Master();
                case HeroType.Teemo:
                    return new Teemo();
                default:
                    throw new Exception("类型错误!");
            }
        }

        /// <summary>
        /// 通过配置文件的方式来创建对象,仍然没有屏蔽工厂细节。可以很好使用,但是不能很好的升级
        /// </summary>
        /// <returns></returns>
        public static IHero GetInstanceByConfig()
        {
            string heroTypeConfig = System.Configuration.ConfigurationManager.AppSettings["heroTypeConfig"];

            switch ((HeroType)Enum.Parse(typeof(HeroType), heroTypeConfig))
            {
                case HeroType.Demacia:
                    return new Demacia();
                case HeroType.Ashe:
                    return new Ashe();
                case HeroType.Tryndamere:
                    return new Tryndamere();
                case HeroType.Master:
                    return new Master();
                case HeroType.Teemo:
                    return new Teemo();
                default:
                    throw new Exception("类型错误!");
            }
        }


        /// <summary>
        /// 通过反射来创建对象,可以很好地创建的同时又可以很好地升级。太棒了
        /// </summary>
        /// <returns></returns>
        public static IHero GetInstanceByReflector()
        {
            string heroTypeReflector = System.Configuration.ConfigurationManager.AppSettings["heroTypeReflector"];

            string assemblyName = heroTypeReflector.Split(',')[0];
            string typeName = heroTypeReflector.Split(',')[1];
            return (IHero)Activator.CreateInstance(assemblyName, typeName).Unwrap();
        }

    }

    public enum HeroType
    {
        Demacia,
        Ashe,
        Tryndamere,
        Master,
        Teemo
    }

 

产品类:

    public class Master : IHero
    {
        public void Show()
        {
            Console.WriteLine("易大师");
        }
    }

接口类:

    public interface IHero
    {
        void Show();
    }

 

配置文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>
  <appSettings>
    <add key="heroTypeConfig" value="Master"/>
    <add key="heroTypeReflector" value="SimpleFactory,SimpleFactory.Ezreal"/>
  </appSettings>
</configuration>

 

posted on 2016-05-29 12:05  梦回过去  阅读(267)  评论(0编辑  收藏  举报