c# - 一个好的做法是留下一个空的界面吗?

标签 c# interface

这次,我要出一道数学题。我计划有一个字典,其中键是 Levels enum {Easy, Medium, Hard} 并且值应该包含一些关于如何创建问题的配置。

例如:

BinaryProblemConfiguration
    + Bound1 : Bound<int>
    + Bound2 : Bound<int>

边界有两个属性:最小值和最大值。

其他类型的问题不需要Bounds,但需要其他数据。

所以,我想创建一个名为 IConfiguration 的接口(interface)。

public interface IConfiguration {}

具体的配置应该是:

public class BinaryProblemConfiguration : IConfiguration
{
    public Bound Bound1 {get;set;}
    public Bound Bound2 {get;set;}
}

public class AnotherProblemConfiguration : IConfiguration
{
    // other stuff
}

想法是有一个名为 ConfigurationLevels 的字典。这是将界面留空的好习惯还是意味着我的设计有问题?

最佳答案

.NET Framework 设计指南将此称为“标记”接口(interface),并明确表示这是一个坏主意。他们建议改用自定义属性。

Avoid using marker interfaces (interfaces with no members).

Custom attributes provide a way to mark a type. For more information about custom attributes, see Writing Custom Attributes. Custom attributes are preferred when you can defer checking for the attribute until the code is executing. If your scenario requires compile-time checking, you cannot comply with this guideline.

http://msdn.microsoft.com/en-us/library/ms229022.aspx

public sealed class ConfigurationAttribute : Attribute {

}


[ConfigurationAttribute]
public class AnotherProblemConfiguration : IConfiguration 
{ 
    // other stuff 
} 

关于c# - 一个好的做法是留下一个空的界面吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9039784/

相关文章:

java - 使用我的类包含的对象实现所有接口(interface)方法

ios - 使用 UIViews...太多了吗? iOS

oop - 接口(interface)是否与多重继承冗余?

c# - 为什么 GC 似乎在递归函数中失败?

c# - C# 中的方法 BitConverter.DoubleToInt64Bits (Double)

c# - 如何向 Iron Python 添加模块?

java - 如何将参数传递给接口(interface)的一个实现者而不是其他实现者(不在构造函数中传递它)?

c# - 如何在多个描述字符之间拆分字符串?

c# - itunes 正在听

java - 实现接口(interface)的单例类