Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities

This article also participated in the “Digitalstar Project” to win a creative gift package and creative incentive money

Recently, I have been learning six design principles and I have been enlightened. Now I will simply record my feelings.

define

The Single responsibility principle states that each class should have a Single function, and that function should be fully encapsulated by that class. All of its services should be strictly parallel to the functionality (functionality parallel, meaning no dependencies).

In layman’s terms, a class does only one job. There are no more than one reason for a class to change.

advantages

  • Class complexity is reduced, a class is responsible for only one function, its logic is much simpler than responsible for multiple functions;
  • Class readability, easy to read;
  • High maintainability, an easy to read, simple class is also easy to maintain;
  • The risk reduction caused by change is inevitable, and if the single responsibility principle is followed well, when modifying one function, the impact on other functions can be significantly reduced.

example

Here is a famous game to do an example, such as the same, pure coincidence ha. Assuming that each summoner can have three heroes, we can define a Role class with the getheros method

The code is as follows:

Class Role {func getheros() -> [String] {return [" yip ", "yIP "," yIP "]}}Copy the code

This is very simple, right? At this time, the product comes to say that the demand needs to be changed, add a VIP and civilian two roles, VIP1 ~ VIP7 to add a hero. After VIP8, there will be 2 more heroes.

Ok, in this case we’ll just get an enumeration to define the character and swit through the enumeration to return the hero.

The code is as follows:

enum IDE { case vip(Int) case pingmin } class Role { func getheros(ide: IDE) -> [String] { switch ide { case let .vip(number): If (number > = 8) {return [" Arthur ", "descendants", "da ji", "zhaoyun", "wu zetian"]} else {return [" Arthur ", "descendants", "da ji", "zhaoyun"]} case. The pingmin: Return [" c ", "d "," d "]}}}Copy the code

Look at it that way, it’s not that hard.

With the iteration of requirements, at this time, the product said that the character added the fighting function, civilian combat capacity at the beginning of 0, viP1 ~ VIP7 is 10, after vip8 is 20.

At this time, in fact, we feel that something is wrong, but on the original basis, it doesn’t seem to change very much. Ok, change again

class Role { func getheros(ide: IDE) -> [String] { switch ide { case let .vip(number): If (number > = 8) {return [" Arthur ", "descendants", "da ji", "zhaoyun", "wu zetian"]} else {return [" Arthur ", "descendants", "da ji", "zhaoyun"]} case. The pingmin: }} func fight(ide: ide) -> Int {switch ide {case let. VIP (number): if (number >= 8) { return 20 }else { return 10 } case .pingmin: return 0 } } }Copy the code

At this time, the product demand, TM increased, VIP role has the skin display function, and after VIP8 can have the group building function.

If the above code base, is it necessary to add two methods, and then enumerate traversal to output the corresponding content. At this point, we realized that the Role class was becoming bloated, and it would be difficult to maintain if the product was changed or added later.

The single responsibility principle says that a class is responsible for only one responsibility, and obviously the Role class is already responsible for many responsibilities. Civilians have 3 heroes, vip1 to VIP7 have 4 heroes, and VIP8 have 5 heroes after them. This is the single duty principle, not the character having 3 or 4 or 5 heroes.

Ok, so we can create a class named pinmin, 1 class vip1,vip2… Each identity has its own function.

class pingmin: Role {override func getheros() -> [String] {return [" ", "" ", } override func fight() -> Int {return 0}} class Vip1: Role {override func getheros() -> [String] {return [" ", } override func fight() -> Int {return 10} func showPiFu() {print(" showPiFu ")} class Vip8: Role {override func getheros() -> [String] {return [" ", "" "," "" "," } override func fight() -> Int {return 20} func havaPiFu() {print(" show skin ")} func createGroup() {print(" show skin ")} }Copy the code

After that, if any identity needs to add functionality, we can add functionality directly to that class, so that the other classes are not affected at all, the coupling is greatly reduced, and the code is not all bloated in one class.

conclusion

Single responsibility principle is six principle is one of the more simple in design, but how can, in the actual development, along with the increase in function, to pay attention to in time, to see if there is a breach of a class is responsible for a responsibility, only can refactor immediately, or code capacity is much, want to refactor and difficult, and hard to continue to have the original on the basis of maintenance.

If there are shortcomings in the above statement, please take note.