In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
In this issue, the editor will bring you about how to understand the common design patterns in Java knowledge points. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.
one。 Observer mode
The observer pattern is described in one sentence, that is, when the state of the object of a class (the observed) changes, the state of other dependent objects (the observer) changes accordingly.
Specific implementation process:
Defines the observer abstract class, which has a member variable of the observer, and a update method to notify the observer class when the observer changes, and the observer instance will be passed in when the observer class is instantiated, so that when the observer calls the observer's update method, the observer can access the state changed by the observer through its own observer member variables.
Defines the observed, which contains a list that stores instances of several observers, exposes ways to add and delete editors, and calls the update method of the observer object in the list to notify the observers when their state changes.
Java provides an Observable class and an Observer interface in the java.util library, and only a update method is provided in the Observer interface. By calling this method, the observer notifies the observer that his state has changed. Observable class we provide methods for adding, deleting, notifying observers of changes, and so on. When we need to notify the observer and need to call the observer update method, we need to call the setChanged method.
There are many scenarios that are used for observer mode in Android. For example, BroadcastReceiver,Eventbus,RxJava and so on all adopt the observer mode.
two。 Adapter mode
As a bridge between two incompatible interfaces, it combines the functions of two independent interfaces. This pattern involves a single class that is responsible for adding independent or incompatible interface functions. Note that the point is to make incompatible functions compatible based on the original class. The Adapter class is generally used to implement functions that are incompatible with the original class. For example, MediaAdapter in demo implements special functions that MediaPlayer does not have. As long as the user calls the play method in AudioPlayer, AudioPlayer will automatically choose different play methods according to the audio type. When the audio type does not meet the capabilities of traditional player, AudioPlayer will use adapter to call previously incompatible methods (functions), thus achieving the so-called adaptation.
three。 Agent mode
In fact, the implementation is very simple, that is, a proxy class wraps the other proxy class and only exposes to the outside world the method of calling the method of the proxied class, so as to realize the proxy pattern. special attention should be paid to the difference between the proxy pattern and the adapter pattern: the adapter pattern mainly changes the interface of the object being considered, while the proxy pattern cannot change the interface of the proxy class. The difference between the decorator mode and the decorator mode: the decorator mode is to enhance functionality, while the proxy mode is to control it.
four。 Factory method model
The factory method pattern actually needs to be used when the instantiation of a class depends on different scenarios, such as the above demo. The implementation logic inside the instantiated Shape object varies according to different shapes, so you can use the factory method pattern to hide the implementation details inside the class. Users only need to tell the factory class what kind of product they need. The factory can automatically call its own internal code for the corresponding scenario to return a "product" that the user needs.
five。 Abstract factory pattern
Compared with the factory method model, because a factory can only produce one product, for example, a ShapeFactory can only instantiate different Shape according to different situations, so it is obviously impossible to use the factory method when we need a whole set of products (such as shapes and colors to form a set of products), so we need abstract factory pattern, which is actually the factory of the factory. That is, the purpose of its function is to instantiate different factories, and then users instantiate complete sets of products under different scenarios through different factories.
six。 Singleton mode
The singleton pattern generally means that a class has only one object in the whole program.
seven。 Command mode
Command mode essentially abstracts commands into a concrete class, that is, this class is dedicated to executing a command. For example, in demo, SellStock is dedicated to executing the command sell. When users need sell, just instantiate SellStock and then excute can complete sell. Another common example is the role of buttons (button) in GUI development. Each button is an object. When the user clicks a button, it triggers a corresponding command, and what the user sees is the effect of clicking the button, while at the code level, the instantiated button object executes its own "command" similar to the excute method in demo.
The above is the editor for you to share how to understand Java knowledge points in the common design patterns, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
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.