Java 中的状态设计模式

状态设计模式是行为设计模式之一,当一个对象根据其内部状态改变其行为时,使用状态设计模式。

国家设计模式

state design pattern, state design pattern in java, state pattern If we have to change the behavior of an object based on its state, we can have a state variable in the Object. Then use if-else condition block to perform different actions based on the state. State design pattern is used to provide a systematic and loosely coupled way to achieve this through Context and State implementations. State Pattern Context is the class that has a State reference to one of the concrete implementations of the State. Context forwards the request to the state object for processing. Let's understand this with a simple example. Suppose we want to implement a TV Remote with a simple button to perform action. If the State is ON, it will turn on the TV and if state is OFF, it will turn off the TV. We can implement it using if-else condition like below; TVRemoteBasic.java

 1package com.journaldev.design.state;
 2
 3public class TVRemoteBasic {
 4
 5    private String state="";
 6    
 7    public void setState(String state){
 8    	this.state=state;
 9    }
10    
11    public void doAction(){
12    	if(state.equalsIgnoreCase("ON")){
13    		System.out.println("TV is turned ON");
14    	}else if(state.equalsIgnoreCase("OFF")){
15    		System.out.println("TV is turned OFF");
16    	}
17    }
18
19    public static void main(String args[]){
20    	TVRemoteBasic remote = new TVRemoteBasic();
21    	
22    	remote.setState("ON");
23    	remote.doAction();
24    	
25    	remote.setState("OFF");
26    	remote.doAction();
27    }
28
29}

请注意,客户端代码应该知道用于设置远程状态的特定值。进一步,如果状态的数量增加,那么实现和客户端代码之间的紧密结合将非常难以维护和扩展。

国家设计模式界面

首先,我们将创建一个国家界面,该界面将定义不同的具体状态和背景类所实施的方法。

1package com.journaldev.design.state;
2
3public interface State {
4
5    public void doAction();
6}

国家设计模式 具体国家实施

在我们的例子中,我们可以有两个状态 - 一个是打开电视,另一个是关闭它.所以我们将为这些行为创建两个具体的状态实现。

 1package com.journaldev.design.state;
 2
 3public class TVStartState implements State {
 4
 5    @Override
 6    public void doAction() {
 7    	System.out.println("TV is turned ON");
 8    }
 9
10}

主頁 » 台灣 »

 1package com.journaldev.design.state;
 2
 3public class TVStopState implements State {
 4
 5    @Override
 6    public void doAction() {
 7    	System.out.println("TV is turned OFF");
 8    }
 9
10}

现在我们已经准备好实现我们的背景对象,它将根据其内部状态改变其行为。

国家设计模式框架实施

网址:Java

 1package com.journaldev.design.state;
 2
 3public class TVContext implements State {
 4
 5    private State tvState;
 6
 7    public void setState(State state) {
 8    	this.tvState=state;
 9    }
10
11    public State getState() {
12    	return this.tvState;
13    }
14
15    @Override
16    public void doAction() {
17    	this.tvState.doAction();
18    }
19
20}

请注意,Context还执行国家,并保留其当前状态的参考,并将请求提交给国家实施。

国家设计模式测试计划

现在让我们写一个简单的程序来测试我们的电视远程的状态模式实现。

 1package com.journaldev.design.state;
 2
 3public class TVRemote {
 4
 5    public static void main(String[] args) {
 6    	TVContext context = new TVContext();
 7    	State tvStartState = new TVStartState();
 8    	State tvStopState = new TVStopState();
 9    	
10    	context.setState(tvStartState);
11    	context.doAction();
12    	
13    	
14    	context.setState(tvStopState);
15    	context.doAction();
16    	
17    }
18
19}

上述程序的输出与不使用状态模式的电视远程基本实现相同。

国家设计模式优势

使用状态模式来实现多形行为的好处是显而易见的。错误的可能性较小,并且为额外的行为添加更多的状态很容易,从而使我们的代码更强大,易于维护和灵活。 国家模式也帮助避免这种情况下的 if-else 或 switch-case 条件逻辑。 国家模式与战略模式非常相似,请参阅 Java 中的战略模式。 这是所有用于 Java 中的国家设计模式,我希望你喜欢它。

Published At
Categories with 技术
comments powered by Disqus