Class<T> 和静态 Class,最佳实践?
Class<T> and static Class, Best Practices?
我有一个场景(简化)如下:
public static class Example
{
public const int CONSTANT_VALUE = 1;
public static Example<T> Create<T>(IEnumerable<T> argument)
where T : class
{
return new Example<T>(argument);
}
//More overloads of Create<T>, possibly other static methods, etc..
}
public class Example<T>
where T : class
{
public Example(IEnumerable<T> argument)
{
//Do Stuff
//Nothing like this in the real code, just example
//that constants are used from the other class.
if (something == Example.CONSTANT_VALUE)
{
//Do A Thing
}
}
//Lots more code
}
基本思想是我可以通过静态 class 的名称使用静态方法、常量等,而实际实现是在类型参数中非静态 class.
我的问题是这是否是设置的好方法。有没有办法放置一些不关心 Example<T>
上的类型参数是什么的静态方法和常量?还有其他更推荐的模式吗?我的工作很好,但我想知道是否还有其他方法,因为这是我第一次做这样的事情(并不是说它在概念上对我来说是新的,只是从来没有需要)。
这只有在常量为 public 时才有意义。如果它们仅供内部使用 Example<T>
那么这是没有意义的,因为您可以在没有完全限定名称的情况下引用它们。
如果常量是public用的,反正我是不会用这个模式的; Example
和 Example<T>
是两个不同的 classes,它可能会让任何用户感到困惑,而且不是很明显,非通用 class 中定义的常量适用于通用的。
您只是避免了用户的几次按键操作,我不确定这是否值得。
更新:其他选项
在这种情况下,我将使用以下工厂模式(假设用户不在您的程序集内)
public class Example<T>
{
internal Example() { } //disallow users from instantiating this class
...
}
public static class Example
{
public const int Constant = ...
public static Example<T> Create<T>() { return new ... }
}
现在所有用户将只与 Example
交互,避免使用 Example<T>
。您甚至可以对您自己的程序集的用户强制执行此操作,您只需要使 Example<T>
成为私有嵌套 class 实现 public 接口:
public interface IExample<T>
{
...
}
public static class Example
{
private class Example<T>: IExample<T> { ... }
public static IExample<T> Create<T>() { ... }
....
}
除非有某种原因这在您的情况下不起作用,否则我宁愿使用非静态基础 class Example
,然后让 Example<T>
继承自这个class。这样您就可以直接访问 Example
中的所有方法,而无需使用名称进行限定。当然,这假设 Example
class 专门用于与各种类型的 classes Example<T>
.
我有一个场景(简化)如下:
public static class Example
{
public const int CONSTANT_VALUE = 1;
public static Example<T> Create<T>(IEnumerable<T> argument)
where T : class
{
return new Example<T>(argument);
}
//More overloads of Create<T>, possibly other static methods, etc..
}
public class Example<T>
where T : class
{
public Example(IEnumerable<T> argument)
{
//Do Stuff
//Nothing like this in the real code, just example
//that constants are used from the other class.
if (something == Example.CONSTANT_VALUE)
{
//Do A Thing
}
}
//Lots more code
}
基本思想是我可以通过静态 class 的名称使用静态方法、常量等,而实际实现是在类型参数中非静态 class.
我的问题是这是否是设置的好方法。有没有办法放置一些不关心 Example<T>
上的类型参数是什么的静态方法和常量?还有其他更推荐的模式吗?我的工作很好,但我想知道是否还有其他方法,因为这是我第一次做这样的事情(并不是说它在概念上对我来说是新的,只是从来没有需要)。
这只有在常量为 public 时才有意义。如果它们仅供内部使用 Example<T>
那么这是没有意义的,因为您可以在没有完全限定名称的情况下引用它们。
如果常量是public用的,反正我是不会用这个模式的; Example
和 Example<T>
是两个不同的 classes,它可能会让任何用户感到困惑,而且不是很明显,非通用 class 中定义的常量适用于通用的。
您只是避免了用户的几次按键操作,我不确定这是否值得。
更新:其他选项
在这种情况下,我将使用以下工厂模式(假设用户不在您的程序集内)
public class Example<T>
{
internal Example() { } //disallow users from instantiating this class
...
}
public static class Example
{
public const int Constant = ...
public static Example<T> Create<T>() { return new ... }
}
现在所有用户将只与 Example
交互,避免使用 Example<T>
。您甚至可以对您自己的程序集的用户强制执行此操作,您只需要使 Example<T>
成为私有嵌套 class 实现 public 接口:
public interface IExample<T>
{
...
}
public static class Example
{
private class Example<T>: IExample<T> { ... }
public static IExample<T> Create<T>() { ... }
....
}
除非有某种原因这在您的情况下不起作用,否则我宁愿使用非静态基础 class Example
,然后让 Example<T>
继承自这个class。这样您就可以直接访问 Example
中的所有方法,而无需使用名称进行限定。当然,这假设 Example
class 专门用于与各种类型的 classes Example<T>
.