使用反射在NET中实现动态工厂(第二部分)

** 使用反射在 ** ** NET ** ** 中实现动态工厂Part Two ** ** **

使用反射来建立动态工厂

反射的机制我这里就省略了。

.NET Framework 提供给对象使用属性来描述自己的能力。

属性被声明成一个从 System.Attribute 继承下来的类。

一个已经定义的属性可以附加到:接口、类、装配件上。

下面定义了两个属性类:

[AttributeUsage(AttributeTargets.Class)]

public class InventoryPartAttribute : Attribute {

private enmInvParts mInventoryPart;

public InventoryPartAttribute (enmInvParts vInvPart) {

mInventoryPart = vInvPart;

}

public enmInvParts InventoryPartSupported {

get {return mInventoryPart;}

set { mInventoryPart = value;}

}

}

[AttributeUsage(AttributeTargets.Interface)]

public class ImplAttr : Attribute {

private Type[] mImplementorList;

public ImplAttr(Type[] Implementors){

mImplementorList = Implementors;

}

public Type[] ImplementorList {

get {return mImplementorList;}

set {mImplementorList = value;}

}

}

InventoryPartAttribute 用来附加到实现 IpartsInventory 接口的类中,用来指定当前处理的零件的类型。下面的代码演示了这个过程:

[InventoryPartAttribute(enmInvParts.Monitors)]

class MonitorInventory : IPartsInventory {

public void Restock() {

Console.WriteLine("monitor inventory restocked");

}

}

[InventoryPartAttribute(enmInvParts.Keyboards)]

class KeyboardInventory : IPartsInventory {

public void Restock() {

Console.WriteLine("keyboard inventory restocked");

}

}

第二个属性用来和 IpartsInventory 接口帮定,用来帮助接口判断是那个类型的零件类实现了他。比如:

[ImplAttr(new Type[]{typeof(MonitorInventory),typeof(KeyboardInventory)})]

interface IPartsInventory() {

public void Restock();

}

到此为止:我们有了两个属性并分别把她们和 IpartsInventory 接口以及实现了这个接口的具体类 (MonitorInventory and KeyboardInventory) 作了帮定。

现在我们看看如何实现动态工厂:

在上面的代码中: PartsFactory 做了很大的改变。

代码如下:

class PartsFactory {

public IPartsInventory ReturnPartInventory(enmInvParts vInvPart) {

IPartsInventory InvPart = null;

object Obj;

Type[] IntrfaceImpl;

Attribute Attr;

enmInvParts enmInventoryPart;

InventoryPartAttribute InvPartAttr;

int ImplementorCount;

// 从 IpartsInventory 接口中获得属性

Attr = Attribute.GetCustomAttribute(typeof(IPartsInventory),

typeof(ImplAttr));

// 获得实现 IpartsInventory 的类型数组

IntrfaceImpl = ((ImplAttr)Attr).ImplementorList;

// 获得实现 IpartsInventory 的类的个数

ImplementorCount = IntrfaceImpl.GetLength(0);

for (int i = 0; i < ImplementorCount; i++) {

Attr = Attribute.GetCustomAttribute(IntrfaceImpl[i],

typeof(InventoryPartAttribute));

InvPartAttr = (InventoryPartAttribute)Attr;

// 决定那个零件类

enmInventoryPart = InvPartAttr.InventoryPartSupported;

if((int) enmInventoryPart == (int)vInvPart) {

Obj = Activator.CreateInstance(IntrfaceImpl[i]);

InvPart = (IPartsInventory)Obj;

break;

}

}

return InvPart;

}

}

我们看看上面代码的具体含义:

The first line retrieves the ImplAttr attribute of the IPartsInventory as shown here:

Attr = Attribute.GetCustomAttribute(typeof(IPartsInventory),

typeof(ImplAttr));

上面的代码获得了关于 IpartsInventory 接口的属性: ImplAttr

使用:

IntrfaceImpl = ((ImplAttr)Attr).ImplementorList;

获得类型数组。

得到实现了接口 IntrfaceImpl 的类的个数。

ImplementorCount = IntrfaceImpl.GetLength(0);

然后循环这个数组。

第一步:

获得实现接口类的 InventoryPartAttribute 属性的具体值,

Attr = Attribute.GetCustomAttribute(IntrfaceImpl[i],

typeof(InventoryPartAttribute));

然后把这个属性转换成为: InventoryPartAttribute 类型 并获得其具体的类型值:

InvPartAttr = (InventoryPartAttribute)Attr;

enmInventoryPart = InvPartAttr.InventoryPartSupported;

这时:如果从中取出的 enumerator 能和客户端传过来的匹配上的话,就创建这个类的实例,并退出循环。

if((int) enmInventoryPart == (int)vInvPart) {

Obj = Activator.CreateInstance(IntrfaceImpl[i])

InvPart = (IPartsInventory)Obj;

break;

}

最后:返回 IpartsInventory 对象

return InvPart;

好了,我们看看上面的这种改变的好处。

假设:现在我们需要 IpartsInventory 接口的另一个类。鼠标垫类

MousePadInventory

我们需要做的改变是:

1、 更新: enmInvParts

public enum enmInvParts : int {Monitors = 1, Keyboards, MousePads};

2、 鼠标垫的具体实现:

[InventoryPartAttribute(enmInvParts.MousePads)]

class MousePadInventory : IPartsInventory {

public void Restock() {

Console.WriteLine("The mouse pad inventory has been restocked");

}

}

3 、 IpartsInventory 接口的改变

[ImplAttr(new Type[]{typeof(MonitorInventory),typeof(KeyboardInventory),

typeof(MousePadInventory)})]

interface IPartsInventory() {

public void Restock();

}

4 、在使用 MousePadInventory 的地方 MainClass 中加上映射:

case "MousePads":

InvMgr.ReplenishInventory(enmInvParts.Keyboards);

break;

结束。

哈,大家看到上面的好处了吗?(翻译到这里我很激动)

我们不需要修改: PartsFactory 、 InventoryMgr

missjasper(2003-3-11 16:57:02 )

我不是高手,不过我很愿意说说我的理解,大家一起来讨论 :)

通过属性,在接口 IPartsInventory 上定义将要创建的实例的类型,并放在数组里

通过属性,在每个类上定义它的枚举值

创建时,根据枚举值,在数组里找到对应的类型,创建出来。

我觉得这个方式比较简单,但是如果支持同一接口的类多了呢?

http://msdn.microsoft.com/msdnmag/issues/03/03/designpatterns/default.aspx

大家对上面的几种做法自己体会吧。

我的联系方式: [email protected]

我现在在我做的项目中主动的使用设计模式收益匪浅。

欢迎交流。

Published At
Categories with Web编程
Tagged with
comments powered by Disqus