In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains the introduction of the seven structural patterns of Java. The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn the introduction of the seven structural patterns of Java.
There are seven structural patterns in Java design pattern: adapter pattern, decoration pattern, proxy pattern, appearance pattern, bridge pattern, combination pattern and sharing meta-pattern. The adapter pattern of the object is the origin of various patterns.
I. Adapter mode:
Adapter pattern for a class: when you want to convert a class to a class that satisfies another new interface, you can use the class's adapter pattern to create a new class, inherit the original class, and implement the new interface.
Adapter pattern for objects: when you want to convert an object to an object that satisfies another new interface, you can create a Wrapper class that holds an instance of the original class, and in the method of the Wrapper class, just call the method of the instance.
Interface adapter pattern: when you do not want to implement all the methods in an interface, you can create an abstract class Wrapper that implements all the methods. When we write other classes, we can inherit the abstract class.
II. Decoration mode (Decorator)
1. You need to extend the functionality of a class.
2. Add function to an object dynamically, and it can also be undone dynamically. Inheritance cannot do this. The inherited function is static and cannot be dynamically added or deleted. Disadvantages: produce too many similar objects, not easy to make mistakes!
Third, agency mode
If an existing method needs to be improved when it is used, there are two ways:
1. Modify the original method to adapt. This violates the principle of "open to extension and closed to modification".
2. It uses a proxy class to call the original method and controls the result. This method is the proxy mode.
Using the agent mode, the function can be divided more clearly, which is helpful for later maintenance!
4. Appearance mode (Facade)
If we do not have the Computer class, then CPU, Memory, and Disk will hold instances and have a relationship with each other, which will cause serious dependencies. Modifying one class may lead to modifications of other classes. This is not what we want to see. With the Computer class, the relationship between them is placed in the Computer class, which plays the role of decoupling. This is the appearance pattern!
5. Bridging mode (Bridge)
Just like jdbc implements multiple databases
VI. Combination mode (Composite)
Use scene: combine multiple objects to operate, often used to represent a tree structure, such as binary tree, number, etc.
7. Enjoy the meta-model (Flyweight)
Through the management of connection pool, the sharing of database connection is realized, and the connection does not need to be recreated every time, which saves the overhead of database re-creation and improves the performance of the system.
Thank you for your reading, the above is the content of "introduction of the seven structural patterns of Java". After the study of this article, I believe you have a deeper understanding of the introduction of the seven structural patterns of Java, and the specific use still needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.