了解使用单一职责原则的实际好处 [英] Understanding the practical benefits of using the Single Responsibility Principle

查看:228
本文介绍了了解使用单一职责原则的实际好处的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解SRP但是,虽然我理解其背后如何应用它的理由,我没有真正看到这样做的好处。考虑这个例子,从罗伯特·马丁的 SRP PDF 采取:

I'm trying to understand the SRP but, whilst I understand the reasoning behind how to apply it, I'm not really seeing the benefit of doing so. Consider this example, taken from Robert Martin's SRP PDF:

interface IModem
{
    void Dial(string number);
    void Hangup();
    void Send(char c);
    char Recv();
}

他提出这个分成两个接口:

He proposes separating this into two interfaces:

interface IModemConnection
{
    void Dial(string number);
    void Hangup();
}

interface IModemDataExchange
{
    void Send(char c);
    char Recv();
}



我也一直在读的这篇文章,进一步采取这一步:

I've also been reading this article, which takes this one step further:

interface IModemConnection : IDisposable
{
    IModemDataExchange Dial(string number);
}

interface IModemDataExchange
{
    void Send(char c);
    char Recv();
}



在这一点上,我明白了什么是功能性的(<$ C $的意思C>发送/ RECV )和非功能(拨号/挂断)方面,但我没有看到他们在这分离的好处例。考虑到这一基本实现:

At this point, I understand what is meant by functional (Send / Recv) and non-functional (Dial / Hangup) aspects, but I don't see the benefit of separating them in this example. Considering this basic implementation:

class ConcreteModem : IModemConnection
{
    public IModemDataExchange Dial(string number)
    {
        if (connection is successful)
        {
            return new ConcreteModemDataExchange();
        }

        return null;
    }

    public void Dispose()
    {
        // 
    }

    public bool IsConnected { get; private set; }
}



在这一点上,让我再次引用罗伯特·马丁,虽然他在说大约从PDF不同的例子:

At this point, let me quote Robert Martin again, even though he's talking about a different example from that PDF:

其次,如果更改了GraphicalApplication使矩形由于某种原因发生变化,这种变化,我们可能被迫重建,复试,并重新部署ComputationalGeometryApplication 。如果我们忘记了这样做,该应用程序可能会破坏不可预知的方式。

Secondly, if a change to the GraphicalApplication causes the Rectangle to change for some reason, that change may force us to rebuild, retest, and redeploy the ComputationalGeometryApplication. If we forget to do this, that application may break in unpredictable ways.

这是我不明白。如果一定要我创建第二个实施 IModemDataExchange ,我想利用这一点,我仍然要修改拨号方法,这意味着该类还需要重新编译:

This is what I don't understand. If I had to create a second implementation of IModemDataExchange, and I wanted to make use of that, I would still have to change the Dial method, meaning the class also needs to be recompiled:

public IModemDataExchange Dial(string number)
{
    if (some condition is met)
    {
        return new ConcreteModemDataExchange();
    }
    else if (another condition is met)
    {
        return new AnotherConcreteModemDataExchange();
    }

    return null;
}



我不明白这是什么做,以减少对变化的影响班上。它仍然需要重新编译,所以有什么好处?你怎么不做这一点,是生产高质量的代码,所以重要的收获?

I can't see what this has done to reduce the effects of change on the class. It still needs to be recompiled, so what's the benefit? What do you gain from doing this that is so important to producing quality code?

推荐答案

要在我之上似乎总是喜欢调制解调器例子对于接口隔离原则而非SRP,但是这是除了一个很好的例子。

To me the modem example above always seemed like a case for the interface segregation principle rather than SRP, but that's besides the point.

在你打电话了部分关于长方形,我认为你只是误解了。马丁使用矩形作为共享库的一个例子。如果 GraphicalApplication 要求在长方形类语义的新方法和新变化,那么,影响 ComputationalGeometryApplication 因为他们都链接到长方形库。他称这违反了SRP,因为它是负责定义渲染界限,也代数的概念。试想一下,如果从的DirectX变更为OpenGL的情况下y坐标反转 GraphicalApplication 。你可能想改变周围的长方形来推动这一一些事情,但你可能随后在 ComputationalGeometryApplication 。

In the part you called out regarding the Rectangle, I think you're just misinterpreting it. Martin is using the Rectangle as an example of a shared library. If the GraphicalApplication requires a new method or change in semantics in the Rectangle class, then that affects the ComputationalGeometryApplication since they both "link" to the Rectangle library. He's saying it violates SRP because it is responsible for defining rendering bounds and also an algebraic concept. Imagine if the GraphicalApplication changed from DirectX to OpenGL where the y-coordinate is inverted. You may want to change some things around on the Rectangle to facilitate this, but you're then potentially causing changes in ComputationalGeometryApplication.

在我的工作,我尽量遵循的 SOLID 原则和TDD,而我发现,SRP使得类简单的编写测试,也保留了类集中。下面的SRP类通常非常小,这减少了在代码和依赖性的复杂性。当删空班,我尽量确保一个类可以是做一件事,或者协调两个(或更多)的事情。这让他们专注,使他们对变革的原因,只有依赖于一件事,他们做的,这对我来说是SRP的地步。

In my work, I try to follow the SOLID principles and TDD, and I've found that SRP makes writing tests for classes simple and also keeps the classes focused. Classes that follow SRP are typically very small, and this reduces complexity both in code and dependencies. When stubbing out classes, I try to make sure a class is either "doing one thing", or "coordinating two (or more) things". This keeps them focused and makes their reasons for change only dependent on the one thing they do, which to me is the point of SRP.

这篇关于了解使用单一职责原则的实际好处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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