设计模式3——State设计模式
来源:互联网 发布:sql如何删除表 编辑:程序博客网 时间:2024/04/29 04:57
State状态设计模式类似于Switch多路分支功能的开关,State状态模式机制如下:
状态模式UML图如下:
State状态设计模式用于改变对象的行为,在代理的生命周期里,随着状态变化从一个目标实现程序切换到另一个目标实现程序。
我们经常遇到如下的程序代码:
- public class Creature{
- private Boolean isFrog = true;//标识
- public void greet(){
- if(isForg){
- System.out.println(“Ribbet!”);
- }else{
- System.out.println(“Darling!”);
- }
- }
- //转换标识
- public void kiss(){
- isForg = false;
- }
- public static void main(String[] args){
- Creature creature = new Creature();
- creature.greet();
- creature.kiss();
- creature.greet();
- }
- }
- 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状态模式是为了根据条件动态改变目标程序。
0 0
- 设计模式3—State设计模式
- 设计模式—State模式
- 设计模式3——State设计模式
- 设计模式3——State设计模式
- 设计模式——状态模式(State)
- JAVA设计模式—状态模式(State)
- 设计模式:State模式
- 设计模式--State 模式
- 设计模式(3)- 状态设计模式State
- 设计模式之State
- 设计模式之State
- 设计模式之State
- 设计模式小议:state
- 设计模式之State
- 设计模式之State
- State设计模式
- 设计模式之State
- 设计模式之State
- 设计模式2——Proxy设计模式
- Cocos2d-JS开发中的一些小技巧
- 设备地址映射到用户空间(mmap)
- Android JNI(Java Native Interface)技术介绍
- 工作流设计简介
- 设计模式3——State设计模式
- MVC3 Razor @RenderSection
- 【OC学习-15】如何判断两个对象是否属于同一个类?如何判断一个对象是否属于某一个类?
- cname记录
- vc++实现word应用程序事件监听及响应
- C++实现文件简单加密与解密(读取文件中的字符,对字符转换,然后写入,完成加密,解密反之)
- http://movie.douban.com/doulist/12825932/
- svn相关
- 23 种设计模式