Seven principles of design patterns
-
Single Responsibility Principle
-
core
A class is only responsible for a duty
-
advantage
- Reduce the complexity of the class
- Improve the readability and maintainability of the class
- Reduce the risk caused by changes
- If the logic is simple, you can observe a single duty on methods to reduce the amount of code
-
Examples
-
Transportation
Into a ship, aircraft, automobiles. Each class implements a responsibility
-
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:
汽车在公路上跑 飞机在空中飞 轮船在海中航行
-
-
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
-
-
Dependency Inversion Principle
- core
- High-level modules should not depend on the underlying module. Both should depend on abstractions.
- Abstract should not rely on the details, the details should rely on their abstract.
- The core is oriented programming interface.
- advantage
- Examples
- Code
- core
-
Substitution principle in style
-
core
Try not to override the parent class when the subclass inherits the parent class.
-
advantage
-
Examples
- Code
-
-
Open Closed Principle
-
core
Open for extension, but closed for modification. Abstract framework for building, implemented with extension detail.
-
advantage
-
Examples
- Code
-
-
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
-
-
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
-