实施替代抽象基类常见的行为? [英] Implement common behaviour in alternative to abstract base classes?

查看:133
本文介绍了实施替代抽象基类常见的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中,我有一个类层次结构与顶部附近的一对夫妇抽象基类和相当数量的派生类。其中的几个具体类具有了具有相同执行一些常用的属性和方法。它令我浪费,因此一个解决办法可能是实现另一个抽象基类这种常见的行为。

In C#, I have a class hierarchy with a couple of abstract base classes near the top and a fair number of derived classes. A few these concrete classes have some common properties and methods that are implemented identically. It strikes me as wasteful and so one solution might be to implement this common behaviour in another abstract base class.

abstract class Control;

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

class Window: SquareControl;
class Button: SquareControl;



但是,如果其他几类层次结构中的共享一些其他的行为,但也分享共同的东西与从另一个基类的控件之一吗?也许有很多共性的地方。它会成为不切实际的抽象基类的实现来模拟这个不会吧

However, what if several other classes in the hierarchy shared some other behaviour but also share something in common with one of the controls from another base class? Perhaps there are lots of areas of commonality. It would become impractical to model this with abstract base class implementation wouldn't it?

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

class StatusBar: FlashableControl;  // but it's also a bit square too, hmm...



那么,你如何去分享跨类这样的实现,而无需使用基类?

So how do you go about sharing such implementations across classes without using base classes?

我想我要委托给另一个类接口的implementaion并具有类实现代表的这些属性和方法所需的类,这样对用户来说,状态栏和窗口似乎支持一个标准的接口,但在幕后是别的东西实现它。

I imagine I want to delegate the implementaion of an interface to another class and have that class implement those properties and methods on behalf of the desired classes, so that to the user, the StatusBar and Window appear to support a standard interface, but under the covers it's something else that implements it.

我可以想像汇总实现此行为的类,但是这是适当的,请问有什么陷阱?有什么办法?

I can visualise aggregating classes that implement this behaviour, but is this appropriate and are there any pitfalls? What are the alternatives?

感谢

推荐答案

您可以使用这样的格局:

You can use a pattern like this:

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!");
    }
}



实现ICommonServices所有类现在还可以得到一些免费的行为通过扩展方法,这完全取决于所有ICommonServices实施者暴露的这些功能。如果您需要访问基类的功能,你可以把它自己的接口,并ICommonServices实现该接口为好。现在你可以不必使用多个基类创建界面默认的扩展功能。

All classes that implement ICommonServices now also get some free behavior via the extension method, which depends solely on those features exposed by all ICommonServices implementers. If you need access to base class functionality, you can put that in its own interface and have ICommonServices implement that interface as well. Now you can create 'default' extension functionality for interfaces without having to use multiple base classes.

修改

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

If you want some of these methods to be internal, you can modify the pattern like this:

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!");
    }
}



基本上我们揭露公共和内部接口。请注意,我们明确地执行内部接口方法,使方法无法提供公共消费(因为公共客户端不能访问该接口类型)。在这种情况下,辅助扩展方法是内部,依靠内部接口,虽然你也可以创建依靠公共接口上公共helper方法。

Basically we're exposing both public and internal interfaces. Note that we implement the internal interface methods explicitly, so that the methods are not available for public consumption (since the public client can't get access to the interface type.) In this case, the helper extension methods are internal, relying on the internal interface, though you could also create public helper methods that rely on the public interface.

这篇关于实施替代抽象基类常见的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆