原來的代碼是這樣的:在這裏使用泛型是否合乎邏輯?
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