当前位置:网站首页>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);
}
边栏推荐
- Drive board network cable directly connected to computer shared network configuration
- AI chief architect 12 AICA industrial landing analysis under the industrial production process optimization scenario
- Solve the problem phpstudy failed to import the database
- Mongodb query and projection operators
- npm+模块加载机制
- Thinkphp6 temporarily close the layout
- Network Security Learning notes-1 file upload
- R language drawing parameters (R language plot drawing)
- Solution of phpstudy service environment 80 port occupied by process system under Windows
- Kotlin 常用知识点汇总
猜你喜欢

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

POI特效 市场调研

行云管家V6.5.1/2/3系列版本发布:数据库OpenAPI能力持续强化

About using NPM command under the terminal, the installation error problem is solved (my own experience)

多模态——Deep Multi-Modal Sets

POI special effects Market Research

Sichuan cuisine menu (I)

Zero crossing position search of discrete data (array)

AI chief architect 12 AICA industrial landing analysis under the industrial production process optimization scenario

MathType安装和解决不能Crtl+V的问题
随机推荐
Bind class style and bind style style
推荐系统——An Embedding Learning Framework for Numerical Features in CTR Prediction
Cuteone: a onedrive multi network disk mounting program / with member / synchronization and other functions
wordpress去掉网站发布时间
Longitude and latitude and its transformation with coordinate system
类和对象(3)
Week 2: convolutional neural network
行云管家V6.5.1/2/3系列版本发布:数据库OpenAPI能力持续强化
Wechat official account, wechat payment development
@Autowired annotation required attribute
[QNX Hypervisor 2.2用户手册]9.7 generate
【接口性能优化】索引失效的原因以及如何进行SQL优化
模拟实现string类常用接口
Source code of YY music wechat applet imitating Netease cloud music
Simulate and implement common interfaces of string class
Rental experience post
Hj7 take approximate value
Take away applet with main version of traffic / repair to add main access function of traffic
Serialize data type
连续三年成为云AI服务领导者,亚马逊云科技做对了什么?