您的位置:首页 > 编程语言 > C#

C#设计模式之工厂模式的抽象工厂的使用

2019-06-07 23:38 1066 查看
版权声明:Copyright:@@个人所有 https://blog.csdn.net/y20_20/article/details/91065119

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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: