单一责任原则-一个很难看到的例子? [英] Single Responsibility Principle - A hard to see example?

查看:69
本文介绍了单一责任原则-一个很难看到的例子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚阅读了《单一责任原则》,罗伯特·C·马丁(Robert C. Martin)曾指出,有时很难看到一堂课承担着多个责任.

I've just read about the Single Responsibility Principle and at one point Robert C. Martin states that it is sometimes hard to see that a class has more than one responsibility.

任何人都可以提供此类课程的示例吗?

Can anyone provide an example of such a class?

推荐答案

考虑一个具有方法

  • 获取(URL网址)
  • SendRequest(字符串请求)

这两种方法都与HTTP有关.但是,Get和SendRequest具有不同的抽象级别. Get可能实际上使用SendRequest发送GET请求.因此,SendRequest应该在低级HTTP类中,而Get应该在使用低级HTTP类的高级HTTP类中.

Both of these methods have to do with HTTP. However, Get and SendRequest have different levels of abstraction. Get may actually use SendRequest to send the GET request. Therefore, SendRequest should be in a low-level HTTP class, and Get should be in a high-level HTTP class which uses the low-level one.

这篇关于单一责任原则-一个很难看到的例子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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