一个好的做法留下了一个空的界面?

Dar*_*Zon 2 c# interface

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

例如:

BinaryProblemConfiguration
    + Bound1 : Bound<int>
    + Bound2 : Bound<int>
Run Code Online (Sandbox Code Playgroud)

Bound有两个属性:min和max.

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

所以,我在想创建一个名为IConfiguration的接口.

public interface IConfiguration {}
Run Code Online (Sandbox Code Playgroud)

具体的配置应该是:

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

public class AnotherProblemConfiguration : IConfiguration
{
    // other stuff
}
Run Code Online (Sandbox Code Playgroud)

我们的想法是拥有一个名为ConfigurationLevels的字典.这是一个很好的做法让界面空了或者我的设计意味着错误吗?

Jon*_*len 8

.NET Framework设计指南将其称为"标记"界面,并且肯定地说这是一个坏主意.他们建议使用自定义属性.

避免使用标记接口(没有成员的接口).

自定义属性提供了标记类型的方法.有关自定义属性的更多信息,请参阅编写自定义属性.如果可以在执行代码之前推迟检查属性,则首选自定义属性.如果您的方案需要编译时检查,则无法遵守此准则.

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

public sealed class ConfigurationAttribute : Attribute {

}


[ConfigurationAttribute]
public class AnotherProblemConfiguration : IConfiguration 
{ 
    // other stuff 
} 
Run Code Online (Sandbox Code Playgroud)