在这里使用泛型符合逻辑吗?

发布于 2024-10-31 00:17:21 字数 1803 浏览 2 评论 0原文

最初的代码是这样的:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

坦然微笑 2024-11-07 00:17:21

是的,这是合理的,但是当重写虚拟方法时,你必须使用 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.

素年丶 2024-11-07 00:17:21

是的,泛型是有意义的,只是因为 Internal 属性的类型有所不同。如果没有,非泛型基类也可以工作。

如果 Name 属性始终为 Internal.Name,那么您可以声明该类:

public class GenericSystem<T> : IApplicableSystem where T : IName

其中 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 be Internal.Name, then you can declare the class:

public class GenericSystem<T> : IApplicableSystem where T : IName

Where IName is an interface which has a Name property, and Effector would implement that interface, and thus you can move the definition of the Name property into the base class instead of overriding it everywhere.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文