zoukankan      html  css  js  c++  java
  • 抽象工厂

    抽象工厂模式是我们项目开发中非常常见的一种模式,属于创建型模式。那么什么是模式呢?世上本没有所谓的模式,用的人多了也就有了。我们经常看到的各种框架,其实就是各种设计模式的一个集合。

    模式就是前人对解决某一类问题的经验方法的总结,打个比方,取水,你可以自己钻井,也可以通过安装自来水。那使用什么样的方式来达到取水的目的,那就得根据实际情况来用了,比如你住在偏远的大山里,那安装自来水肯定就不如钻井或者直接从山泉里面引流过来了。如果住在城市里,自己钻井,肯定又不太现实了,一则影响城市规划,二来城市里的地那么贵((*^__^*) 嘻嘻)。

    抽象工厂:每一个模式都是针对一定问题的解决方案。抽象工厂模式与工厂方法模式的最大区别就在于,工厂方法模式针对的是一个产品等级结构;而抽象工厂模式则需要面对多个产品等级结构。当每个抽象产品都有多于一个的具体子类的时候,工厂角色怎么知道实例化哪一个子类呢?比如每个抽象产品都有两个具体产品。抽象工厂模式提供两个具体工厂角色,分别对应于这两个具体产品角色,每一个具体工厂角色只负责某一个产品角色的实例化。每一个具体工厂类只负责创建抽象产品的某一个具体子类的实例。

    大家参考C#设计模式(4)——抽象工厂模式

    我这里主要给大家演示下下工作中比较low的实现方式,虽然很low,但是很多项目就是这么弄的,只是有一些项目用反射来代替具体实例化某个类对象了。

    在典型的三层架构中,我们经常会看到这样的项目结构,为了演示新建了两个实体类,Msg和Users

    先看下数据访问层的抽象工厂和工厂

    AbsFactoryDAL.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace DALFactory
    {
        /// <summary>
        /// 数据 抽象工厂类
        /// </summary>
        public abstract class AbsFactoryDAL
        {
            /// <summary>
            /// 根据配置文件 获取 实体数据工厂 对象
            /// </summary>
            /// <returns></returns>
            public static AbsFactoryDAL GetFatory()
            {
                string strType = System.Configuration.ConfigurationManager.AppSettings["dalType"].ToString();
                AbsFactoryDAL dalFactory = null;
                switch (strType)
                { 
                    case "dal":
                        dalFactory = new DALFactory();
                        break;
                    case "dalA":
                        dalFactory = null;
                        break;
                }
                return dalFactory;
            }
    
            public abstract IDAL.IUsers GetUser();
            public abstract IDAL.IMsg GetMsg();
        }
    }

    DALFactory.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace DALFactory
    {
        /// <summary>
        /// 数据工厂,只负责生成 数据层 DAL 项目里的 数据操作类
        /// </summary>
        public class DALFactory:AbsFactoryDAL
        {
            public override IDAL.IUsers GetUser()
            {
                return new DAL.Users();
            }
    
            public override IDAL.IMsg GetMsg()
            {
                return new DAL.Msg();
            }
        }
    }

    接下来是业务逻辑层的抽象工厂和具体工厂

    AbsFactoryBLL.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace BLLFactory
    {
        /// <summary>
        /// 业务抽象工厂类
        /// </summary>
        public abstract class AbsFactoryBLL
        {
            /// <summary>
            /// 根据配置文件 获取 实体业务工厂 对象
            /// </summary>
            /// <returns></returns>
            public static AbsFactoryBLL GetFatory()
            {
                string strType = System.Configuration.ConfigurationManager.AppSettings["bllType"].ToString();
                AbsFactoryBLL bllFactory = null;
                switch (strType)
                { 
                    case "bll":
                        bllFactory = new BLLFactory();
                        break;
                    case "blla":
                        bllFactory = new BLLFactoryA();
                        break;
                }
                return bllFactory;
            }
    
            public abstract IBLL.IUsers GetUser();
            public abstract IBLL.IMsg GetMsg();
        }
    }

    BLLFactory.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace BLLFactory
    {
        /// <summary>
        /// 业务工厂,只负责生成 业务层 BLL 项目里的 业务类
        /// </summary>
        public class BLLFactory:AbsFactoryBLL
        {
            public override IBLL.IUsers GetUser()
            {
                return new BLL.Users();
            }
    
            public override IBLL.IMsg GetMsg()
            {
                return new BLL.Msg();
            }
        }
    }

    BLLFactoryA.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace BLLFactory
    {
        /// <summary>
        /// 负责 生产 BLLA 项目里的业务类对象
        /// </summary>
        public class BLLFactoryA:AbsFactoryBLL
        {
            public override IBLL.IUsers GetUser()
            {
                return new BLLA.Users();
            }
    
            public override IBLL.IMsg GetMsg()
            {
                return new BLLA.Msg();
            }
        }
    }

    web.config

    <?xml version="1.0" encoding="utf-8"?>
    
    <!--
      有关如何配置 ASP.NET 应用程序的详细信息,请访问
      http://go.microsoft.com/fwlink/?LinkId=169433
      -->
    
    <configuration>
      
      <appSettings>
        <add key="bllType" value="bll"/>
        <add key="dalType" value="dal"/>
      </appSettings>
      
      <system.web>
        <compilation debug="true" targetFramework="4.0" />
      </system.web>
    
    </configuration>

     以上很low的写法只是说明学习的一个过程。

    其实完全可以用仓储模式和IOC来替代:

    业务逻辑层:

    using ProjectBase.Data;
    using ProjectBase.Utils.Entities;
    public class InChina : DomainObject<InChina, int, IInChinaRepository>                                                                                                                              
    {
    }

    数据层:

        public class InChinaRepository:ProjectBase.Data.AbstractNHibernateDao<InChina,int>,IInChinaRepository
        {
       }
        public interface IInChinaRepository:IDao<InChina,int>
        {
    }

    然后配置文件中使用IOC注入

     <unity>
        <containers>
          <container>
            <types>
              <type type="xxx.Core.xx,xxx.Core" mapTo="xxx.Interface.xx,xxx.Interface" />
            </types>
          </container>
        </containers>
      </unity>
  • 相关阅读:
    VS2008环境下的Freeglut 2.6.0配置
    Qt Creator and Clang
    qml 4.8 bug: ListView.view不能访问
    Qt platform mkspecs
    未在本地计算机上注册“Microsoft.Jet.OleDb.4.0”提供程序
    SSIS学习之数据挖掘
    SQL循环执行while控制
    SQL Server查询优化50法
    SSAS使用时间智能解决本年累计、同比、环比【转载】
    企业信息门户:EIP系统
  • 原文地址:https://www.cnblogs.com/jiekzou/p/4776879.html
Copyright © 2011-2022 走看看