1. Seven design principles of Java design pattern and UML class diagram

xiaocantian 2020-11-09 17:44:34
design principles java design pattern

The purpose of the design pattern :

  • Code reusability ( namely : Code for the same function , Not many times )
  • Readability ( namely : Programming normalization , Easy for other programmers to read and understand )
  • Extensibility ( namely : When new features need to be added , Very convenient , It's called maintainable )
  • reliability ( namely : When we add new features , It has no effect on the original function )
  • Make the program present high cohesion , Low coupling characteristics

1、 Principle of single responsibility

​ For a class , That is, a class should only be responsible for one responsibility , Split the class . Such as class A Responsible for two different responsibilities : duty 1, duty 2. Responsibility 1 Requirements change and change A when , May cause responsibility 2 Perform error , So we need to put the class A The granularity of is broken down into A1,A2, You can also implement a single responsibility at the method level

matters needing attention :

  • Reduce class complexity , A class is responsible for only one responsibility
  • Improve the readability of the class , Maintainability
  • Reduce the risk of change
  • Usually , We should abide by the principle of single responsibility , Only logic is simple enough , To break the single responsibility principle at the code level ; There are only enough methods in the class , The principle of single responsibility can be maintained at the method level

2、 Interface isolation principle

​ The client should not rely on interfaces it does not need , namely The dependency of one class on another should be based on the smallest interface ( Eliminate useless methods ), Split the interface , Abstract the common parts of multiple interfaces into a parent interface , Subinterfaces have only their own methods , Implementation classes do not need to implement invalid methods .A For the class ,B Is the implementation class of the interface , call A Method time , Implementation class of interface B Is the method parameter ( be used B class ), Just say A Depending on this interface B class

3、 Dependence Inversion Principle

  • High level modules should not rely on low level modules , Both should rely on their abstractions

  • Abstraction should not depend on details , Details should depend on abstraction

  • The central idea of dependency inversion is interface oriented programming ( Interface specification rules , The details are left to the implementation class )

    Three ways of dependency transmission

    1. Interface delivery ( Ordinary method parameters directly accept interfaces as parameters )
    2. Construct method passing ( The interface is passed to the class as a constructor parameter )
    3. setter Method ( Use setter Method to pass the interface inside the class )

4、 Richter's principle of substitution

The problem of inheritance :

  • All the implemented methods in the parent class , It's actually setting norms and contracts , Although it doesn't force all subclasses to follow these contracts , But if a subclass modifies any of these implemented methods , It will damage the whole inheritance system
  • Using inheritance can be intrusive , The portability of the program is reduced , Increase coupling between objects , If a class is inherited by another class , When this class needs to be modified , All subclasses must be considered , And after the parent class is modified , All functions involving subclasses are likely to fail

An overview of the principles :

​ If for each type is T1 The object of o1, All of them are of type T2 The object of o2, So that T1 All programs defined P In all objects o1 All generations into o2 when , Program P There is no change in behavior , So the type T2 It's the type T1 In other words , All references to the base class must transparently use the object of its subclass

​ Unconsciously override the parent method , Make the program result incorrect , Solution : Create a more basic base class , Let the two classes get rid of the inheritance relationship

matters needing attention :

  • When using inheritance , Follow the Richter principle of substitution , Try not to override the method of the parent class in the child class
  • Inheritance actually enhances the coupling of the two classes , Where appropriate , You can aggregate , Combine , Rely on to solve problems

5、 Opening and closing principle (ocp)

  • A software entity such as class , Modules and functions should be Open to expansion ( To the provider ), Yes Modify close ( For the user ). Building framework with abstraction , Extend details with implementation
  • When the software needs to change , As far as possible Change is achieved by extending the behavior of software entities , instead of Change by modifying existing code
  • There's still interface oriented programming

6、 Dimitar's law

  • Dimitar's law (Demeter Principle) Also known as the least known principle , It's a class The less you know about the classes you depend on, the better . in other words , No matter how complex the dependent classes are , Try to encapsulate the logic inside the class . In addition to the external public Method , Don't divulge any information , There's a simpler definition of Dimiter's law : Communicate only with direct friends
    • A direct friend : Each object is coupled to other objects , As long as there is a coupling relationship between two objects , We say that the relationship between the two objects is friendship . There are many ways of coupling , rely on , relation , Combine , Polymerization, etc. . among , We call it appear Member variables , Method parameter , The class in the return value of the method is a direct friend , And the class that appears in the local variable is not a direct friend . in other words , A strange class is best not to appear as a local variable inside the class
    • More popular : That is, in the method of a class new The variable that comes out is a strange class

matters needing attention :

  • The core of Dimiter's law is to reduce the coupling between classes
  • But notice : Because each class reduces unnecessary dependencies , So Dimitri's law only requires that classes be reduced ( Between objects ) Coupling relationship , It's not a requirement to be completely independent

7、 Synthetic multiplexing principle

  • The principle is to use composition as much as possible / Way of aggregation , Instead of using inheritance

8、 The core idea of design principle

  • Find out what changes may be needed in the application , Separate them out , Don't mix with code that doesn't need to change
  • Programming to an interface , Instead of programming for implementation
  • For loose coupling design between interactive objects

Two 、UML Class diagram

1、 Basic introduction

  • UML——Unified modeling language UML( Unified modeling language ), It is a language tool for software system analysis and design , It is used to help software developers think and record the results of ideas
  • UML It is a set of rules of symbols , Just like mathematical symbols and chemical symbols , These symbols are used to describe the various elements in the software model and their relationships , Such as class 、 Interface 、 Realization 、 generalization 、 rely on 、 Combine 、 Polymerization, etc.
  • They describe their thoughts to others , The key is thinking and organization
  • UML Picture classification
    • Use case diagram
    • Static structure diagram : Class diagram 、 Object graph 、 Package diagram 、 Component diagram 、 Deployment diagram
    • Dynamic behavior diagram : Interaction diagram ( Sequence diagram and collaboration diagram )、 State diagram 、 Activity diagrams
  • UML Class diagram
    • Used to describe classes in the system ( object ) Its composition and class ( object ) All kinds of static relations between
    • Relationships between classes : rely on 、 generalization ( Inherit )、 Realization 、 relation 、 Aggregation and combination

2、 rely on

  • As long as the other party is used in the class , Then there is a dependency between them , If there is no other party, you can't compile
  • Symbol ---------->(dependency)
  • A Class B class ,A rely on B, The arrow points to B

3、 generalization

  • Generalization is actually inheritance
  • Symbol ——————————▷(generalization)
  • A Class inheritance B class ,A Inherit B, The arrow points to B

4、 Realization

  • It's the interface implementation relationship
  • Symbol ----------▷(implementation)
  • A Class implementation B class ,A Realization B, The arrow points to B

5、 relation (association)

  • Association is actually the relationship between classes , A special case of dependency

  • Relevance is oriented : It's a two-way relationship ( A person has an ID card , A personal ID card ( stay Person Classes and IDCard Class to create each other's entity properties )) And a one-way relationship ( A person has an ID card ( Only in Person Class IDCard Attributes of a class )), Self correlation ( Attributes of this class exist in objects of this class ( A tree structure ))

  • Correlation has multiplicity : Such as '1'( There is only one ),'0...'( Express 0 One or more ),'0,1'( Express 0 A or a ),'n...m'( Express n To m Every one can ),'m...*'( At least m individual )

  • type

    • One way Association : Symbol Person——————————>IDcard
    • Two way connection : Symbol Person——————————IDcard
    • Self correlation : Symbol TreeNode——————————>TreeNode

6、 polymerization

  • An aggregate relationship is a relationship between the whole and the part , The whole and the part can be separated . The aggregation relationship is A special case of association , So he's connected Navigation and multiplicity
  • Such as : A computer consists of a keyboard (keyboard)、 Monitor (monitor), Mouse, etc ; The components of a computer can be separated from the computer , After being stripped out, it can exist alone
  • Symbol ——————————◇(aggregation, Hollow diamond )
  • monitor——————————◇keyboard

7、 Combine

  • synthetic relation : It's also the relationship between the whole and the part , But the whole cannot be separated from the part
  • Such as : In the program we define the entity :Person And IDCard、Head( head ), that Head and Person It's a combination ,IDCard and Person It's polymerization ( It can also be defined as a combination )
  • Symbol ——————————◆(composition, Solid diamond )
  • Head——————————◆Person

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云