The idea of SRP was first proposed by Tom DeMarco and is an important component of cohesion.
Each object should have only one responsibility, and the services provided by this object should be focused on their responsibilities.
* The object here is different from the instance of classes in OO. According to process-oriented programming, a function can also be called an object.
Uncle Bob re-interpreted this principle in the Object-Oriented field (download Uncle Bob's interpretation ).Object-Oriented Design PrinciplesOne.
Uncle Bob pointed out:
A class cannot have any extra reason for its changes.
Uncle Bob's views and Tom's views are as follows:Quite differentTom emphasizes that an object can only have one responsibility, while Bob emphasizes that a class can only have one reason for change. So what exactly does Bob want to express? Can a class have only one function (Responsibility )? If a class can only have one method, then weProgramThere are not many classes in it. With questions, let's take a look at Bob's book 《Agile Software Development: Principles, patterns, and practices.
This principle was described in the work of Tom demarco1 and meilir Page-Jones2. They
Called it
Cohesion
It turns out that Uncle Bob is talking about a single task of high cohesion. That's right. How should we design our classes? In combination, the SRP principle includes two aspects:
- First, the function should be highly cohesive (Tom's point of view)
- Part that is easy to cause changes is isolated (Bob's point of view)
1.High Cohesion: In a sense, a high cohesion means that a good abstraction can ignore irrelevant details as much as possible. The goal isComplexity Reduction. Specifically:
All external interfaces (including method and property) in the class are highly consistent with each other on a central target.
If we need a materil class in the BOM system, we can obtain the name, main supplier, and all external interfaces such as suppliers after initialization.
Materiel class Interface
- Materiel (string materielid );
- String materielid {Get ;}
- String name {Get ;}
- Vendor mainvendor {Get ;}
- Vendorcollection getallvendor ();
The external interfaces of the materiel class are an abstract service of materials. Although the internal implementation of material has other attributes of the service class, such as basicinfodal. material, basicinfodal. there are two Vendor classes. however, customers (callers) of the material class do not know, nor do they need to know their internal implementation.
2.Isolation of easy-to-change points: This is Uncle Bob's core point of view. It is also the point of View on SRP in most books. The goal is to make it easier.ReuseAndReduce modification costsThe common practice is to split the class.
Violation of SRR means that unrelated responsibilities are coupled together, leading to the fragile "Porcelain design", which allows later users and even the original author
I dare not easily move this class. The last common case is to copy a copy for each caller.Code. Corrupt design;
Common violations include:
- Too large class;
- Omnipotent/hodgedge;
- Business logic and persistence Processing
- Business logic and GUI Processing
- This is a common mistake made by new programmers,Universal super page
Too large class: One of the common code stinks is that too many tasks are added to the class. in my personal practice, if a class has more than 7 public methods or more than 300 lines of code, I can check whether the class can be improved.
Omnipotent/hodgedge: What can be done and what can be known. hodgedge is common in some enterprises, and even has a standard name common. CS, any unexpected stuff is added to it. to solve this problem, we recommend that you use a better class name when creating common. eliminate common. CS
Business logic and persistence Processing: Normally, there is no independent Dal layer, and the logic and SQL are written in one method. step by step code and construct an SQL statement to check the data check result. keep repeating. maintenance is still a big problem when the program is slow.
Business logic and GUI Processing: It is common to pass controls to the class, and even the class itself has a reference of frm. the methods of the non-Form class include *. TEXT = "***"; or Int xxx = convert. toint32 (**. text. trim () Similar code ;*. enable = true is similar to the code.
Universal super page:There are two special benefits for this name to be highlighted. One is because this stuff is very common, and new siblings must have forgotten the interface to sigh. I did not know how to do this before. I have done this before,
Once this problem occurs, there will be no 3 CS files in the project without Aspx. In addition, all operations are performed on the page, it is even completed in *** _ click (Object sender, ** eventarg E. this type of super page is calledUniversal pageWhen the business logic is a little more complex, the number of lines of code will constantly refresh records. According to Microsoft's statistics, the per-capita bitrate of large teams is 3 K, you will find thatUniversal super pageIt is easy to win an engineer's one-year output, and keep a few backups for the next year.
Bytes ---------------------------------------------------------------------------------------------------
It is rare to have a leisurely weekend and talk about the most basic and simple oo principles,ArticleConcept, generation time is short, there must be a lot of deficiencies and errors, please note, another small statistics
How long is the longest code document you have ever seen?
I have seen the longest code document (*. aspx + *. aspx. CS) with 4 K lines, which can be produced by about 16 persons in a month.