In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "what are the design patterns often used in development". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Factory model
This is the most basic design pattern, but also the most commonly used design pattern. This is a design pattern that we usually use all the time, but we don't know what we are using.
The factory pattern is called Siyi, which is a processing plant. Unlike reality, what we produce here is not clothes, leather shoes, trousers, etc., but the most important object in our object-oriented programming.
In reality, we all know the benefits of factories, not only mass production, but also mass customization, because there are different moulds, it can produce various types of products that people need.
In software development, we are more concerned with the methods of using objects, but we are not very concerned about how objects are created, so abstract factories solve our problems. We just need to customize the functional interface of the product we need, and then let the factory produce objects according to our interface, that is, the interface.
Through this call, if there is a problem with our product, it can be recalled at any time and can be patched by the factory at any time, thus realizing the flexible expansion of the product.
Adapter mode
In reality, we often charge our mobile phones, and what we use is a power adapter. Why is it called an adapter? Because it can not only charge our own mobile phones, but also charge other people's mobile phones, and even charge other electrical devices, because it can adapt to a variety of charging devices, it is called an adapter.
In software development, it is almost or impossible for us to develop an application without the database, so what database we need to care about most, because if we choose the wrong database, there may be a performance bottleneck in the later stage. Is there a way for us to switch databases seamlessly without modifying the code or with little code modification? The answer is adapter mode.
We first define the adapter interface, and then let various databases implement the interface we defined, and we use the methods defined in the interface in the code, so that when we want to switch the database, as long as the database implements the method of the corresponding interface, we can complete the database adaptation and seamlessly connect.
Observer mode
Observer mode, also known as notification mode, is an one-to-many model. In reality, when the headmaster or teacher issues an order on the stage, the loudspeaker will convey the order to every student, and the students will have their own reaction when they hear the order. The headmaster or teacher does not have to convey the order to the students one by one. Because the loudspeaker will transmit the command to the students, here the loudspeaker is an observer, and the teacher and principal are the observers, and the students are the objects to be informed. Therefore, the observer pattern is also called the publish-subscribe model.
In software development, for example, our product has such a function that after a user has successfully placed an order, a text message will be sent to notify the user. If you want to send not only SMS, but also email, but also voice notification, in this case, we can adopt the observer mode, and we will put the payment success information into the message queue, as for texting or email. Each business module subscribes to the message queue to handle it. In this way, in the order module, there is no need for a notification SMS module, mail module.
Decorator mode
In real life, under normal circumstances, after a person has changed a piece of clothes, we can still recognize him, which is his decoration, which shows that decoration does not affect a person's appearance, that is, his function, but by wearing a high-end coat, it can improve a person's temperament, which is the function of decoration. The decorator pattern is to make an object more powerful by decorating it without changing it.
To take an example in software development, for example, we have designed a short message sending function, and there is no problem with this function after testing and online testing, but now there is a new requirement that when sending text messages, we can also make voice reminders. How can we achieve this function without affecting the original function? at this time, we can use the decorator. That is to decorate a voice function for the SMS class, so that it can not only send text messages, but also achieve the function of voice transmission.
Strategy mode
The strategy name thought means a collection of solutions to achieve a goal, simply a collection of methods, all of which are used to achieve one thing.
In software development, an object uses different strategies for different scenarios to achieve the same function, that is, the strategy method. If you have heard of polymorphism, you will find that it is very similar to the definition of polymorphism. The difference is that polymorphism is different methods for different objects to deal with the same operation, while the policy pattern is that the same object uses different strategies for the same operation.
For example, we have a seven-day National Day tour, the starting point and destination are from Beijing to Moscow, but we can use different strategies, you can fly, you can train, you can drive yourself, the same dream, different ways to achieve.
The thousands of people on the home page of Taobao is also a strategy mode, all show goods, in the face of different people, show different goods, this is determined by the strategy.
This is the end of the content of "what design patterns are often used in development". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.