Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the design principles of Design Pattern

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is about what Design Pattern design principles are all about. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Design Pattern notes

Too stiff: can't easily add new features

Too fragile: one modification may spread to many other places

Low reuse rate: existing code depends on other things, so it is difficult to reuse this code

High viscosity: changes are always made in a way that destroys the original intent and the original design framework

Design principle

1. OCP principle (opening and closing principle)

Software entities should be opon for extension but closed for modification.

Encapsulation of variability: when a component is considered to be mutable, the mutable component should be encapsulated so that its internal changes only act within the internal scope and do not extend to other places.

A different representation of variability is a specific subclass in the same inheritance structure. Different variability should not be mixed together in design.

2. Richter scale substitution principle:

If every object O1 of type C1 has an object of type O2 of type C2, so that O1 in a program defined by C1 can be replaced with O2, then C2 is a subclass of C1.

The Richter substitution principle requires that its subclasses also apply where the base class is used, which is a verification method in the process of abstracting objects during OOD. If we have designed a superclass An and we have designed a subclass of A, then to check whether our design is reasonable, we should look at the place where the object of An is used in the application code according to the principle of Richter substitution. It is also true that the subclass object can be replaced with its subclass object, if it is not true, then it is proved that the subclass is not really a superclass child.

3. Dependency reversal relationship:

Usually when designing a hierarchical structure, we always design that the high level depends on the low level, and the requirement of relying on inversion is that the low level depends on the high level. The simple example here is the interface technology in Java. According to the principle of dependency inversion, there is a concept of interface programming: a concrete Java class should only implement the methods in its abstract class and interface, rather than giving redundant methods.

4. Abstract class is like interface:

a. Abstract classes can implement some functions, but interfaces cannot, so subclasses of abstract classes have that part of the functionality implemented by abstract classes.

b. The implementation of an abstract class exists in its hierarchical structure, and the implementation of the interface can implement not only this interface, but also other interfaces. In this way, this subclass can provide different services.

c. It is difficult to define an abstract parent class for an existing subclass because it changes its hierarchical structure, while it is easy to define an interface for it.

d. Interfaces are an ideal tool for defining hybrid types.

5. ISP (principle of interface separation):

This principle emphasizes how to design the interface, it emphasizes that the interface should be divided by role, and the functions of all roles should not be attributed to one interface. It emphasizes that many interfaces should not be used as optimization objects.

For example, there is a website with a full-text search function, and the system has an Interface to provide all the operational functions, such as managing indexes, search operations, and so on. According to the ISP principle, an interface should be designed for each role, such as an index management interface and a search operation interface.

6. The principle of CARP synthesis / polymerization reuse:

7. LoD Dimitt principle: also becomes the principle of least knowledge

If a class does not have to communicate directly with other classes, then the two classes should not interact with each other, that is, neither of them is aware of the other's existence. If a class needs to call a method of another class, it can be called through a third party.

This principle is to close the variability of components, so that their changes do not spread to other places.

Thank you for reading! This is the end of this article on "what are the design principles of Design Pattern?". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report