在以下示例中,对单一责任原则感到困惑

在以下示例中,对单一责任原则感到困惑

问题描述:

在下面的视频中,作者采用了一个现有的课程并分配了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.