当前位置:网站首页>Strategy mode_
Strategy mode_
2022-07-25 23:20:00 【wfsm】
quote :https://blog.csdn.net/qq_51165184/article/details/123997917
The strategy pattern Stragery : For a set of algorithms , Encapsulate each algorithm into a separate class with a common interface , Make them interchangeable
The essence : Separation algorithm , Selective implementation
Strategic patterns can separate behavior from the environment , The environment class Context Be responsible for inquiring what to do , Various algorithms are in the specific strategy class (ConcreteStrategy) Provided in the
Characteristics of the strategic model
- The specific algorithm is independent from the specific business
- Multiple
if-elseConsider using a policy pattern - Policy algorithms are different implementations of the same behavior ( polymorphic )
- Client selection . Context to implement the policy algorithm
Advantages and disadvantages
advantage :
- Avoid involving the client with important algorithms and data
- Avoid using multiple conditional selection statements that are difficult to maintain
- Easy to expand
shortcoming
- The judgment logic is on the client side , When the demand changes , To change the client program
- The client must know all policy classes , And decide which strategy class to use , This means that the client must understand the differences between these algorithms , In order to timely select the appropriate algorithm class
- Increased the number of objects
- Only suitable for flat algorithm structure
Code :
Discount policy interface :
public interface DiscountStrategy {
BigDecimal getDiscount(BigDecimal total);
}
Two discount strategies :
public class OverDiscountStrategy implements DiscountStrategy{
@Override
public BigDecimal getDiscount(BigDecimal total) {
// full 100 reduce 20
return total.compareTo(BigDecimal.valueOf(100)) >= 0?BigDecimal.valueOf(20): BigDecimal.ZERO;
}
}
public class UserDiscountStrategy implements DiscountStrategy{
@Override
public BigDecimal getDiscount(BigDecimal total) {
// 10% discount for members
return total.multiply(new BigDecimal("0.1")).setScale(2, RoundingMode.HALF_UP);
}
}
The default policy used by environment settings ,, And you can switch strategies :
public class DiscountContext {
// Hold a strategy
private DiscountStrategy strategy = new UserDiscountStrategy();
// Allow clients to set new policies
public void setStrategy(DiscountStrategy discountStrategy){
this.strategy = discountStrategy;
}
public BigDecimal calculatePrice(BigDecimal total){
return total.subtract(strategy.getDiscount(total));
}
}
test :
public static void main(String[] args) {
DiscountContext context = new DiscountContext();
context.setStrategy(new OverDiscountStrategy());
BigDecimal result = context.calculatePrice(BigDecimal.valueOf(200));
System.out.println("result = " + result);
}
边栏推荐
- TS function
- Recommended system - an embedded learning framework for numerical features in CTR prediction
- The fifth article in the series of radar Fundamentals: the function of radar modulation style
- The small icon of notification setting shows a small square
- Zero crossing position search of discrete data (array)
- How does Navicat modify the language (Chinese or English)?
- WebMvcConfigurationSupport
- @Autowired注解 required属性
- Analysis of the influence of ESM direction finding error on positioning error
- 类和对象(3)
猜你喜欢

The new UI people help task help PHP source code with a value of 1500 / reward task Tiktok Kwai headline like source code / with three-level distribution can be packaged applet

Unity uses macros

Network Security Learning notes-1 file upload

firewall 命令简单操作

serialization and deserialization

MathType安装和解决不能Crtl+V的问题

多模态——Deep Multi-Modal Sets

PHP JSON variable array problem

Source code of wechat applet for discerning flowers and plants / source code of wechat applet for discerning plants

idea设置get、set模板解决boolean类型字段的命名问题
随机推荐
Several commonly used traversal methods
[QNX Hypervisor 2.2用户手册]9.8 load
Source code of YY music wechat applet imitating Netease cloud music
[QNX Hypervisor 2.2用户手册]9.7 generate
Sichuan cuisine menu (I)
Redis expiration key deletion strategy [easy to understand]
File contains vulnerability
[QNX Hypervisor 2.2用户手册]9.6 gdb
网格参数化Least Squares Conformal Maps实现(3D网格映射到2D平面)
Zcmu--5015: complete the task
Mongodb update operator (modifier)
Servlet overview
R language drawing parameters (R language plot drawing)
The fifth article in the series of radar Fundamentals: the function of radar modulation style
Hj7 take approximate value
理解的英文(言语理解)
The small icon of notification setting shows a small square
firewall 命令简单操作
[QNX hypervisor 2.2 user manual]9.7 generate
多模态——Deep Multi-Modal Sets