简体   繁体   English

多种类型的单一方法?

[英]Single method for multiple types?

In cases where you have a function that works for many different custom types with the same implementation, is it ok to use a design pattern like this?:如果您有一个 function 可用于具有相同实现的许多不同自定义类型,那么可以使用这样的设计模式吗?:

type1 implicitly casts to type0
type2 implicitly casts to type0
type3 implicitly casts to type0

Operate ( type0 )

and call:并致电:

type1 a
type2 b
type3 c

Operate ( a )
Operate ( b )
Operate ( c )

Are there any problems with this technique?这种技术有问题吗? Performance wise, clarity, etc?性能明智,清晰度等?

EDIT: Also by implicit cast, I meant custom implemented casts for the type with no data loss, just convenience.编辑:同样通过隐式转换,我的意思是自定义实现的类型转换,没有数据丢失,只是方便。 Say you have a Pixel class, then sending an instance of this to a method that takes a Point2 automatically casts the pixel to a Point2.假设您有一个像素 class,然后将其实例发送到采用 Point2 的方法,该方法自动将像素投射到 Point2。

That's the basis of how interfaces work.这是接口如何工作的基础。

public interface IBlah {
    void DoSomething();
}
public class Type1 : IBlah {
    void DoSomething() { /* implementation */ }
}
public class Type2 : IBlah {
    void DoSomething() { /* implementation */ }
}
public class Type3 : IBlah {
    void DoSomething() { /* implementation */ }
}

public class Foo {
    void Operate(IBlah blah) {
        blah.DoSomething();
    }
}

Casting usually creates a new object, which is unnecessary in this case铸造通常会创建一个新的 object,这在这种情况下是不必要的

The more OOP way to do this would be through a base class or an interface.执行此操作的更多 OOP 方法是通过基础 class 或接口。

class Type1 : IOperatableType {}
class Type2 : IOperatableType {}
class Type3 : IOperatableType {}

void Operate ( IOperatableType a )

or或者

class Type1 : Type0 {}
class Type2 : Type0 {}
class Type3 : Type0 {}

void Operate ( Type0 a )

The calling method (Operate in this case) depends on using its parameters' methods or properties.调用方法(在这种情况下为操作)取决于使用其参数的方法或属性。 If these properties/methods are defined across all types (type1, type2, type3), consider using an interface that defines common functionality.如果在所有类型(type1、type2、type3)中定义了这些属性/方法,请考虑使用定义通用功能的接口。 If the implementation of the properties and methods are the same, save yourself from repeated code and consider inheriting from a base class.如果属性和方法的实现相同,请避免重复代码并考虑从基础 class 继承。

Also, when trying to understand your code, developers are more likely to first look at a class diagram, which allows them to see the relationship between classes, or at least the class definition (which will show the base types and implemented interfaces) rather than looking into (implicit/explicit) operators to see which class is castable to which other class.此外,在尝试理解您的代码时,开发人员更有可能首先查看 class 图,它可以让他们看到类之间的关系,或者至少是 class 定义(将显示基本类型和实现的接口)而不是查看(隐式/显式)运算符以查看哪个 class 可转换为其他 class。

This is a perfectly normal practice.这是完全正常的做法。

Performance wise, I don't believe there are any issues.性能方面,我认为没有任何问题。

Clarity wise, because of Intellisense, you're unlikely to have issues with this either.清晰明智,由于 Intellisense,您也不太可能对此有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM