Net设计模式之抽象工厂模式(Abstract Factory Pattern)(2)

本文涉及的产品
云数据库 RDS SQL Server,独享型 2核4GB
简介:

四.案例分析(Example

1、场景

使用抽象工厂 + 反射 + 配置文件实现数据访问层程序。结构 如下图所示
 
用反射 + 抽象工厂 + 配置文件的数据访问程序。
Assembly.Load(" 程序集名称 ").CreateInstance(" 命名空间 . 类名称 ") 。比如:
IProduct product=(IProduct)Assembly.Load(" 抽象工程模式 ").CreateInstance(" 抽象工程模式 .SqlServerProduct")
常用做法是:
Private static readonly string AssemblyName=" 抽象工程模式 ";
Private static readonly string DB=ConfiurationManager.AppSettings["db"];
配置文件如下:
<configuration>
     <appSettings>
          <add key="db" value="Sqlserver"/>
     <appSettings>
<configuration>
通过读配置文件给 DB 字符赋值,在配置文件中写明当前使用的是 SqlServer  还是 Access 数据库。反射 + 抽象工厂 + 配置文件解决方案解决了数据访问时的可维护、可扩展问题
 

2、代码

1 、对象 Uer Product 及其相对应的操作
public  interface IUser
{
    void  Insert();
}
 
public  class SqlServerUser :IUser
{
    public void  Insert()
     {
        Console .WriteLine("{0} 插入用户 ." ,this .GetType().Name);
     }
}
 
public  class AccessUser  : IUser
{
    public void  Insert()
     {
        Console .WriteLine("{0} 插入用户 ." this .GetType().Name);
     }
}
 
public  interface IProduct
{
    void  GetProduct();
}
 
public  class SqlServerProduct  : IProduct
{
    public void  GetProduct()
     {
        Console .WriteLine("{0} 查询商品 ." this .GetType().Name);
     }
}
 
public  class AccessProduct  : IProduct
{
    public void  GetProduct()
     {
        Console .WriteLine("{0} 查询商品 ." this .GetType().Name);
     }
}
 
2 、数据访问类 DataAccess
public  class DataAccess
{
    private static readonly string  AssemblyName = "AbstractFactoryReflection" ;
    private static readonly string  db = "SqlServer" ;
 
    public static IUser  CreateUser()
     {
        string  className = AssemblyName + "."  + db + "User" ;
        IUser  user = (IUser )Assembly .Load(AssemblyName).CreateInstance(className);
        return  user;
     }
 
    public static IProduct  CreateProduct()
     {
        string  className = AssemblyName + "."  + db + "Product" ;
        return  (IProduct )Assembly .Load(AssemblyName).CreateInstance(className);
     }
}
 
3 、客户端代码
static  void Main (string [] args)
{
    IUser  user = DataAccess .CreateUser();
     user.Insert();
 
    IProduct  product = DataAccess .CreateProduct();
     product.GetProduct();
 
    Console .ReadKey();
}
 

五、总结(Summary

抽象工厂模式( Abstract Factory Pattern ),提供一个创建一系列相关或者相互依赖对象的接口,而无需制定他们的具体类。抽象工厂模式的典型应用就是,使用抽象工厂 + 反射 + 配置文件实现数据访问层程序。









本文转自 灵动生活 51CTO博客,原文链接:http://blog.51cto.com/smartlife/260399,如需转载请自行联系原作者

相关实践学习
使用SQL语句管理索引
本次实验主要介绍如何在RDS-SQLServer数据库中,使用SQL语句管理索引。
SQL Server on Linux入门教程
SQL Server数据库一直只提供Windows下的版本。2016年微软宣布推出可运行在Linux系统下的SQL Server数据库,该版本目前还是早期预览版本。本课程主要介绍SQLServer On Linux的基本知识。 相关的阿里云产品:云数据库RDS&nbsp;SQL Server版 RDS SQL Server不仅拥有高可用架构和任意时间点的数据恢复功能,强力支撑各种企业应用,同时也包含了微软的License费用,减少额外支出。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/sqlserver
目录
相关文章
|
3月前
|
设计模式 算法 Java
行为型设计模式-策略模式(Strategy Pattern)
行为型设计模式-策略模式(Strategy Pattern)
|
4月前
|
设计模式 缓存
二十三种设计模式全面解析-代理模式(Proxy Pattern)详解:探索隐藏于背后的力量
二十三种设计模式全面解析-代理模式(Proxy Pattern)详解:探索隐藏于背后的力量
|
3月前
|
设计模式 算法
设计模式 - 行为型模式_ 访问者模式Visitor Pattern
设计模式 - 行为型模式_ 访问者模式Visitor Pattern
39 1
设计模式 - 行为型模式_ 访问者模式Visitor Pattern
|
3月前
|
设计模式 Java 应用服务中间件
设计模式 -结构型模式_门面模式(外观模式) Facade Pattern 在开源软件中的应用
设计模式 -结构型模式_门面模式(外观模式) Facade Pattern 在开源软件中的应用
31 1
|
3月前
|
设计模式 缓存 安全
设计模式 - 创建型模式_ 单例模式 Singleton Pattern
设计模式 - 创建型模式_ 单例模式 Singleton Pattern
39 0
|
10天前
|
设计模式 存储 Java
Java设计模式:解释一下单例模式(Singleton Pattern)。
`Singleton Pattern`是Java中的创建型设计模式,确保类只有一个实例并提供全局访问点。它通过私有化构造函数,用静态方法返回唯一的实例。类内静态变量存储此实例,对外仅通过静态方法访问。
15 1
|
4月前
|
设计模式 Java
Java设计模式:什么是观察者模式(Observer Pattern)?
Java设计模式:什么是观察者模式(Observer Pattern)?
31 0
|
4月前
|
设计模式 自然语言处理 编译器
二十三种设计模式全面解析-解释器模式(Interpreter Pattern):用代码诠释语言的魅力
二十三种设计模式全面解析-解释器模式(Interpreter Pattern):用代码诠释语言的魅力
|
4月前
|
设计模式
二十三种设计模式全面解析-职责链模式(Chain of Responsibility Pattern):解放代码责任链,提升灵活性与可维护性
二十三种设计模式全面解析-职责链模式(Chain of Responsibility Pattern):解放代码责任链,提升灵活性与可维护性
|
4月前
|
设计模式
二十三种设计模式全面解析-外观模式(Facade Pattern)详解:简化复杂系统的奇妙之道
二十三种设计模式全面解析-外观模式(Facade Pattern)详解:简化复杂系统的奇妙之道