设计模式之State模式

本文介绍了状态模式的概念及其在软件设计中的应用。通过一个具体的例子展示了如何使用状态模式来改变对象的行为,并对比了状态模式与代理模式的区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

State模式类似于switch的多路分钟功能:

状态模式的ULM图:


状态模式用于改变目标对象的行为方式,随着状态变化目标程序从一个转到另一个目标程序。

package state;

public class Creature{
    //状态接口
    private interface State{
          String response();
    }
    private class Forg implements State{
          public String response(){
	   return "Ribbet!";
          }
    }
    private class Prince implements State{
          public String response(){
	   return "Darling!";
          }
    }
    private State state = new Forg();
    public void greet(){
          System.out.println(state.response());
    }
    public void kiss(){
          state = new Prince();
    }
    public static void main(String[] args){
          Creature creature = new Creature();
          creature.greet();
          creature.kiss();
          creature.greet();
    } 
}


 

State状态设计模式中,状态自动切换并传播,不需要再改动标识,代码显得非常优雅。

State状态设计模式一个基本框架如下:

//状态接口
interface State{
    void operation1();
    void operation2();
    void operation3();
}
//状态实现类1
class implementation1 implements State{
    public void operation1(){
        System.out.println(“Implementation1.operation1()”);
    }
    public void operation2(){
        System.out.println(“Implementation1.operation2()”);
    }
   public void operation3(){
        System.out.println(“Implementation1.operation3()”);
    }
}
//状态实现类2
class implementation2 implements State{
    public void operation1(){
        System.out.println(“Implementation2.operation1()”);
    }
    public void operation2(){
        System.out.println(“Implementation2.operation2()”);
    }
   public void operation3(){
        System.out.println(“Implementation2.operation3()”);
    }
}
//服务提供者
class ServiceProvider{
    private State state;
    public ServiceProvider(State state){
         this.state = state;
    }
    //状态更改
    public void changeState(State newState){
         state = newState;
    }
    public void service1(){
          //……
          state.operation1();
          //……
          state.operation3();
    }
    public void service2(){
          //……
          state.operation1();
          //……
          state.operation2();
    }
    public void service3(){
          //……
          state.operation3();
          //……
          state.operation2();
    }
}
public class StateDemo{
    private ServiceProvider sp = new ServiceProvider(new Implementation1());
    private void run(ServiceProvider sp){
         sp.service1();
         sp.service2();
         sp.service3();
    }
    public static void main(String[] args){
        StateDemo demo = new StateDemo();
        demo.run(sp);
        sp.changeState(new Implementation2());
        demo.run(sp);
    }
}

State模式和Proxy模式都为目标程序提供一个目标程序代理,真正的目标程序被隐藏了。当客户端调用目标程序,首先会发送请求给代理程序,代理程序才会真正调用目标程序。但是Proxy代理模式和State状态模式有如下区别:

(1).Proxy代理模式中被调用的目标程序只有一个,而State状态模式中被调用的目标程序有多个。

(2).Proxy代理模式的目的是控制客户端对目标程序的访问,而State状态模式是为了根据条件动态改变目标程序。


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值