随笔-42  评论-128  文章-0  trackbacks-4
VS 2008

针对多个平行产品体系的产品创建问题,使用抽象工厂模式

1. 模式UML图




2. 应用

    考虑应用程序支持多种数据库的设计,对于领域对象的数据库访问层定义数据访问的接口,分别提供基于SqlServer的实现和基于Oracle的实现。



IUser.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPattern.AbstractFactory.DAL {
    
public interface IUser {

        
void Operation();
    }

}


SqlUser.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPattern.AbstractFactory.DAL {
    
public class SqlUser : IUser {
        
IUser Members
    }

}


OracleUser.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPattern.AbstractFactory.DAL {
    
public class OracleUser : IUser {
        
IUser Members
    }

}


ITopic.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPattern.AbstractFactory.DAL {
    
public interface ITopic {

        
void Operation();
    }

}


SqlTopic.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPattern.AbstractFactory.DAL {
    
public class SqlTopic : ITopic {
        
ITopic Members
    }

}


OracleTopic.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPattern.AbstractFactory.DAL {
    
public class OracleTopic: ITopic {
        
ITopic Members
    }

}


IDALFactory.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPattern.AbstractFactory.DAL {
    
public interface IDALFactory {

        IUser CreateUser();
        ITopic CreateTopic();
    }

}


SqlFactory.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPattern.AbstractFactory.DAL {
    
public class SqlFactory : IDALFactory {
        
IDALFactory Members
    }

}


OracleFactory.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPattern.AbstractFactory.DAL {
    
public class OracleFactory : IDALFactory {
        
IDALFactory Members
    }

}


Client

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DesignPattern.AbstractFactory.DAL;

namespace DesignPattern.AbstractFactory {
    
class Program {
        
static void Main(string[] args) {

            IDALFactory sqlFactory 
= new SqlFactory();
            IUser sqlUser 
= sqlFactory.CreateUser();
            sqlUser.Operation();

            ITopic sqlTopic 
= sqlFactory.CreateTopic();
            sqlTopic.Operation();

            IDALFactory oracleFactory 
= new OracleFactory();
            IUser oracleUser 
= oracleFactory.CreateUser();
            oracleUser.Operation();

            ITopic oracleTopic 
= oracleFactory.CreateTopic();
            oracleTopic.Operation();
        }

    }

}


Output

posted on 2008-03-16 00:25 Tristan(GuoZhijian) 阅读(1738) 评论(4)  编辑 收藏 所属分类: Design Pattern

评论:
#1楼  2008-03-16 12:47 | 金色海洋(jyk)      
就例子论例子。

ADO.net2.0,自身就带了一个工厂,可以实现多种数据库的连接,不对,是工厂方式的创建。

如果使用连接多种数据库的情况讲解工厂的话,建议把ADO.net2.0 或者ADO.net3.0/ADO.net3.5 的内部代码拿出来分析,这样看完了你的分析就可以直接应用了,即掌握了工厂,又知道了ADO.net2.0的内部代码结构。岂不是更好。
  回复  引用  查看    
#2楼 [楼主] 2008-03-16 14:26 | Tristan(Guozhijian)      
兄弟考虑真是全面
有机会深入探讨一下
  回复  引用  查看    
#3楼  2008-03-17 10:39 | 力大无比 [未注册用户]
不同的理解
  回复  引用    
#4楼 [楼主] 2008-03-17 11:49 | Tristan(Guozhijian)      
@力大无比
那您是怎么理解的呢?
  回复  引用  查看    

标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2008-03-16 00:30 编辑过


相关链接: