Object-oriented design principle (1): SRP

zhaozj2021-02-12  138

SRP

In the case of a class, there should be only one reason that caused it.

What is duty? We define responsibilities as "Causes", if you can think of more motivations to change

A class, then this class has more responsibilities If a class is too duty, it is equal to coupling these responsibilities together, and a change in responsibility may suppress this class to complete other responsibilities. Example: Interface Modem {Public Void Dial (String PNO); Public Void Hangup (); Public Void Send (CHAR C); Public Void Recv ();} There are two responsibilities in the interface, one is connection management, one is data Communication, whether the two duties are separated, depending on the application's way of change, if the application changes affects the connection, then this interface has a rigid odor, which needs to be separated. Conversely, if the application changes always leads to two duties, they do not have to be separated.

转载请注明原文地址:https://www.9cbs.com/read-6552.html

New Post(0)