设计模式实例-抽象工厂模式
2015-04-02 15:12
295 查看
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; } } }
相关文章推荐
- 设计模式之抽象工厂模式实例
- 设计模式--2: Net设计模式实例之抽象工厂模式(Abstract Factory Pattern)
- Net设计模式实例之抽象工厂模式(Abstract Factory Pattern)
- 23种设计模式C++实例之抽象工厂模式
- Java设计模式之抽象工厂模式实例详解
- Net设计模式实例之抽象工厂模式(Abstract Factory Pattern)
- Java设计模式编程中简单工厂与抽象工厂模式的使用实例
- 设计模式实例(Lua)笔记之二(AbstractFactory抽象工厂模式)
- 设计模式(三)——抽象工厂模式实例
- 设计模式实例学习-抽象工厂模式
- C++设计模式——抽象工厂模式(含实例)
- Net设计模式实例之抽象工厂模式(Abstract Factory Pattern)
- 设计模式笔记-抽象工厂模式
- .NET设计模式:抽象工厂模式(Abstract Factory)
- C#面向对象的设计模式 抽象工厂模式
- 我的设计模式之旅(3)――抽象工厂模式AbstractFactory
- 学习TerryLee 的设计模式:抽象工厂模式(附源码)_AX
- 设计模式C#描述——抽象工厂模式
- 设计模式之抽象工厂模式
- .Net设计模式之抽象工厂模式(Abstract Factory)