在以下示例中,对单一责任原则感到困惑 [英] Confused about Single Responsibility Principle in the following example

查看:73
本文介绍了在以下示例中,对单一责任原则感到困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的视频中,作者采用了一个现有的课程并分配了Single责任原则.他参加了打印班,负责访问数据,格式化和打印报告.他将每个方法分解为自己的类,因此他创建了一个DataAccess类来处理数据访问,他创建了一个ReportFormatter类来处理Report的格式,并且他创建了一个ReportPrinter类来处理Report的打印.然后,将原始的Report类留给一个方法Print(),该方法调用ReportPrinter的类方法Print. DataAccess和ReportFormatter似乎有责任,但是ReportPrinter依赖于DataAcess和ReportFormatter,所以这不会破坏SRP还是我误会了?

In the following video, the author takes an existing class and assigns the Single Responsibility Principle to it. He takes a Print Class that has the job of Accessing Data, Formatting, and Printing the report. He breaks up each method to its own class, so he creates a DataAccess class to handle data access, he creates a ReportFormatter class to handle the formatting of the Report, and he creates a ReportPrinter class to handle the printing of the Report. The original Report class is then left with one method, Print() which calls the ReportPrinter's class method Print. DataAccess and ReportFormatter appear to have responsibility, but ReportPrinter relies on DataAcess and ReportFormatter, so doesn't this break the SRP or am I misunderstanding it?

推荐答案

单一责任原则"指示给定的班级应负单一责任(或改变理由").它本身并没有表明应如何履行这一责任.这样做可能而且通常确实需要其他多个类作为合作者进行合作.

The Single Responsibility Principle indicates that a given class should have a single responsibility (or 'reason to change'). It does not, in and of itself, indicate how that responsibility is to be satisfied. Doing so can, and often does, require the cooperation of multiple other classes as collaborators.

这篇关于在以下示例中,对单一责任原则感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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