Single Responsibility seven principles of design patterns learning principles

  In the process of writing software, our programmers facing from coupling, cohesion and maintainability, various challenges scalability, reusability, flexibility, and design patterns to better program

  1, code reuse: the same function code, do not write many times

  2, Readability: programming specifications, ease of reading and understanding of other programmers

  3, scalability: When you need to add new features, very convenient --- maintainable

  4. Reliability: When we add features, has no effect on the original function

  5, so that the program exhibits high cohesion properties, low coupling

  Design Patterns principle, in fact, is based on the principle of programmers in programming should be observed, and it is designed based on patterns of various design patterns.

  PS: "widen the gap with others, often in their spare time."

 First, the seven principles of design patterns

  ① Single Responsibility Principle

  ② Interface Segregation Principle

  ③ Dependency Inversion Principle

  ④ Richter substitution principle

  ⑤ Open Closed Principle

  ⑥ Demeter

  ⑦ synthesis multiplexing principle

Second, the single responsibility principle

  Basic introduction : for classes that a class should only be responsible for a duty. Such as class A charge of two different responsibilities: Responsibilities, responsibilities 2. When demand changes and changes in duties 1 A, may result in duties of 2 execution error, it needs to be broken down into size classes A, A1, A2.

  Test scenarios: vehicle class

1, a test program

 Analysis :

  ① obvious violation of the Single Responsibility Principle ( a class should only be responsible for a duty )

  ② the vehicle based decomposition scheme 2 -----

2. Option II test

 Analysis :

  ① comply with Single Responsibility Principle ( a class should only be responsible for a duty )

  ② such great changes, the type of decomposition while modifying the client

  ③ improvements: directly modifying the vehicle class, relatively small changes in Scheme 3 -----

3. Option III test

 Analysis :

  ① modify this method does not modify the original class bigger, just add method

  ② Although there is no single comply with the principle of responsibility at the level of the class, but at the method level, is still the single responsibility principle

4, single responsibility principle Summary

(1) reduce the complexity of the class, a class is only responsible for a duty

(2) improve the readability class, maintainability

(3) reduce the risks of change

Guess you like

Origin www.cnblogs.com/rmxd/p/12535842.html