Design Patterns seven principles (study notes)

Seven principles of design patterns

  1. Single Responsibility Principle

    • core

      A class is only responsible for a duty

    • advantage

      1. Reduce the complexity of the class
      2. Improve the readability and maintainability of the class
      3. Reduce the risk caused by changes
      4. If the logic is simple, you can observe a single duty on methods to reduce the amount of code
    • Examples

      1. Transportation

        Into a ship, aircraft, automobiles. Each class implements a responsibility

      2. DAO class

        A DAO is responsible for a list of additions and deletions to change search.

    • Code

      Reference code portion of the content is still Silicon Valley Hanshun Ping teacher.

      /**
      * @program:design_pattern
      * @descript:公路
      * @author: luyongjian746
      * @create: 2020-02-11
      */
      public class Road {
      
         public void run(String vehicle) {
             System.out.println(vehicle + "在公路上跑");
         }
      }
       /**
        * @program:design_pattern
        * @descript:天空
        * @author: luyongjian746
        * @create: 2020-02-11
        */
       public class Sky {
      
           public void run(String vehicle) {
               System.out.println(vehicle + "在空中飞");
           }
       }
      /**
      * @program:design_pattern
      * @descript:海
      * @author: luyongjian746
      * @create: 2020-02-11
      */
      public class Sea {
      
         public void run(String vehicle) {
             System.out.println(vehicle + "在海中航行");
         }
      }
      /**
      * @program:design_pattern
      * @descript:测试
      * @author: luyongjian746
      * @create: 2020-02-11
      */
      public class Test {
      
         public static void main(String[] args) {
             Road road = new Road();
             road.run("汽车");
      
             Sky sky = new Sky();
             sky.run("飞机");
      
             Sea sea = new Sea();
             sea.run("轮船");
         }
      }

      result:

      汽车在公路上跑
      飞机在空中飞
      轮船在海中航行
  2. Interface Segregation Principle

    • core

      The client should not rely on his unwanted interfaces. I.e., a class dependent of another class, should be based on the smallest interface.

      Interface disassembled into large number of small interface.

    • advantage

    • Examples

    • Code
  3. Dependency Inversion Principle

    • core
      1. High-level modules should not depend on the underlying module. Both should depend on abstractions.
      2. Abstract should not rely on the details, the details should rely on their abstract.
      3. The core is oriented programming interface.
    • advantage
    • Examples
    • Code
  4. Substitution principle in style

    • core

      Try not to override the parent class when the subclass inherits the parent class.

    • advantage

    • Examples

    • Code
  5. Open Closed Principle

    • core

      Open for extension, but closed for modification. Abstract framework for building, implemented with extension detail.

    • advantage

    • Examples

    • Code
  6. Demeter

    • core

      A class should be kept to a minimum understanding of other objects.

      If not direct friends, there are other categories of local variables in a method, a violation of the Law of Demeter.

      It refers to direct friends, a method parameters, return value used by class, the method for such direct friend belongs.

    • advantage

    • Examples

    • Code
  7. Synthesis of multiplexing principles

    • core

      To make use of synthetic or replaced multiplexed inheritance.

      Synthesis: A class as a member variable or parameter appears in the form of Class B

      Multiplexing: A class to new A () appears in the manner as in class B

    • advantage

    • Examples

    • Code

Guess you like

Origin blog.51cto.com/12133258/2470405