在这里使用泛型符合逻辑吗?
最初的代码是这样的:
public interface IApplicableSystem
{
string Name { get; }
void Apply ( );
}
public class Effector
{
public string Name { get; set; }
}
public class EffectSystem : IApplicableSystem
{
Effector Internal { get; set; }
public string Name
{
get { return this.Internal.Name; }
}
RelayCommand applyCommand;
public ICommand ApplyCommand
{
get
{
if ( applyCommand == null )
applyCommand = new RelayCommand ( p => this.Apply ( ) );
return applyCommand;
}
}
public void Apply ( )
{
}
public EffectSystem ( Effector value )
{
this.Internal = value;
}
}
所以有很多不同的类型实现 IApplicableSystem ,它们的不同之处在于它们的 Name 属性、Apply 方法和在类内部使用的私有 Internal
属性的类型。
我应该使用泛型来使它们像这样吗?:
这合理吗?这主要减少了实现 IApplicableSystem 的其他类型中的代码量。
public abstract class GenericSystem<T> : IApplicableSystem
{
protected T Internal { get; set; }
public virtual string Name
{
get { return String.Empty; }
}
RelayCommand applyCommand;
public ICommand ApplyCommand
{
get
{
if ( applyCommand == null )
applyCommand = new RelayCommand ( p => this.Apply ( ) );
return applyCommand;
}
}
public virtual void Apply ( )
{
}
public GenericSystem ( T value )
{
this.Internal = value;
}
}
public class EffectSystemGeneric : GenericSystem<Effector>
{
public override string Name
{
get { return GetUniqueName ( base.Internal.Name ); }
}
public override void Apply ( )
{
Console.WriteLine ( "Do something" );
}
}
Originally the code is like this:
public interface IApplicableSystem
{
string Name { get; }
void Apply ( );
}
public class Effector
{
public string Name { get; set; }
}
public class EffectSystem : IApplicableSystem
{
Effector Internal { get; set; }
public string Name
{
get { return this.Internal.Name; }
}
RelayCommand applyCommand;
public ICommand ApplyCommand
{
get
{
if ( applyCommand == null )
applyCommand = new RelayCommand ( p => this.Apply ( ) );
return applyCommand;
}
}
public void Apply ( )
{
}
public EffectSystem ( Effector value )
{
this.Internal = value;
}
}
So there are many different types implementing IApplicableSystem
, all where they differ is their Name
property, Apply
method and the type of the private Internal
property that's used inside the class.
Should I use generic to make them like this?:
Is this reasonable? Mainly this reduces the amount of code in the other types implementing IApplicableSystem
.
public abstract class GenericSystem<T> : IApplicableSystem
{
protected T Internal { get; set; }
public virtual string Name
{
get { return String.Empty; }
}
RelayCommand applyCommand;
public ICommand ApplyCommand
{
get
{
if ( applyCommand == null )
applyCommand = new RelayCommand ( p => this.Apply ( ) );
return applyCommand;
}
}
public virtual void Apply ( )
{
}
public GenericSystem ( T value )
{
this.Internal = value;
}
}
public class EffectSystemGeneric : GenericSystem<Effector>
{
public override string Name
{
get { return GetUniqueName ( base.Internal.Name ); }
}
public override void Apply ( )
{
Console.WriteLine ( "Do something" );
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,这是合理的,但是当重写虚拟方法时,你必须使用 override 关键字,而不是 virtual。您可能希望将通用基类抽象化,以强制实现者提供实现。
Yes, this is reasonable, but when overriding a virtual method, you have to use the override keyword, not virtual. You may want to make the generic base class abstract instead, to force implementers to provide an implementation.
是的,泛型是有意义的,只是因为
Internal
属性的类型有所不同。如果没有,非泛型基类也可以工作。如果
Name
属性始终为Internal.Name
,那么您可以声明该类:其中
IName
是一个具有的接口Name
属性,而Effector
将实现该接口,因此您可以将Name
属性的定义移动到基类中,而不是在任何地方重写它。Yes, the generic makes sense, only because the type of the
Internal
property varies. If it did not, a non-generic base class would work just as well.If the
Name
property will always beInternal.Name
, then you can declare the class:Where
IName
is an interface which has aName
property, andEffector
would implement that interface, and thus you can move the definition of theName
property into the base class instead of overriding it everywhere.