In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Reading this article takes about 1.7 minutes.
This article is the third in a series of design patterns. Although I wrote the corresponding article before, it was broken for various reasons, and I found that the content written before was also very bad and not systematic enough. So now I plan to rewrite it, plus it's been a while since I started, so it's my own review!
Learning and learning from time to time, not also said.
Recommended reading:
Single Row Design Pattern "UML Class Diagram" Grading Competition
Single Row Design Mode "Simple Factory Design Mode" Black Iron III
Current Rank: Black Iron II
cdn.xitu.io/2019/2/14/168eb35943726dd3? w=568&h=291&f=jpeg&s=7582">
Let's Go!
preface
Design patterns are not syntax, they are clever ways to make programs more flexible. Architecture patterns are larger than design patterns, architecture patterns are strategic, and design patterns are tactical.
There are three types of design patterns: creative, behavioral, and structural, totaling 23.
strategy pattern
Policy patterns wrap algorithms, separating responsibility for using algorithms from the algorithms themselves and delegating management to different objects. Policy patterns typically wrap a set of algorithms into a set of policy classes, as subclasses of an abstract policy class.
In a nutshell: "Prepare a set of algorithms and package each one so that they are interchangeable."
In Strategy Pattern, the behavior of a class or its algorithm can be changed at runtime. This type of design pattern is a behavioral pattern.
There are many design principles, here is the use of strategy patterns, see examples to think about where the following design patterns are useful:
1. Encapsulate changes (identify possible changes in your application, isolate them, and don't mix them with code that doesn't need to change).
2. Programming for interfaces, superclasses, not implementations.
3. Use combination more, inherit less.
business needs
If you design a store checkout software, salespeople charge customers according to the unit price and quantity of goods they buy.
code implementation
You see the demand, you analyze it, you multiply the quantity by the unit price to get the total cost, which doesn't bother me, and you write the original version.
It's simple, you just declare the method and you calculate the total settlement price.
When you are ready to submit code, the evil product comes to you and gives you a new version of the requirements. (Draw a circle secretly to curse the product...)
new business requirements
If you design a store checkout software, salespeople charge customers according to the unit price and quantity of goods they buy. That is, shopping malls now need to hold activities, and they will give discounts to the goods purchased by customers, such as 25% off, 5% off, and so on.
code implementation
This is not after calculating the total price and multiplying it by a discount.
The new code is as follows:
Before you test, the product will come to you again. We may realize 100 for 300 and 50 for 200... And so on a lot of activities, so your program needs to be designed a little bit more flexible ha, otherwise my needs may change at that time.
See here, there is no way, or have to continue to improve the program, want to steal a lazy have no way, think about all this is possible, you find this can be perfect appropriate to this need to deal with the method.
10% discount and 10% discount is just a different form, abstract analysis, all discount algorithms are the same, so discount algorithm should be a class. After much thought, you wrote a third version of the program:
Here extract out a cash charge class, cash collection class abstract method, collect cash, parameters for the original price, return the current price.
Normal charge subcategory, normal charge, original price return.
Discount Fee Subcategory
Rebate Fee Subcategory
CashContext class, through the construction method, into the specific charging policy.
Client Main Code
operation results
Now you are not afraid, if you add new features, you only need to create new subclasses.
UML Class Diagram for Policy Patterns
summary
Intent: Define a series of algorithms, package them one by one, and make them interchangeable.
The main solution: in the case of multiple algorithms similar, use if... Else brings complexity and is difficult to maintain.
When to use: A system has many classes, and what distinguishes them is their direct behavior.
How to solve it: encapsulate these algorithms into classes and replace them arbitrarily.
Key code: Implement the same interface.
Application examples:
1, Zhuge Liang's tricks, each trick is a strategy.
2, travel mode, choose to ride a bicycle, take a car, each way of travel is a strategy.
LayoutManager in Java AWT.
Advantages:
1. The algorithm can be switched freely.
Avoid using multiple conditional judgments.
3. Good expandability.
Disadvantages:
1. The strategy class will increase.
All policy classes need to be exposed.
Usage scenario:
If there are many classes in a system that differ only in their behavior, then using policy patterns allows an object to dynamically choose one behavior among many.
A system needs to dynamically select one of several algorithms.
If an object has many behaviors, these behaviors will have to be implemented using multiple conditional choice statements if the appropriate pattern is not used.
Note: If there are more than four policies in a system, consider using hybrid patterns to address policy class inflation.
a wonderful review in the past
Landlord: If you dare to declare, I will raise the rent! Jingdong cancels the year-end award!
A thousand lines of MySQL detailed study notes (worth learning and collecting)
Is your girlfriend highly available?
How to gracefully null in Java
Recently, the whole industry is not in a good state. Pay attention to the small nine of some companies!
Java crawl and download cool dog TOP500 songs
How to calculate how much pv the server can withstand?
Programmers, stop learning frameworks!
What is the difference between professional programmers and undergraduate programmers?
How does a programmer's day go?
Start writing Tomcat from 0 and do it in 7 steps!
Welcome to pay attention to my public number "Programmer's growth path", read more wonderful!
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.