设计模式实例-抽象工厂模式

using System;
namespace Ahoo.Demo.DesignPatterns.Patterns.AbstractFactory
{
    /*######抽象工厂模式#######
     * 提供创建一系列相关/项目依赖对象的接口,
     * 无需指定具体子类。
     */
    public class Department { }
    interface IDepartmentDAL
    {
        void Insert(Department entity);
        Department GetEntity(long Id);
    }
    /// <summary>
    /// 3....
    /// </summary>
    public class SqlServerDepartment : IDepartmentDAL
    {
        public void Insert(Department entity)
        {
            Console.WriteLine("在MS-SQL中插入一条Department记录!");
        }
        public Department GetEntity(long Id)
        {
            Console.WriteLine("在MS-SQL中获得一条Department记录!");
            return new Department();
        }
    }
    public class MySqlDepartment : IDepartmentDAL
    {
        public void Insert(Department entity)
        {
            Console.WriteLine("在MySql中插入一条Department记录!");
        }
        public Department GetEntity(long Id)
        {
            Console.WriteLine("在MySql中获得一条Department记录!");
            return new Department();
        }
    }
    /// <summary>
    /// 1....
    /// 抽象工厂接口,包含所有产品创建的抽象方法
    /// </summary>
    interface IFactory
    {
        IDepartmentDAL CreateDepartmentDAL();
        //IUserDAL CreateUserDAL();
    }

    /// <summary>
    /// 2....
    /// 具体实现工厂
    /// </summary>
    public class SqlServerFactory : IFactory
    {
        public IDepartmentDAL CreateDepartmentDAL()
        {
            return new SqlServerDepartment();
        }
    }

    public class Client
    {
        public static void Excute()
        {
            IFactory factory = new SqlServerFactory();
            IDepartmentDAL dal = factory.CreateDepartmentDAL();
            dal.Insert(new Department { });
        }
    }

    /// <summary>
    /// 反射技术的改善
    /// </summary>
    public class DataAccess
    {
        private static readonly string AssemblyName = "抽象工厂模式";
        private static readonly string DB = "SqlServer";
        public static IDepartmentDAL CreateDepartmentDal()
        {
            string className = String.Format("{0}.{1}.{2}", AssemblyName, DB, "DepartmentDAL");
            IDepartmentDAL dal = (IDepartmentDAL)Assembly.Load(AssemblyName).CreateInstance(className);
            return dal;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值