Design patterns and seven design principles are not difficult

InfoQ 2022-09-23 09:23:06 阅读数:893

designpatternsdesignprinciplesdifficult

Author: Knowledge Talk, CSDN Blog Expert, Alibaba Cloud Signed Blogger, InfoQ Signed Blogger, Huawei Cloud Cloud Sharing Expert
Areas of Expertise: Full Stack Engineer, Crawler, ACM Algorithm
Official Account: Knowledge Talk

Summary of Design Patterns and Other Principles
The following is my simple understanding of design patterns and design principles, not to copy and paste things on the Internet, my own brief understanding,
It is more vernacular
, hope that helps.

The main dish is here

Design mode

Design mode has a total of 23 design modes. In fact, I personally think that design mode is 23 kinds of programming ideas.Maybe you don't know during the coding process, but you are actually using a certain idea to code, just to name a few patterns and applications that you can still remember.

  • Factory mode: such as our custom object creation object, such as A object is specially used to create B object, that is, A is a factory class.
  • Abstract factory mode: Our commonly used ApplicationContext, BeanFactory, is the abstract factory mode
    Insert a little:The factory pattern specializes in producing the same kind of objects, and the abstract factory pattern produces multiple kinds of objects
    .
  • Singleton mode: The objects created by default in the commonly used SpringIOC containers are all singleton objects, that is, the application of the singleton mode.
  • Prototype mode: This mode is that the objects that we commonly use new to create are recorded as objects created in prototype mode.
  • Decorator mode: This is mainly to compete for the function of an object method.
  • Proxy mode: The proxy mode is also a proxy object created to enhance the function of a method.
    The difference between the decorator and the proxy mode is that the decorator does not change the original object, and a new target object will be created inside the proxy object created by the proxy mode
    .
  • Observer mode: the listener in spring, that is, it will notify when there is a problem.
  • Adapter mode: We commonly use FileInputstream, which can only read byte streams by default. In order to adapt to read character streams, use InputStreamReader to transfer theThe incoming FileInputstream creates a reader object.
  • Flyweight pattern: Flyweight pattern, also known as lightweight pattern, is a label for object pooling.Similar to the thread pool, the thread pool can avoid the constant creation and destruction of objects, which consumes performance.Flyweight pattern can reduce the number of objects. Its purpose is to share fine-grained objects and centralize multiple accesses to the same object.

Seven design principles

Warm reminder: This is a bit too much, please read it carefully

  • Open and closed principle: open for extension, closed for modification
  • Single responsibility principle: that is, a class should focus on a single responsibility.
  • The Law of Demeter: The less a class knows about other classes, the better.
  • Liskov replacement principle: that is, the subclass can replace the position of the parent class, which means the method of the parent class, which the subclass cannot changeThought.
  • Dependency inversion principle: try to rely on interfaces instead of objects to reduce coupling.
  • Combination principle: try to use synthesis/aggregation to achieve reuse, and try to use inheritance as little as possible.
  • The principle of interface separation: an interface should have a single function as much as possible to avoid inserting very different interfaces into the interface.

Summary

The above is my simple understanding of design patterns and design principles, not to copy and paste things on the Internet, my own short-answer understanding, I'm more inclined to the vernacular, I hope it helps.
copyright:author[InfoQ],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/266/202209230910157593.html