preferred风格(在抽象类或接口委托) [英] Preferred style (delegates over abstract Classes or interfaces)

查看:87
本文介绍了preferred风格(在抽象类或接口委托)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个工具的类库我写。一个写了一个工厂类是通用的,与来自它可以在许多不同的情况可以重复使用呼叫者传入几个代表。然而,我的一个同事建议我用一个抽象类或者接口,所以它更明确需要什么样的功能覆盖。在这个模型中,我创建利用图书馆,那么你就需要重写2类,而不​​只是一个。什么标准确定何时可以适当使用委托功能或接口。

使用委托模式。


  A类
{
    FUNC<字符串,ReultClass> FUN1 {获取;设置;}    FactoryObj CREATEOBJ()
    {
         返回FUN1()
    }
}

使用的接口模式

  B类
{
    InterfaceObj {获取;设置;}    FactoryObj CREATEOBJ()
    {
         返回InterfaceObj.fun1()
    }
}


解决方案

如果设计是这样的类依赖于另一个类(即使是一个简单的类),我会建议接口的做法,因为对象确实有依赖在外面code:

 接口IA
{
    ResultClass FUN1(字符串ARG);
}

在这种情况下,你仍然可以使用委托;我通常写我的匿名班,显式接口实现(如微软称他们),所以委托属性的名称是一样的,因为它实现了接口方法的名称:

 类AnonymousA:IA
{
    FUNC<字符串,ResultClass> FUN1 {搞定;组; }
    ResultClass IA.Fun1(字符串ARG){返回this.Fun1(ARG); }
}

如果有涉及仿制药(例如, IA< ResultClass> 和相应的 AnonymousA< ResultClass> ),那么它的有用定义一个工厂类,只是为了清理创作语法:

 静态类匿名
{
    公共静态IA< ResultClass> A< ResultClass>(Func键<字符串,ResultClass> FUN1)
    {返回新AnonymousA< ResultClass> {FUN1 = FUN1}
}

I have a utility class library I am writing. A have written a factory class that is generic enough that with a few delegates passed in from the caller it can be reused in many different situations. However, one of my colleagues suggested that I use an abstract class or an interface so it is more explicit what functions needed to be overridden. In the model I have created to use the library you would then need to override 2 classes instead of just one. What criteria determines when it is appropriate to use delegate functions or interfaces.

Using a delegate pattern.


Class A 
{
    Func<string , ReultClass> fun1 {get;set;}

    FactoryObj CreateObj()
    {
         return fun1("")
    }
}

Using an interface pattern

Class B 
{
    InterfaceObj{get;set;}

    FactoryObj CreateObj()
    {
         return InterfaceObj.fun1("")
    }
}

解决方案

If the design is such that the class depends on another class (even a simple class), I would recommend the interface approach, since the object does have to depend on outside code:

interface IA
{
    ResultClass Fun1(string arg);
}

In this case, you can still use delegates; I usually write my "anonymous" classes (as Microsoft calls them) with explicit interface implementations, so the name of the delegate property is the same as the name of the interface method it implements:

class AnonymousA : IA
{ 
    Func<string, ResultClass> Fun1 { get; set; }
    ResultClass IA.Fun1(string arg) { return this.Fun1(arg); }
} 

If there are generics involved (e.g., IA<ResultClass> and the corresponding AnonymousA<ResultClass>), then it's useful to define a factory class, just to clean up the creation syntax:

static class Anonymous
{
    public static IA<ResultClass> A<ResultClass>(Func<string, ResultClass> fun1)
    { return new AnonymousA<ResultClass> { Fun1 = fun1 }
}

这篇关于preferred风格(在抽象类或接口委托)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆