Skip to content

Commit

Permalink
添加设计模式之策略模式;代码配色修改
Browse files Browse the repository at this point in the history
  • Loading branch information
971230 committed Aug 21, 2024
1 parent 9dab75f commit 5c57e1d
Show file tree
Hide file tree
Showing 9 changed files with 323 additions and 73 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
# 971230.github.io
# 971230.github.io

1.要有Python环境</br>
2.cd venv/Script 之后 运行activate</br>
3.退出到根目录,执行 mkdocs serve</br>

图标搜索: https://squidfunk.github.io/mkdocs-material/reference/icons-emojis/#using-icons
符号大全: https://shijianchuo.net/tesufuhao/
1 change: 0 additions & 1 deletion docs/ArchLinux/Arch Linux安装.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
title: Arch Linux安装
description: 介绍一些Arch Linux的安装
status: new
---

> # Arch Linux使用archinstall安装
Expand Down
14 changes: 13 additions & 1 deletion docs/css/extra.css
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ h2 {
}

h3 {
color: rgb(48, 151, 48);
color: rgb(74, 174, 68);

}

Expand All @@ -166,3 +166,15 @@ md-header__button md-logo {
md-footer__inner md-grid {
height: 400px;
}

/* 代码的配色 */
:root>* {
--md-code-hl-string-color: #0eb49b;
--md-code-hl-comment-color: #0e8941;
--md-code-hl-number-color: #e6695b;
--md-code-hl-special-color: #f06090;
--md-code-hl-constant-color: #9383e2;
--md-code-hl-variable-color: #92915b;
--md-code-hl-name-color: #c48a3a;
--md-code-hl-function-color: #c973d9;
}
2 changes: 2 additions & 0 deletions docs/网站所有样式/demo.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ status: new

[这是本站的超链接](https://971230.github.io/)

[Hover me](https://example.com "I'm a tooltip!")

------

> 这是一段引用
Expand Down
Binary file added docs/设计模式/img/strategy-2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions docs/设计模式/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
title: 设计模式
description: 设计模式
icon: simple/libreofficeimpress
---

# 设计模式
220 changes: 220 additions & 0 deletions docs/设计模式/策略模式.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
---
title: 策略模式
description: 介绍策略模式的三种使用方式
status: new
---

# 策略模式

![图片的样式](..\img\strategy-2x.png)

## ❶单纯策略模式,没有结合其他设计模式
<P style="text-indent:2em;">
感觉策略模式更适合实际开发中有类似的业务,但是有不同的实际情况,可能是业务需要也可以是逻辑需要,
对于代码可以简化业务逻辑,不至于将代码写成一坨,长长的几十上百行,最典型的就是if的逻辑判断简化。
</p>

!!!但是它也有缺点
1️⃣.如果 if-else的判断情况很多,那么对应的具体策略实现类也会很多</br>
2️⃣.没法俯视整个分派的业务逻辑,因为具体的逻辑被分拆</br>

<P style="text-indent:2em;">
一次面试中,阿里面试官提到策略模式,他不这样认为,觉得都是一样需要看很多分支逻辑的,无论怎么改代码,
基础的业务逻辑就是无可更改的,天王老子来了该写的逻辑就是要写,该看的逻辑还是要看,策略模式更像是整理代码的一种方法,
就是不用也没法“俯视”所有逻辑。这也是一种全新的理解。</br>
</p>

<P style="text-indent:2em;">
不大喜欢一些理论性质的解释,直接样例理解,自己更能懂</br>
</p>

### 代码样例
<P style="text-indent:2em;">
首先要有一个接口,里面要包含用于判断每个业务的不同,每个业务具体的实现的方法,比如要计算快递的收费,
就要有一个判断每个快递的类型的方法,每一种快递到底是如何计费的。</br>
</p>

```java
/**
* 策略模式接口设计
*/
public interface LogisticsService {

/**
* ①需要用来判断具体走那个逻辑
*/
boolean isCurrentLogistics(Integer type);

/**
* ②计算逻辑
*/
BigDecimal calculateFee(TransferFeeRequest transferFeeRequest);
}
```

<P style="text-indent:2em;">
同时具体如何计费,就要有快递的运输距离,单价,最重要的快递类型,这些都是具体的业务,参数到底如何写是业务决定的,
也可以有快递的重量,体积,形状等,每家快递公司的收费标准不一样,还有一些政策更改,体量优惠减免,是否有保价,
都会决定具体的逻辑。
</p>

```java
@Data
public class TransferFeeRequest {

/**
* 距离
*/
private BigDecimal distance;

/**
* 单价
*/
private BigDecimal unitPrice;

/**
* 快递类型
*/
private Integer type;
}
```

一般这种业务都是写成具体的服务,比如合作的快递公司有京东、顺丰、圆通、中通,那就是四个服务

=== "京东快递服务"

``` java
/**
* JD快递服务
*/
@Service
public class JDTransfercompany implements LogisticsService {
private final BigDecimal pickFee = BigDecimal.TEN;

private final BigDecimal minDistance = BigDecimal.valueOf(80);

@Override
public BigDecimal calculateFee(TransferFeeRequest transferFeeRequest) {
BigDecimal distance = minDistance.compareTo(transferFeeRequest.getDistance()) > 0 ?
minDistance : transferFeeRequest.getDistance();
// do business
return distance.multiply(transferFeeRequest.getUnitPrice()).add(pickFee);
}

@Override
public boolean isCurrentLogistics(Integer type) {
return Objects.equals(type, 1);
}
}
```

=== "顺丰快递服务"

``` java
/**
* 顺丰快递服务
*/
@Service
public class SFTransfercompany implements LogisticsService {
private final BigDecimal pickFee = BigDecimal.TEN;

private final BigDecimal minDistance = BigDecimal.valueOf(60);

@Override
public BigDecimal calculateFee(TransferFeeRequest transferFeeRequest) {
BigDecimal distance = minDistance.compareTo(transferFeeRequest.getDistance()) > 0 ?
minDistance : transferFeeRequest.getDistance();
// do business
return distance.multiply(transferFeeRequest.getUnitPrice()).add(pickFee);
}

@Override
public boolean isCurrentLogistics(Integer type) {
return Objects.equals(type, 2);
}
}
```

=== "中通快递服务"

``` java
/**
* 中通快递服务
*/
@Service
public class ZTTransfercompany implements LogisticsService {
private final BigDecimal pickFee = BigDecimal.TEN;

private final BigDecimal minDistance = BigDecimal.valueOf(40);

@Override
public BigDecimal calculateFee(TransferFeeRequest transferFeeRequest) {
BigDecimal distance = minDistance.compareTo(transferFeeRequest.getDistance()) > 0 ?
minDistance : transferFeeRequest.getDistance();
// do business
return distance.multiply(transferFeeRequest.getUnitPrice()).add(pickFee);
}

@Override
public boolean isCurrentLogistics(Integer type) {
return Objects.equals(type, 3);
}
}
```

=== "圆通快递服务"

```java
/**
* 圆通快递服务
*/
@Service
public class YTTransfercompany implements LogisticsService {
private final BigDecimal pickFee = BigDecimal.TEN;

private final BigDecimal minDistance = BigDecimal.valueOf(20);

@Override
public BigDecimal calculateFee(TransferFeeRequest transferFeeRequest) {
BigDecimal distance = minDistance.compareTo(transferFeeRequest.getDistance()) > 0 ?
minDistance : transferFeeRequest.getDistance();
// do business
return distance.multiply(transferFeeRequest.getUnitPrice()).add(pickFee);
}

@Override
public boolean isCurrentLogistics(Integer type) {
return Objects.equals(type, 4);
}
}
```

都是类似的,本来可能是if-else一撸到底,现在变成分开实现(例子只是例子,比较简)</br>

那我们在使用的时候就简单一些了,用stream流就能调用</br>

### 使用方式

```java hl_lines="8 9 10 11"
/**
* 策略模式需要把所有的可能都注入进来
*/
private final List<LogisticsService> logisticsServices;

public BigDecimal calculateFee(@RequestBody TransferFeeRequest transferFeeRequest) {
// 遍历,根据每个类中的具体实现类来判断到底走那个逻辑
LogisticsService logisticsService = logisticsServices.stream()
.filter(logistics -> logistics.isCurrentLogistics(transferFeeRequest.getType()))
.findFirst()
.orElse(null);
// 一定要有对应匹配为空对不上的兜底逻辑,也可以单独列出一个实现类来写
if (logisticsService == null) {
throw new RuntimeException("没有对应的快递计算方式");
}
return logisticsService.calculateFee(transferFeeRequest);
}
```

我现在也只讲设计模式如何用,使用场景等,但是对它的理解实在是见仁见智了。其他理论化的介绍就不写了。

## ❷策略模式结合工厂方法模式
Loading

0 comments on commit 5c57e1d

Please sign in to comment.