• 简单工厂之见解设计模式


         不好意思了各位,由于最近换工作的事情导致忙不过了,没有上来继续完善这个章节的内容,现在继续写。

        通过写上一张单例模式之见解-设计模式,我才发现即使是一个很简单的技术,想要以自己知识来完整的表达而且他人还能看懂掌握切实是难度大啊。这节就是讲简单工厂跟抽象工厂算起来这个简单好多,现在大部分人达到这个目的都使用一些依赖反转技术去实现,基本就是配置下,然后就一句话。下面慢慢来讲下:

    View Code
        /// <summary>
        /// MSSQL仓储
        /// </summary>
        public class MSSQLRespository 
        {
            public void Insert()
            {
                Console.WriteLine("MSSQL操作:增加");
            }
    
            public void Delete()
            {
                Console.WriteLine("MSSQL操作:删除");
            }
    
            public void Updata()
            {
                Console.WriteLine("MSSQL操作:更新");
            }
        }
    
        /// <summary>
        /// ORACLE仓储
        /// </summary>
        public class ORACLERespository 
        {
            public void Insert()
            {
                Console.WriteLine("ORACLE操作:增加");
            }
    
            public void Delete()
            {
                Console.WriteLine("ORACLE操作:删除");
            }
    
            public void Updata()
            {
                Console.WriteLine("ORACLE操作:更新");
            }
        }

         上面代码有2个数据库的仓储,现在我如果要使用第一个MSSQL的话,那么代码可能直接new实例化对象再调用,如下:

    1             //第一种原始方法实现
    2             MSSQLRespository mres = new MSSQLRespository();
    3             mres.Insert();
    4             mres.Delete();
    5             mres.Updata();

    现在大家就可能会有个想法:如果现在我要ORACLE的呢,那没有办法也直接new了。

    1             ORACLERespository ores = new ORACLERespository();
    2             ores.Insert();
    3             ores.Delete();
    4             ores.Updata();

    这样就再编译一次代码再发布,可以想象这样不是很方面哦,有没有一种方法就是我需要再编译的,直接修改某个地方就能达到这个目的,有吗,那么现在这个简单工厂就可以上场了,

    原理先说下,通过抽象封装下这个仓储行为,再配置文件把相关的程序集已经类名配置好,最后通过.NET反射技术实例化对象,代码如下:

        /// <summary>
        /// 仓储接口
        /// </summary>
        public interface IRepository
        {
            /// <summary>
            ////// </summary>
            void Insert();
    
            /// <summary>
            ////// </summary>
            void Delete();
    
            /// <summary>
            ////// </summary>
            void Updata();
    
        }
    
        /// <summary>
        /// MSSQL仓储
        /// </summary>
        public class MSSQLRespository : IRepository
        {
            public void Insert()
            {
                Console.WriteLine("MSSQL操作:增加");
            }
    
            public void Delete()
            {
                Console.WriteLine("MSSQL操作:删除");
            }
    
            public void Updata()
            {
                Console.WriteLine("MSSQL操作:更新");
            }
        }
    
        /// <summary>
        /// ORACLE仓储
        /// </summary>
        public class ORACLERespository : IRepository
        {
            public void Insert()
            {
                Console.WriteLine("ORACLE操作:增加");
            }
    
            public void Delete()
            {
                Console.WriteLine("ORACLE操作:删除");
            }
    
            public void Updata()
            {
                Console.WriteLine("ORACLE操作:更新");
            }
        }
    
        #endregion

    .NET反射代码如下:

        #region 使用反射创建对象,也可以使用依赖反转技术实现(类似)
    
        public sealed class SimpleFactory
        {
            private static readonly string repositionSetting;
            private static readonly string assemblySetting;
    
            static SimpleFactory()
            {
                assemblySetting = System.Configuration.ConfigurationManager.AppSettings["AssemblySetting"];
                repositionSetting = System.Configuration.ConfigurationManager.AppSettings["RepositionSetting"];
            }
            public static IRepository GetInstance
            {
                get
                {
                    return (IRepository)System.Reflection.Assembly.Load(assemblySetting).CreateInstance(repositionSetting);
                }
            }
        }
    
        #endregion

    最后配置文件以及调用下:

      <appSettings>    
        <add key="AssemblySetting" value="SimpleFactoryPattern"/>
        <add key="RepositionSetting" value="SimpleFactoryPattern.ORACLERespository"/>    
      </appSettings>
                //第二种简单工厂模式实现
                var res = SimpleFactory.GetInstance;
    
                res.Insert();
                res.Delete();
                res.Updata();
    
    
                Console.Read();

    哈哈,就这样简单吧,输出就:

    ORACLE操作:增加
    ORACLE操作:删除
    ORACLE操作:更新

    因为如此简单所以叫简单工厂!整个代码下载

  • 相关阅读:
    关于公司电脑修改host文件无法生效的问题
    Cannot access nexus-aliyun (http://maven.aliyun.com/nexus/content/groups/public) in offline mode and the artifact org.springframework
    DCL-单例模式的线程安全
    关于volatile
    关于CAS中的ABA问题存在的隐患
    无法获取 dpkg 前端锁 (/var/lib/dpkg/lock-frontend),是否有其他进程正占用它
    vue整合笔记
    vue9-axios、细节和杂项
    vue8-vuex、路径配置别名、promise
    vue07-路由 vue-router、keep-alive、tab-bar
  • 原文地址:https://www.cnblogs.com/luoliang/p/3035616.html
Copyright © 2020-2023  润新知