设计模式

This commit is contained in:
2025-09-20 22:08:45 +08:00
parent cbf2c61d8e
commit 2c8d065d2a
11 changed files with 45 additions and 0 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 206 KiB

0
public/api/i/2025/09/18/8d4y9-1.webp Normal file → Executable file
View File

Before

Width:  |  Height:  |  Size: 23 KiB

After

Width:  |  Height:  |  Size: 23 KiB

0
public/api/i/2025/09/18/8or7m-1.webp Normal file → Executable file
View File

Before

Width:  |  Height:  |  Size: 75 KiB

After

Width:  |  Height:  |  Size: 75 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 34 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 34 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 38 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 47 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 46 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 58 KiB

View File

@@ -0,0 +1,45 @@
---
title: 常见设计模式
published: 2025-09-18
description: ''
image: ''
tags: []
category: '设计模式'
draft: false
lang: ''
---
![](https://blog.meowrain.cn/api/i/2025/09/18/119exsd-1.webp)
# 策略模式
现实生活中我们到商场买东西的时候卖场往往根据不同的客户制定不同的报价策略比如针对新客户不打折扣针对老客户打9折针对VIP客户打8折...
现在我们要做一个报价管理的模块,简要点就是要针对不同的客户,提供不同的折扣报价。
```java
package org.strategy;
import java.math.BigDecimal;
public class QuoteManager {
public BigDecimal quote(BigDecimal originalPrice, String customType){
if ("新客户".equals(customType)) {
System.out.println("抱歉!新客户没有折扣!");
return originalPrice;
}else if ("老客户".equals(customType)) {
System.out.println("恭喜你老客户打9折");
originalPrice = originalPrice.multiply(new BigDecimal(0.9)).setScale(2,BigDecimal.ROUND_HALF_UP);
return originalPrice;
}else if("VIP客户".equals(customType)){
System.out.println("恭喜你VIP客户打8折");
originalPrice = originalPrice.multiply(new BigDecimal(0.8)).setScale(2,BigDecimal.ROUND_HALF_UP);
return originalPrice;
}
//其他人员都是原价
return originalPrice;
}
}
```