原来的代码是这样的:在这里使用泛型是否合乎逻辑?
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");
}
}
谢谢编辑了代码。 – 2011-04-06 20:11:21