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

使用反射来建立动态工厂
反射的机制我这里就省略了。
.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
大家对上面的几种做法自己体会吧。
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值