diff --git a/.idea/compiler.xml b/.idea/compiler.xml index ffa6d6bba80e4b56e37aae128c27f648bb839a20..1b749e01df206e6ef0171830aab2c0f47eff4356 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -9,9 +9,10 @@ - + + @@ -21,6 +22,7 @@ + diff --git a/.idea/encodings.xml b/.idea/encodings.xml index 30c15027f52341b84aaf549533c90b6e6967f60a..a55bdf49ab9345a634e1a117abe70771ddd5e849 100644 --- a/.idea/encodings.xml +++ b/.idea/encodings.xml @@ -15,6 +15,8 @@ + + diff --git a/.idea/misc.xml b/.idea/misc.xml index 74dc93664b8894d19b38d53bef85bd15927bd6cd..965b7c90c81e99d62b204aeb2d281a3becf70368 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,3 +1,4 @@ + @@ -15,6 +16,7 @@ diff --git a/README.md b/README.md index 2e197d0e2ea2e252cd7e3dd02bdd248689bf06e6..85458e7cabf618f400e4d5f0562e96bd4a67d75b 100644 --- a/README.md +++ b/README.md @@ -194,9 +194,11 @@ ![img.png](img/img_chainduty_pattern_application_example.png) ## 职责链模式总结 ![img.png](img/img_chainDuty_summary.png) - - -# 特技 +# 6.5 状态模式 +### 状态模式的介绍 +![img.png](img/img_state_pattern_concept.png) +# 状态模式的结构图 +![img.png](img/img_state_pattern_struct.png) 1. 使用 Readme.md 来支持不同的语言, Readme\_en.md: 支持英文, Readme\_zh.md: 支持中文 2. Gitee 官方博客 [blog.gitee.com](https://blog.gitee.com) diff --git a/byv-state-patttern-16/.gitignore b/byv-state-patttern-16/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..5ff6309b7199129c1afe4f4ec1906e640bec48c6 --- /dev/null +++ b/byv-state-patttern-16/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/byv-state-patttern-16/pom.xml b/byv-state-patttern-16/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..00064d5c35f0a7e672dbe5e594e59156c04c271f --- /dev/null +++ b/byv-state-patttern-16/pom.xml @@ -0,0 +1,58 @@ + + + 4.0.0 + + org.example + byv-state-patttern-16 + 1.0-SNAPSHOT + + + 8 + 8 + UTF-8 + + + + + org.projectlombok + lombok + 1.18.28 + compile + + + org.dom4j + dom4j + 2.1.3 + + + jaxen + jaxen + 1.1.6 + + + log4j + log4j + 1.2.12 + + + org.slf4j + slf4j-api + 1.7.36 + + + com.alibaba + fastjson + 1.2.62 + + + junit + junit + 4.12 + compile + + + + + \ No newline at end of file diff --git a/byv-state-patttern-16/src/main/java/com/boyunv/state/example01/Client.java b/byv-state-patttern-16/src/main/java/com/boyunv/state/example01/Client.java new file mode 100644 index 0000000000000000000000000000000000000000..969b4de70ce14c8243900a9032fcb8c6591998c2 --- /dev/null +++ b/byv-state-patttern-16/src/main/java/com/boyunv/state/example01/Client.java @@ -0,0 +1,22 @@ +package com.boyunv.state.example01; +/* + *@description + * + *@author boyunv + *@create 2023/8/27 21:16 + *@version 1.0 + */ + +public class Client { + public static void main(String[] args) { + Context context = new Context(); + State concreteStateA = new ConcreteStateA(); + concreteStateA.handle(context); + System.out.println(context.getCurrentState().toString()); + + System.out.println("=============================="); + State concreteStateB = new ConcreteStateB(); + concreteStateB.handle(context); + System.out.println(context.getCurrentState().toString()); + } +} diff --git a/byv-state-patttern-16/src/main/java/com/boyunv/state/example01/ConcreteStateA.java b/byv-state-patttern-16/src/main/java/com/boyunv/state/example01/ConcreteStateA.java new file mode 100644 index 0000000000000000000000000000000000000000..d199961309121fdde4be7a71be5cf93c10e52700 --- /dev/null +++ b/byv-state-patttern-16/src/main/java/com/boyunv/state/example01/ConcreteStateA.java @@ -0,0 +1,21 @@ +package com.boyunv.state.example01; +/* + *@description + * + *@author boyunv + *@create 2023/8/27 21:13 + *@version 1.0 + */ + +public class ConcreteStateA implements State{ + @Override + public void handle(Context context) { + System.out.println("进入到状态模式A'''''''''''''''''"); + context.setCurrentState(this); + } + + @Override + public String toString() { + return "当前年状态: concreteStateA"; + } +} diff --git a/byv-state-patttern-16/src/main/java/com/boyunv/state/example01/ConcreteStateB.java b/byv-state-patttern-16/src/main/java/com/boyunv/state/example01/ConcreteStateB.java new file mode 100644 index 0000000000000000000000000000000000000000..8375b201e64e817000117b8045c3fa2d1a94ca39 --- /dev/null +++ b/byv-state-patttern-16/src/main/java/com/boyunv/state/example01/ConcreteStateB.java @@ -0,0 +1,21 @@ +package com.boyunv.state.example01; +/* + *@description + * + *@author boyunv + *@create 2023/8/27 21:13 + *@version 1.0 + */ + +public class ConcreteStateB implements State{ + @Override + public void handle(Context context) { + System.out.println("进入到状态模式B'''''''''''''''''"); + context.setCurrentState(this); + } + + @Override + public String toString() { + return "当前年状态: concreteStateB"; + } +} diff --git a/byv-state-patttern-16/src/main/java/com/boyunv/state/example01/Context.java b/byv-state-patttern-16/src/main/java/com/boyunv/state/example01/Context.java new file mode 100644 index 0000000000000000000000000000000000000000..9368049237263b08f0e86bbda1c3ea73bc0c3295 --- /dev/null +++ b/byv-state-patttern-16/src/main/java/com/boyunv/state/example01/Context.java @@ -0,0 +1,28 @@ +package com.boyunv.state.example01; + +import lombok.Data; +import lombok.ToString; + +/* + *@description + * 上下文类 + *@author boyunv + *@create 2023/8/27 21:09 + *@version 1.0 + */ +@Data +@ToString +public class Context { + //维持一个对状态对象的引用 + private State currentState; + + public Context(){ + this.currentState=null; + } + + public Context(State currentState) { + this.currentState = currentState; + } + + +} diff --git a/byv-state-patttern-16/src/main/java/com/boyunv/state/example01/State.java b/byv-state-patttern-16/src/main/java/com/boyunv/state/example01/State.java new file mode 100644 index 0000000000000000000000000000000000000000..8f0a2a06b2c93a1d02981a9dea5970d57bb5b505 --- /dev/null +++ b/byv-state-patttern-16/src/main/java/com/boyunv/state/example01/State.java @@ -0,0 +1,14 @@ +package com.boyunv.state.example01; +/* + *@description + * 抽象状态接口 + *@author boyunv + *@create 2023/8/27 21:06 + *@version 1.0 + */ + +public interface State { + + //声明抽象方法,不同具体状态类可以有不同的实现 + void handle(Context context); +} diff --git a/byv-state-patttern-16/src/main/resources/config.xml b/byv-state-patttern-16/src/main/resources/config.xml new file mode 100644 index 0000000000000000000000000000000000000000..7cbe016568f295adf6621d9d76f83604173b20e5 --- /dev/null +++ b/byv-state-patttern-16/src/main/resources/config.xml @@ -0,0 +1,4 @@ + + + com.boyunv.strategy.example03.MT1101HandlerStrategy + \ No newline at end of file diff --git a/img/img_state_pattern_concept.png b/img/img_state_pattern_concept.png new file mode 100644 index 0000000000000000000000000000000000000000..1965c41a24a6f694408b9f6671792e690adeb2dc Binary files /dev/null and b/img/img_state_pattern_concept.png differ diff --git a/img/img_state_pattern_struct.png b/img/img_state_pattern_struct.png new file mode 100644 index 0000000000000000000000000000000000000000..6c94aa77a2aa1a736f2a1813f83ecfc7564c9160 Binary files /dev/null and b/img/img_state_pattern_struct.png differ