C#设计模式之工厂模式的抽象工厂的使用
C#设计模式之工厂模式的抽象工厂的使用
目录
一、设计模式
二、抽象工厂设计模式
一、设计模式
1、设计模式 : 针对项目中遇到的一些特殊问题,我们可以借鉴开发者前辈们给我们总结的经验
2、设计模式的分类:
一、创建型设计模式
解决对象创建的问题,就是因为某些时候为了项目的扩展,我们必须把项目中对象的创建教给“第三方”完成,以达到解耦的目标
(1)、 一个对象的创建:简单工厂模式
(2)、 一组对象的创建:抽象工厂模式
(3)、 只能创建一个对象:单利模式
二、结构型设计模式
研究类与类之间的关系问题
适配器模式、桥接模式、装饰器模式、组合模式、外观模式、代理模式。。。
三、行为型设计模式
研究问题:对象和行为的分离问题
模板模式、命令模式、终结者模式、观察者模式、、、、
二、抽象工厂设计模式
1、抽象工厂的使用场合举例: 一个项目的多数据库支持 (数据访问层DAL),或者一个项目的业务层变 (业务层) ,服务变化 (服务层)
2、抽象工厂需要哪些元素:
(1) 实体层(Models): 封装和传递数据
(2)业务层: 可以是任何可以抽象封装的模块 (广义概念,可以为数据层也可以为业务层)
首先定义业务接口,通用类型IBizLayerInterface
其次实现业务接口,根据需要在不同的模块中实现接口,IBizLayerImpl1;IBizLayerImpl2
最后根据 用户不同要求完成接口和实现的组合BizFactory
(3)其他层: 业务逻辑层,通用层,通信层,UI层,服务层等
3、代码演示:
(1)首先,创建解决方案内的程序集和类
(2)程序集BizFactory内创建ObjectFactory类
using System; using System.Collections.Generic; using System.Linq; using System.Text; //引入命名空间 using System.Configuration; using System.Reflection; using IBizLayerInterace; namespace BizFactory { /// <summary> /// 多对象选择工厂 /// </summary> public class ObjectFactory { //获取配置信息 private static string bizName = ConfigurationManager.AppSettings["bizName"].ToString(); //添加一个工厂选择方法(有多少个接口类就实心多少个方法) public static IStudent CreateStudent() { return (IStudent)Assembly.Load(bizName).CreateInstance(bizName + ".StudentImpl"); } public static ITeacher CreateTeacher() { return (ITeacher)Assembly.Load(bizName).CreateInstance(bizName+ ".TeacherImpl"); } //如果还有其他接口类,在这里继续添加即可 //为了防止接口过多而编写大量方法,使用泛型方法 public static T CreateObject<T>(string className) { return (T)Assembly.Load(bizName).CreateInstance(bizName+"."+className); }} }
(3)程序集IBizLayerImpl1内创建两个类StudentImpl 类和TeacherImpl类
StudentImpl 类
using System; using System.Collections.Generic; using System.Linq; using System.Text; //引用命名空间 using Models; using IBizLayerInterace; namespace IBizLayerImpl { public class StudentImpl : IStudent { public int GetStudent(string param) { //实际开发中在这里写具体的业务实现、、、 return 100; } public List<Student> Query(int param1) { return new List<Student> { //可以在这个地方,比如查询数据库、或者调用其他服务(WCF)、、、、 new Student{ StudentId=1,StudentName="学生1"}, new Student { StudentId=2,StudentName="学生2"} }; } } }
TeacherImpl类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Models; using IBizLayerInterace; namespace IBizLayerImpl { public class TeacherImpl : ITeacher { public ITeacher GetTeacher(string param) { throw new NotImplementedException(); } public string Query(string param1, string param2) { throw new NotImplementedException(); } } }
(4)程序集IBizLayerImpl2内创建两个类StudentImpl类和TeacherImpl类
StudentImpl类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Models; using IBizLayerInterace; namespace IBizLayerImpl2 { public class StudentImpl : IStudent { public int GetStudent(string param) { //实际开发中在这里写具体的业务实现、、、 return 200; } public List<Student> Query(int param1) { return new List<Student> { //可以在这个地方,比如查询数据库、或者调用其他服务(WCF)、、、、 new Student{ StudentId=1,StudentName="学生1"}, new Student { StudentId=2,StudentName="学生2"}, new Student { StudentId=3,StudentName="学生3"} }; } } }
TeacherImpl类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Models; using IBizLayerInterace; namespace IBizLayerImpl2 { public class TeacherImpl : ITeacher { public ITeacher GetTeacher(string param) { throw new NotImplementedException(); } public string Query(string param1, string param2) { throw new NotImplementedException(); } } }
(5)创建一个接口程序集,内含两个接口IStudent 接口和ITeacher接口
IStudent 接口
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Models; namespace IBizLayerInterace { public interface IStudent { int GetStudent(string param); List<Student> Query(int param1); } }
ITeacher接口
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace IBizLayerInterace { public interface ITeacher { ITeacher GetTeacher(string param); string Query(string param1, string param2); } }
(6)添加界面UI程序集CustomerUI,内含应用配置文件App.config和program类
App.config
<?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="bizName" value="IBizLayerImpl2"/> </appSettings> </configuration>
Program类
using System; using System.Collections.Generic; using System.Linq; using System.Text; //引用命名空间 using Models; using IBizLayerInterace; using BizFactory; namespace CustomerUI { class Program { static void Main(string[] args) { //这种直接创建的方式,如果需求有变 20000 动还需要修改程序,不满足OOP的开闭原则 //IStudent student =new IBizLayerImpl.StudentImpl(); //抽象工厂模式创建对象不用关心具体对象创建的模块,达到很好的解耦效果 IStudent student = ObjectFactory.CreateStudent(); //IStudent student = ObjectFactory.CreateObject<IStudent>("StudentImpl");//泛型方法的使用 Console.WriteLine(student.GetStudent("")); Console.ReadKey(); } } }
(7)实体程序集内含两个类
Student类
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Models { public class Student { public int StudentId { get; set; } public string StudentName { get; set; } } }
Teacher类
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Models { public class Teacher { public int TeacherId { get; set; } public string TeacherName { get; set; } public string PhoneNumber { get; set; } } }
(8)运行测试结果
配置文件和结果1:
配置文件和结果2:
4、碰到的问题及解决办法:
解决办法:因为缺少IbizLayerImpl1的dll模块,引入到UI中即可结局
问题:
解决办法:
检查value是否写错
将要加载的程序集重新生成,用新的.dll文件替换原来的就行了
3、扩展后的修改
//为了防止接口过多而编写大量方法,使用泛型方法 public static T CreateObject<T>(string className) { return (T)Assembly.Load(bizName).CreateInstance(bizName+"."+className); }
- 抽象工厂设计模式(C#)
- C#设计模式之抽象工厂
- C#--抽象工厂设计模式--三层框架
- Java技术_每天掌握一种设计模式(004)_使用场景及简单实例(创建型:抽象工厂)
- C#设计模式:抽象工厂方法模式
- 设计模式(三) 抽象工厂的学习和使用
- C#面向对象设计模式纵横谈 学习笔记3 Abstract Factory 抽象工厂
- 实例解析C#设计模式编程中简单工厂模式的使用
- 扩展无极限:工厂设计模式与C#扩展方法的结合使用
- 《C# 设计模式》笔记: 第4章 在C#中使用类和对象
- C#设计模式之简单工厂篇
- 设计模式:简单工厂、工厂方法、抽象工厂之小结与区别 (转)
- C#设计模式之简单工厂篇
- 设计模式--抽象工厂
- 设计模式-旧话重提之类工厂的使用
- 设计模式-简单工厂、工厂方法、抽象工厂之小结与区别
- C#设计模式之简单工厂篇(转载)
- 设计模式-----抽象工厂
- 设计模式之抽象工厂
- 设计模式:简单工厂、工厂方法、抽象工厂之小结与区别 (转)