人生是一场不能存盘的RPG,我只能尽量多搞几个Screenshot

July 26, 2006

ADO.NET 中的抽象工厂

Filed under: Design Pattern

用Interface或抽象类来生成一组相关的对象.
以ADO.Net2.0为例
见(Abstract Factory Design Pattern in ADO.NET 2.0 by Muhammad Mosa)
ado.net 定义了一组抽象的class: DbConnection, DbCommand, DbParameter等,
每个抽象class都有几个对应的实现: SqlConnection, OralceConnection.

ado.net 2.0中的DbProviderFactory class充当了abstract factory的角色,
DbProviderFactory中定义了一系列careateXXX函数来生成connection, command等class.
从它派生的SqlClientFactory, OracleClientFactory 等重载了这些createXXX方法,
可以生成具体的connection, command.

优点在于
1. 隔离具体的class
2. 可以任意替换一系列的class
3. 强制使用统一系列的class

缺点:
AbstractFactory interface 定义了需要产生的class的规格,如果需要增加一个新的系列,
需要从头到尾搞一套Factory class.

如果AbstractFactory 定义一个CreateProduct()方法,使用.net的reflection来生成对象,就
可以省略Concrate Factory的定义.

Comments »

The URI to TrackBack this entry is: http://recordsome.blogsome.com/2006/07/26/p132/trackback/

No comments yet.

RSS feed for comments on this post.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>



Anti-spam measure: please retype the above text into the box provided.






















Get free blog up and running in minutes with Blogsome
Theme designed by Hadley Wickham