当前位置:网站首页>状态模式,身随心变
状态模式,身随心变
2022-06-26 05:36:00 【绿毛水怪12138】
什么是状态模式
状态模式(state):当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了其类。
UML图

小练习
实现请假机制(不同长度要又不同的人决定)
public class Context {
protected State state;
private int time;
private String status;
public Context(State state,int time){
this.state=state;
this.time=time;
status="未答复";
}
public void request(){
state.handle(this);
}
public int getTime(){
return time;
}
public void setStatus(String a){
status=a;
}
public String getStatus(){
return status;}
}
public abstract class State {
public abstract void handle(Context context);
public abstract String getOpinion();
}
public class ProjectManager extends State{
@Override
public void handle(Context context) {
System.out.println("项目经理处理请假");
if(getOpinion().equals("同意")){
if(context.getTime()>3){
context.state=new DepartManager();
context.request();
}else context.setStatus("同意");
}else context.setStatus("不同意");
}
@Override
public String getOpinion() {
// TODO Auto-generated method stub
return "同意";
}
}
public class DepartManager extends State{
@Override
public void handle(Context context) {
System.out.println("部门经理处理请假");
// TODO Auto-generated method stub
if(getOpinion().equals("同意")){
context.setStatus("同意");
}else context.setStatus("不同意");
}
@Override
public String getOpinion() {
return "同意";
}
}
public class test {
public static void main(String[] args) {
Context context=new Context(new ProjectManager(), 3);
context.request();
System.out.println(context.getStatus());
}
}
使用场景
1、 如果对象需要根据自身当前状态进行不同行为, 同时状态的数量非常多且与状态相关的代码会频繁变更的话, 可使用状态模式。
模式建议你将所有特定于状态的代码抽取到一组独立的类中。 这样一来, 你可以在独立于其他状态的情况下添加新状态或修改已有状态, 从而减少维护成本。
2、当相似状态和基于条件的状态机转换中存在许多重复代码时, 可使用状态模式。
状态模式让你能够生成状态类层次结构, 通过将公用代码抽取到抽象基类中来减少重复。
与其他设计模式的关系
边栏推荐
- AutowiredAnnotationBeanPostProcessor什么时候被实例化的?
- cartographer_local_trajectory_builder_2d
- Could not get unknown property ‘*‘ for SigningConfig container of type org. gradle. api. internal
- The wechat team disclosed that the wechat interface is stuck with a super bug "15..." The context of
- Project suspension
- Red team scoring method statistics
- 生命原来如此脆弱
- uniCloud云开发获取小程序用户openid
- Introduction to alluxio
- 11 IO frame
猜你喜欢

Ad tutorial series | 4 - creating an integration library file

How does P2P technology reduce the bandwidth of live video by 75%?

Official image acceleration

Command line interface of alluxio

Leetcode513. Find the value in the lower left corner of the tree

Supplementary course on basic knowledge of IM development (II): how to design a server-side storage architecture for a large number of image files?

cartographer_ optimization_ problem_ 2d

Ribbon负载均衡服务调用

【活动推荐】云原生、产业互联网、低代码、Web3、元宇宙……哪个是 2022 年架构热点?...

Replacing domestic image sources in openwrt for soft routing (take Alibaba cloud as an example)
随机推荐
虚拟项目失败感想
LeetCode_二叉搜索树_简单_108.将有序数组转换为二叉搜索树
MySQL source code reading (II) login connection debugging
[arm] add desktop application for buildreoot of rk3568 development board
[arm] build boa based embedded web server on nuc977
Ad tutorial series | 4 - creating an integration library file
生命原来如此脆弱
Cyclic displacement
June 3 is a happy day
【活动推荐】云原生、产业互联网、低代码、Web3、元宇宙……哪个是 2022 年架构热点?...
Posting - don't get lost in the ocean of Technology
Installation and deployment of alluxio
skimage. morphology. medial_ axis
转帖——不要迷失在技术的海洋中
1212312321
Official image acceleration
一段不离不弃的爱情
基于SDN的DDoS攻击缓解
劣币驱逐良币的思考
pytorch(网络模型)