-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Q10Viking
committed
Mar 28, 2024
1 parent
527a402
commit 4d49b50
Showing
2 changed files
with
338 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,338 @@ | ||
--- | ||
sidebarDepth: 3 | ||
sidebar: auto | ||
prev: | ||
text: Back To 目录 | ||
link: /java/ | ||
typora-root-url: ..\.vuepress\public | ||
--- | ||
|
||
|
||
|
||
## 处理if-else分支 | ||
|
||
1. 定义函数式接口 创建一个名为BranchHandle的函数式接口,接口的参数为两个Runnable接口。这两个两个Runnable接口分别代表了为true或false时要进行的操作 | ||
|
||
```java | ||
/** | ||
* 分支处理接口 | ||
**/ | ||
@FunctionalInterface | ||
public interface BranchHandle { | ||
|
||
/** | ||
* 分支操作 | ||
* | ||
* @param trueHandle 为true时要进行的操作 | ||
* @param falseHandle 为false时要进行的操作 | ||
* @return void | ||
**/ | ||
void trueOrFalseHandle(Runnable trueHandle, Runnable falseHandle); | ||
|
||
} | ||
``` | ||
|
||
2. 编写判断方法 创建一个工具类VUtils里面有一个名为isTureOrFalse的方法,方法的返回值为刚才定义的函数式接口-BranchHandle | ||
|
||
```java | ||
/** | ||
* 参数为true或false时,分别进行不同的操作 | ||
**/ | ||
public static BranchHandle isTureOrFalse(boolean b){ | ||
|
||
return (trueHandle, falseHandle) -> { | ||
if (b){ | ||
trueHandle.run(); | ||
} else { | ||
falseHandle.run(); | ||
} | ||
}; | ||
} | ||
``` | ||
|
||
3. 使用 | ||
|
||
```java | ||
// 参数为true时,执行trueHandle | ||
VUtil.isTureOrFalse(true) | ||
.trueorFalseHandle ( trueHandle: O -> { | ||
System.out.printin("true,没毛病”); | ||
},falseHandle: () -> { | ||
System.out.println("有毛病"); | ||
}) | ||
``` | ||
### 如果存在值执行消费操作,否则执行基于空的操作 | ||
1. 定义函数 创建一个名为PresentOrElseHandler的函数式接口,接口的参数一个为Consumer接口。一个为Runnable,分别代表值不为空时执行消费操作和值为空时执行的其他操作 | ||
```java | ||
/** | ||
* 空值与非空值分支处理 | ||
*/ | ||
@FunctionalInterface | ||
public interface PresentOrElseHandler<T extends Object> { | ||
/** | ||
* 值不为空时执行消费操作 | ||
* 值为空时执行其他的操作 | ||
* | ||
* @param action 值不为空时,执行的消费操作 | ||
* @param emptyAction 值为空时,执行的操作 | ||
* @return void | ||
**/ | ||
void presentOrElseHandle(Consumer<? super T> action, Runnable emptyAction); | ||
} | ||
``` | ||
2. 编写判断方法 创建一个名为isBlankOrNoBlank的方法,方法的返回值为刚才定义的函数式接口-PresentOrElseHandler。 | ||
```java | ||
/** | ||
* 参数为true或false时,分别进行不同的操作 | ||
* | ||
* @param b | ||
* @return com.example.demo.func.BranchHandle | ||
**/ | ||
public static PresentOrElseHandler<?> isBlankOrNoBlank(String str){ | ||
return (consumer, runnable) -> { | ||
if (str == null || str.length() == 0){ | ||
runnable.run(); | ||
} else { | ||
consumer.accept(str); | ||
} | ||
}; | ||
} | ||
``` | ||
3. 使用方式 调用工具类参数参数后,调用函数式接口的presentOrElseHandle方法传入一个Consumer和Runnable | ||
## 案例chart处理多分支 | ||
[Source Code](https://gitee.com/q10viking/design-patterns/tree/master/springboot-design-patterns/src/main/java/org/hzz/%E5%88%9B%E5%BB%BA%E8%80%85%E6%A8%A1%E5%BC%8F/CP2_%E7%AE%80%E5%8D%95%E5%B7%A5%E5%8E%82%E6%A8%A1%E5%BC%8F/%E6%A1%88%E4%BE%8B/%E5%9B%BE%E8%A1%A8%E8%AE%BE%E8%AE%A1) | ||
### 源文件 | ||
```java | ||
package org.hzz.创建者模式.CP2_简单工厂模式.案例.图表设计.chart; | ||
import org.hzz.创建者模式.CP2_简单工厂模式.案例.图表设计.chart.impl.HistogramChart; | ||
import org.hzz.创建者模式.CP2_简单工厂模式.案例.图表设计.chart.impl.LineChart; | ||
import org.hzz.创建者模式.CP2_简单工厂模式.案例.图表设计.chart.impl.PieChart; | ||
import java.util.function.Supplier; | ||
/** | ||
* @author hzz | ||
* @date 2024/03/28 | ||
* @desc 图表简单工厂模式,静态工厂方法 | ||
*/ | ||
public class ChartFactory { | ||
// 柱状图 | ||
public static final String H = "histogram"; | ||
// 饼状图 | ||
public static final String P = "pie"; | ||
// 折线图 | ||
public static final String L = "line"; | ||
// 静态工厂方法 | ||
public static Chart getChart(String type){ | ||
Chart chart = null; | ||
if(type.equalsIgnoreCase(H)){ | ||
chart = new HistogramChart(); | ||
System.out.println("初始化柱状图"); | ||
}else if(type.equalsIgnoreCase(P)){ | ||
chart = new PieChart(); | ||
System.out.println("初始化饼状图"); | ||
}else if(type.equalsIgnoreCase(L)){ | ||
chart = new LineChart(); | ||
System.out.println("初始化折线图"); | ||
} | ||
return chart; | ||
} | ||
} | ||
``` | ||
### supplier map结构优化 | ||
```java | ||
package org.hzz.创建者模式.CP2_简单工厂模式.案例.图表设计.chart.supplier; | ||
import org.hzz.创建者模式.CP2_简单工厂模式.案例.图表设计.chart.Chart; | ||
import java.util.function.Supplier; | ||
/** | ||
* 语义化supplier接口 | ||
*/ | ||
public interface ChartSupplier extends Supplier<Chart> { | ||
} | ||
``` | ||
```java | ||
package org.hzz.创建者模式.CP2_简单工厂模式.案例.图表设计.chart; | ||
import org.hzz.创建者模式.CP2_简单工厂模式.案例.图表设计.chart.impl.HistogramChart; | ||
import org.hzz.创建者模式.CP2_简单工厂模式.案例.图表设计.chart.impl.LineChart; | ||
import org.hzz.创建者模式.CP2_简单工厂模式.案例.图表设计.chart.impl.PieChart; | ||
import org.hzz.创建者模式.CP2_简单工厂模式.案例.图表设计.chart.supplier.ChartSupplier; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
/** | ||
* map结构优化if-else if多分支 | ||
*/ | ||
public class ChartFactory2 { | ||
// 柱状图 | ||
public static final String H = "histogram"; | ||
// 饼状图 | ||
public static final String P = "pie"; | ||
// 折线图 | ||
public static final String L = "line"; | ||
private static Map<String, ChartSupplier> chartSuppliers = new HashMap<>(); | ||
// 使用map进行优化 if结构 | ||
static { | ||
chartSuppliers.put(H,ChartFactory2.histogramChartSupplier()); | ||
chartSuppliers.put(P,ChartFactory2.pieChartSupplier()); | ||
chartSuppliers.put(L,ChartFactory2.lineChartSupplier()); | ||
} | ||
static ChartSupplier histogramChartSupplier(){ | ||
return ()->{ | ||
HistogramChart histogramChart = new HistogramChart(); | ||
System.out.println("柱状图初始化"); | ||
return histogramChart; | ||
}; | ||
} | ||
// 静态工厂方法 | ||
public static Chart getChart(String type){ | ||
Chart chart = null; | ||
ChartSupplier chartSupplier = chartSuppliers.get(type); | ||
if(chartSupplier != null){ | ||
chart = chartSupplier.get(); | ||
}else{ | ||
throw new UnsupportedOperationException("无法识别图表类型: " + type); | ||
} | ||
return chart; | ||
} | ||
static ChartSupplier pieChartSupplier(){ | ||
return ()->{ | ||
PieChart pieChart = new PieChart(); | ||
System.out.println("饼状图初始化"); | ||
return pieChart; | ||
}; | ||
} | ||
static ChartSupplier lineChartSupplier(){ | ||
return ()->{ | ||
LineChart lineChart = new LineChart(); | ||
System.out.println("折线图图初始化"); | ||
return lineChart; | ||
}; | ||
} | ||
} | ||
``` | ||
### Function 数组 | ||
```java | ||
/** | ||
* 语义化Function函数接口,更加清晰 | ||
*/ | ||
public interface ChartFunction extends Function<String, Chart> { | ||
} | ||
``` | ||
```java | ||
package org.hzz.创建者模式.CP2_简单工厂模式.案例.图表设计.chart; | ||
import org.hzz.创建者模式.CP2_简单工厂模式.案例.图表设计.chart.impl.HistogramChart; | ||
import org.hzz.创建者模式.CP2_简单工厂模式.案例.图表设计.chart.impl.LineChart; | ||
import org.hzz.创建者模式.CP2_简单工厂模式.案例.图表设计.chart.impl.PieChart; | ||
import org.hzz.创建者模式.CP2_简单工厂模式.案例.图表设计.chart.supplier.ChartFunction; | ||
/** | ||
* Function数组优化 | ||
*/ | ||
public class ChartFactory3 { | ||
// 柱状图 | ||
public static final String H = "histogram"; | ||
// 饼状图 | ||
public static final String P = "pie"; | ||
// 折线图 | ||
public static final String L = "line"; | ||
// Function 数组优化 | ||
private static ChartFunction[] handlers = { | ||
histogramChartFunction(), | ||
lineChartFunction(), | ||
pieChartFunction() | ||
}; | ||
// 静态工厂方法 | ||
public static Chart getChart(String type){ | ||
Chart chart = null; | ||
// 遍历 | ||
for(ChartFunction f: handlers){ | ||
chart = f.apply(type); | ||
if(chart != null) | ||
return chart; | ||
} | ||
return chart; | ||
} | ||
private static ChartFunction histogramChartFunction() { | ||
return input -> { | ||
if (input.equalsIgnoreCase(H)) { | ||
HistogramChart histogramChart = new HistogramChart(); | ||
System.out.println("柱状图初始化"); | ||
return histogramChart; | ||
} | ||
return null; | ||
}; | ||
} | ||
static ChartFunction pieChartFunction(){ | ||
return input -> { | ||
if (input.equalsIgnoreCase(P)) { | ||
PieChart pieChart = new PieChart(); | ||
System.out.println("饼状图初始化"); | ||
return pieChart; | ||
} | ||
return null; | ||
}; | ||
} | ||
static ChartFunction lineChartFunction(){ | ||
return input->{ | ||
if (input.equalsIgnoreCase(L)) { | ||
LineChart lineChart = new LineChart(); | ||
System.out.println("折线图图初始化"); | ||
return lineChart; | ||
} | ||
return null; | ||
}; | ||
} | ||
} | ||
``` | ||
### 策略模式 |