2. 抽象工厂模式(Abstract Factory) ---------- 创建型模式

1. 抽象工厂的目的是提供一个创建一系列相关或具有相似行为对象的实例,而无需指定它们具体的类。 

2. 举例, 数据库访问中, 如果我们要求我们的系统支持SQL SERVER, Oracle等数据, 根据不同用户的需求, 切换不同的数据库.  我们不想到处用if  else判断用户的需求的话, 抽象工厂就可以大派用场了.

例如我们先定义抽象工厂, 然后两个实际访问数据库的类都继承此工厂. 然后利用抽象工厂生成其需要的实例.

using System;                                                                                              
using System.Reflection;                                                                                   
                                                                                                           
namespace AbstractFactory                                                                                  
{                                                                                                          
    /** <summary>                                                                                      
    /// AbstractFactory类                                                                                  
    /// </summary>                                                                                         
    public abstract class AbstractFactory                                                                  
    {                                                                                                      
        public static AbstractFactory GetInstance()                                                        
        {                                                                                                  
            string factoryName =  ConfigurationManager.AppSettings["DBFactory"];                           
                                                                                                           
            AbstractFactory instance;                                                                      
                                                                                                           
            if(factoryName != "")                                                                          
                instance = (AbstractFactory)Assembly.Load(factoryName).CreateInstance(factoryName);        
            else                                                                                           
                instance = null;                                                                           
                                                                                                           
            return instance;                                                                               
        }                                                                                                  
                                                                                                           
                                                                                                           
       public abstract void Open();                                                                         
                                                                                                            
       public abstract void ExeQuery();                                                                    
                                                                                                     
       public abstract void Close();                                                                       
    }                                                                                                      
    

    public class SQLServerFactory : AbstractFactory 
    {            

       public  SQLServerFactory ()

      {;}                                                                          
       public void Open()

       {; }                                                                        
                                                                                                            
       public void ExeQuery()

       {; }
                                                                                                     
       public void Close()

      {; }
                                                                      
    }           

    public class ORAServerFactory : AbstractFactory 
    {            

       public  ORAServerFactory ()

      {;}                                                                          
       public void Open()

       {; }                                                                        
                                                                                                            
       public void ExeQuery()

       {; }
                                                                                                     
       public void Close()

      {; }
                                                                      
    }                                                                                                                

}           

在配置文件中加上, 以得取我们希望生成的类的实例.

<AppSettings>                                             
<add key="DBFactory" value="SQLServerFactory"/>         
</AppSettings>                                             
                                                        
题外话:  看代码中这句话                                                                                             
 instance = (AbstractFactory)Assembly.Load(factoryName).CreateInstance(factoryName);         
利用C#反射生成一个在config文件中配置的 SQLServerFactory的类的实例.  这种不通过在代码中直接通过new生成实而通过配置文件反射生成实例的方法被称为IOC(反转控制,又名依赖注入), 目的是利用配置文件, 达到在运行时决定生成实例的类型, 避免在代码中硬编码从而达到解偶的目的. 这样用户可以随时更改SQLServerFactor或者ORAServerFactory , 来访问不同的数据库.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值