实现除抽象基类之外的常见行为?

DaB*_*zUK 7 c# implementation share interface

在C#中,我有一个类层次结构,顶部附近有几个抽象基类,并有相当数量的派生类.这些具体类中的一些具有一些相同实现的共同属性和方法.它让我感到浪费,因此一种解决方案可能是在另一个抽象基类中实现这种常见行为.

abstract class Control;

abstract class SquareControl: Control
{
    public int SquarishProperty;
    public void SquarishMethod();
};

class Window: SquareControl;
class Button: SquareControl;
Run Code Online (Sandbox Code Playgroud)

但是,如果层次结构中的其他几个类共享某些其他行为,但是与另一个基类中的一个控件共享某些内容,该怎么办?也许有很多共同点.使用抽象基类实现对它进行建模是不切实际的吗?

abstract class FlashableControl: Control
{
    public int FlashyProperty;
    public void FlashMethod();
};

class StatusBar: FlashableControl;  // but it's also a bit square too, hmm...
Run Code Online (Sandbox Code Playgroud)

那么如何在不使用基类的情况下跨类共享这样的实现呢?

我想我想将接口的实现委托给另一个类,并让该类代表所需的类实现这些属性和方法,这样对于用户来说,StatusBar和Window似乎支持标准接口,但是在涵盖它是实现它的其他东西.

我可以想象实现这种行为的聚合类,但这是否适当,是否有任何陷阱?有哪些替代方案?

谢谢

Dan*_*ant 3

您可以使用这样的模式:

public interface ICommonServices
{
    string SomeProperty { get; set; }

    void SomeMethod(string param);
}

public static class CommonServiceMethods
{
    public static void DoSomething(this ICommonServices services, string param)
    {
        services.SomeMethod(services.SomeProperty + ": " + param + " something extra!");
    }
}
Run Code Online (Sandbox Code Playgroud)

所有实现 ICommonServices 的类现在还可以通过扩展方法获得一些自由行为,这仅取决于所有 ICommonServices 实现者公开的那些功能。如果您需要访问基类功能,您可以将其放入其自己的接口中,并让 ICommonServices 也实现该接口。现在,您可以为接口创建“默认”扩展功能,而无需使用多个基类。


编辑

如果您希望其中一些方法是内部的,您可以像这样修改模式:

public class MyObject : IServices
{
    public string PublicProperty { get; private set; }

    string IServices.SomeProperty { get; set; }

    void IServices.SomeMethod(string param)
    {
        //Do something...
    }
}

public interface IPublicServices
{
    string PublicProperty { get; }
}

internal interface IServices : IPublicServices
{
    string SomeProperty { get; set; }

    void SomeMethod(string param);
}

internal static class ServiceMethods
{
    public static void DoSomething(this IServices services, string param)
    {
        services.SomeMethod(services.SomeProperty + ": " + param + " something extra!");
    }
}
Run Code Online (Sandbox Code Playgroud)

基本上我们公开了公共和内部接口。请注意,我们显式实现内部接口方法,因此这些方法不可用于公共使用(因为公共客户端无法访问接口类型。)在这种情况下,帮助器扩展方法是内部的,依赖于内部接口,尽管您也可以创建依赖于公共接口的公共帮助器方法。