feat: 状态模式
This commit is contained in:
parent
9d57b5b702
commit
1c8c2a958e
Binary file not shown.
After Width: | Height: | Size: 30 KiB |
|
@ -21,9 +21,9 @@
|
||||||
### 工厂模式的主要角色
|
### 工厂模式的主要角色
|
||||||
|
|
||||||
1. **产品接口(Product)**:通常是一个接口或者抽象类,定义了具体产品的公共方法。
|
1. **产品接口(Product)**:通常是一个接口或者抽象类,定义了具体产品的公共方法。
|
||||||
2. **具体产品(ConcreteProduct)**:实现了产品接口的具体类,代表不同的产品。
|
2. **具体产品(Concrete Product)**:实现了产品接口的具体类,代表不同的产品。
|
||||||
3. **工厂接口(Creator)**:通常是一个抽象类或接口,定义了一个工厂方法用于创建产品。
|
3. **工厂接口(Creator)**:通常是一个抽象类或接口,定义了一个工厂方法用于创建产品。
|
||||||
4. **具体工厂(ConcreteCreator)**:实现了工厂接口的具体类,负责创建具体的产品对象。
|
4. **具体工厂(Concrete Creator)**:实现了工厂接口的具体类,负责创建具体的产品对象。
|
||||||
|
|
||||||
### 工厂模式的类型
|
### 工厂模式的类型
|
||||||
|
|
||||||
|
@ -1858,7 +1858,268 @@ public class BakeDemo {
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 状态模式---行为型2
|
## 状态模式---行为型
|
||||||
|
|
||||||
|
![image-20250204181749629](./images/设计模式-v2/image-20250204181749629.png)
|
||||||
|
|
||||||
|
需要角色:状态对象(Context)、状态接口(State)、具体状态实现(Concrete State)
|
||||||
|
|
||||||
|
### 简单示例
|
||||||
|
|
||||||
|
#### 示例代码
|
||||||
|
|
||||||
|
**上下文对象**
|
||||||
|
|
||||||
|
```java
|
||||||
|
@Getter
|
||||||
|
public class Lift {
|
||||||
|
// 开门状态
|
||||||
|
public static LiftState OPEN_STATE = new OpeningLiftState();
|
||||||
|
// 关门状态
|
||||||
|
public static LiftState CLOSE_STATE = new ClosingLiftState();
|
||||||
|
// 运行状态
|
||||||
|
public static LiftState RUNNING_STATE = new RunningLiftState();
|
||||||
|
// 停止状态
|
||||||
|
public static LiftState STOPPING_STATE = new StoppingLiftState();
|
||||||
|
|
||||||
|
// 当前电梯状态
|
||||||
|
private LiftState currentLiftState;
|
||||||
|
|
||||||
|
public void setCurrentLiftState(LiftState currentState) {
|
||||||
|
this.currentLiftState = currentState;
|
||||||
|
// 执行开门动作
|
||||||
|
this.currentLiftState.lift = this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void open() {
|
||||||
|
currentLiftState.open();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void close() {
|
||||||
|
currentLiftState.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
currentLiftState.run();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void stop() {
|
||||||
|
currentLiftState.stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**抽象状态接口**
|
||||||
|
|
||||||
|
```java
|
||||||
|
@Setter
|
||||||
|
abstract class LiftState {
|
||||||
|
protected Lift lift;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 开门动作
|
||||||
|
*/
|
||||||
|
public abstract void open();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 关门动作
|
||||||
|
*/
|
||||||
|
public abstract void close();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 运行动作
|
||||||
|
*/
|
||||||
|
public abstract void run();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 停止动作
|
||||||
|
*/
|
||||||
|
public abstract void stop();
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**开门状态---具体状态实现**
|
||||||
|
|
||||||
|
```java
|
||||||
|
public class OpeningLiftState extends LiftState {
|
||||||
|
/**
|
||||||
|
* 开门动作
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void open() {
|
||||||
|
System.out.println("电梯门慢慢打开。。。");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 关门动作
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void close() {
|
||||||
|
super.lift.setCurrentLiftState(Lift.CLOSE_STATE);
|
||||||
|
super.lift.getCurrentLiftState().close();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 运行动作
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 停止动作
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void stop() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**关门状态---具体状态实现**
|
||||||
|
|
||||||
|
```java
|
||||||
|
public class ClosingLiftState extends LiftState {
|
||||||
|
/**
|
||||||
|
* 开门动作
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void open() {
|
||||||
|
super.lift.setCurrentLiftState(Lift.OPEN_STATE);
|
||||||
|
super.lift.getCurrentLiftState().open();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 关门动作
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void close() {
|
||||||
|
System.out.println("电梯关门。。。");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 运行动作
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
super.lift.setCurrentLiftState(Lift.RUNNING_STATE);
|
||||||
|
super.lift.getCurrentLiftState().run();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 停止动作
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void stop() {
|
||||||
|
super.lift.setCurrentLiftState(Lift.STOPPING_STATE);
|
||||||
|
super.lift.getCurrentLiftState().stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**运行状态---具体状态实现**
|
||||||
|
|
||||||
|
```java
|
||||||
|
public class RunningLiftState extends LiftState {
|
||||||
|
/**
|
||||||
|
* 开门动作
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void open() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 关门动作
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void close() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 运行动作
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
System.out.println("电梯正在运行。。。");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 停止动作
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void stop() {
|
||||||
|
super.lift.setCurrentLiftState(Lift.STOPPING_STATE);
|
||||||
|
super.lift.getCurrentLiftState().stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**停止状态---具体状态实现**
|
||||||
|
|
||||||
|
```java
|
||||||
|
public class StoppingLiftState extends LiftState {
|
||||||
|
/**
|
||||||
|
* 开门动作
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void open() {
|
||||||
|
super.lift.setCurrentLiftState(Lift.OPEN_STATE);
|
||||||
|
super.lift.getCurrentLiftState().open();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 关门动作
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void close() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 运行动作
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
super.lift.setCurrentLiftState(Lift.RUNNING_STATE);
|
||||||
|
super.lift.getCurrentLiftState().run();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 停止动作
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void stop() {
|
||||||
|
System.out.println("电梯停止。。。");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 运行结果
|
||||||
|
|
||||||
|
**当运行时**
|
||||||
|
|
||||||
|
```java
|
||||||
|
public class Client {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Lift lift = new Lift();
|
||||||
|
lift.setCurrentLiftState(Lift.RUNNING_STATE);
|
||||||
|
lift.open();
|
||||||
|
lift.run();
|
||||||
|
lift.close();
|
||||||
|
lift.stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**运行结果**
|
||||||
|
|
||||||
|
```java
|
||||||
|
电梯正在运行。。。
|
||||||
|
电梯停止。。。
|
||||||
|
```
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,7 @@ import java.util.List;
|
||||||
public class Composite {
|
public class Composite {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
ConcreteCompany company = new ConcreteCompany("总公司");
|
ConcreteCompany company = new ConcreteCompany("总公司");
|
||||||
|
|
||||||
// 总公司的分公司
|
// 总公司的分公司
|
||||||
ConcreteCompany subCompany1 = new ConcreteCompany("上海分公司");
|
ConcreteCompany subCompany1 = new ConcreteCompany("上海分公司");
|
||||||
subCompany1.add(new HRDepartment("上海华东分公司"));
|
subCompany1.add(new HRDepartment("上海华东分公司"));
|
||||||
|
|
|
@ -0,0 +1,14 @@
|
||||||
|
package cn.bunny.pattern15;
|
||||||
|
|
||||||
|
import cn.bunny.pattern15.state.Lift;
|
||||||
|
|
||||||
|
public class Client {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Lift lift = new Lift();
|
||||||
|
lift.setCurrentLiftState(Lift.RUNNING_STATE);
|
||||||
|
lift.open();
|
||||||
|
lift.run();
|
||||||
|
lift.close();
|
||||||
|
lift.stop();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,39 @@
|
||||||
|
package cn.bunny.pattern15.state;
|
||||||
|
|
||||||
|
|
||||||
|
public class ClosingLiftState extends LiftState {
|
||||||
|
/**
|
||||||
|
* 开门动作
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void open() {
|
||||||
|
super.lift.setCurrentLiftState(Lift.OPEN_STATE);
|
||||||
|
super.lift.getCurrentLiftState().open();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 关门动作
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void close() {
|
||||||
|
System.out.println("电梯关门。。。");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 运行动作
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
super.lift.setCurrentLiftState(Lift.RUNNING_STATE);
|
||||||
|
super.lift.getCurrentLiftState().run();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 停止动作
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void stop() {
|
||||||
|
super.lift.setCurrentLiftState(Lift.STOPPING_STATE);
|
||||||
|
super.lift.getCurrentLiftState().stop();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,40 @@
|
||||||
|
package cn.bunny.pattern15.state;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
public class Lift {
|
||||||
|
// 开门状态
|
||||||
|
public static LiftState OPEN_STATE = new OpeningLiftState();
|
||||||
|
// 关门状态
|
||||||
|
public static LiftState CLOSE_STATE = new ClosingLiftState();
|
||||||
|
// 运行状态
|
||||||
|
public static LiftState RUNNING_STATE = new RunningLiftState();
|
||||||
|
// 停止状态
|
||||||
|
public static LiftState STOPPING_STATE = new StoppingLiftState();
|
||||||
|
|
||||||
|
// 当前电梯状态
|
||||||
|
private LiftState currentLiftState;
|
||||||
|
|
||||||
|
public void setCurrentLiftState(LiftState currentState) {
|
||||||
|
this.currentLiftState = currentState;
|
||||||
|
// 执行开门动作
|
||||||
|
this.currentLiftState.lift = this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void open() {
|
||||||
|
currentLiftState.open();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void close() {
|
||||||
|
currentLiftState.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
currentLiftState.run();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void stop() {
|
||||||
|
currentLiftState.stop();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,28 @@
|
||||||
|
package cn.bunny.pattern15.state;
|
||||||
|
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
@Setter
|
||||||
|
abstract class LiftState {
|
||||||
|
protected Lift lift;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 开门动作
|
||||||
|
*/
|
||||||
|
public abstract void open();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 关门动作
|
||||||
|
*/
|
||||||
|
public abstract void close();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 运行动作
|
||||||
|
*/
|
||||||
|
public abstract void run();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 停止动作
|
||||||
|
*/
|
||||||
|
public abstract void stop();
|
||||||
|
}
|
|
@ -0,0 +1,36 @@
|
||||||
|
package cn.bunny.pattern15.state;
|
||||||
|
|
||||||
|
public class OpeningLiftState extends LiftState {
|
||||||
|
/**
|
||||||
|
* 开门动作
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void open() {
|
||||||
|
System.out.println("电梯门慢慢打开。。。");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 关门动作
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void close() {
|
||||||
|
super.lift.setCurrentLiftState(Lift.CLOSE_STATE);
|
||||||
|
super.lift.getCurrentLiftState().close();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 运行动作
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 停止动作
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void stop() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,36 @@
|
||||||
|
package cn.bunny.pattern15.state;
|
||||||
|
|
||||||
|
public class RunningLiftState extends LiftState {
|
||||||
|
/**
|
||||||
|
* 开门动作
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void open() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 关门动作
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void close() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 运行动作
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
System.out.println("电梯正在运行。。。");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 停止动作
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void stop() {
|
||||||
|
super.lift.setCurrentLiftState(Lift.STOPPING_STATE);
|
||||||
|
super.lift.getCurrentLiftState().stop();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,37 @@
|
||||||
|
package cn.bunny.pattern15.state;
|
||||||
|
|
||||||
|
public class StoppingLiftState extends LiftState {
|
||||||
|
/**
|
||||||
|
* 开门动作
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void open() {
|
||||||
|
super.lift.setCurrentLiftState(Lift.OPEN_STATE);
|
||||||
|
super.lift.getCurrentLiftState().open();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 关门动作
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void close() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 运行动作
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
super.lift.setCurrentLiftState(Lift.RUNNING_STATE);
|
||||||
|
super.lift.getCurrentLiftState().run();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 停止动作
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void stop() {
|
||||||
|
System.out.println("电梯停止。。。");
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue