本文概述
状态模式表示“类行为根据其状态而改变”。在状态模式中, 我们创建表示各种状态的对象和上下文对象, 其行为随其状态对象的变化而变化。
状态模式也称为状态对象。
好处
- 它保留特定于状态的行为。
- 它使任何状态转换都明确。
用法
- 当对象的行为取决于其状态时, 它必须能够在运行时根据新状态更改其行为。
- 当操作具有依赖于对象状态的大型, 多部分条件语句时, 将使用它。
用于状态模式的UML
状态模式的实现
步骤1:
创建一个Connection接口, 该接口将提供到Controller类的连接。
//This is an interface.
public interface Connection {
public void open();
public void close();
public void log();
public void update();
}// End of the Connection interface.
第2步:
创建一个将实现到Connection接口的Accounting类。
//This is a class.
public class Accounting implements Connection {
@Override
public void open() {
System.out.println("open database for accounting");
}
@Override
public void close() {
System.out.println("close the database");
}
@Override
public void log() {
System.out.println("log activities");
}
@Override
public void update() {
System.out.println("Accounting has been updated");
}
}// End of the Accounting class.
第三步:
创建一个将实现到Connection接口的Sales类。
//This is a class.
public class Sales implements Connection {
@Override
public void open() {
System.out.println("open database for sales");
}
@Override
public void close() {
System.out.println("close the database");
}
@Override
public void log() {
System.out.println("log activities");
}
@Override
public void update() {
System.out.println("Sales has been updated");
}
}// End of the Sales class.
步骤4:
创建一个将实现到Connection接口的Sales类。
//This is a class.
public class Sales implements Connection {
@Override
public void open() {
System.out.println("open database for sales");
}
@Override
public void close() {
System.out.println("close the database");
}
@Override
public void log() {
System.out.println("log activities");
}
@Override
public void update() {
System.out.println("Sales has been updated");
}
}// End of the Sales class.
步骤5:
创建一个将实现到Connection接口的Management类。
//This is a class.
public class Management implements Connection {
@Override
public void open() {
System.out.println("open database for Management");
}
@Override
public void close() {
System.out.println("close the database");
}
@Override
public void log() {
System.out.println("log activities");
}
@Override
public void update() {
System.out.println("Management has been updated");
}
}
// End of the Management class.
步骤6:
创建一个Controller类, 该类将使用Connection接口与不同类型的连接进行连接。
//This is a class.
public class Controller {
public static Accounting acct;
public static Sales sales;
public static Management management;
private static Connection con;
Controller() {
acct = new Accounting();
sales = new Sales();
management = new Management();
}
public void setAccountingConnection() {
con = acct;
}
public void setSalesConnection() {
con = sales;
}
public void setManagementConnection() {
con = management;
}
public void open() {
con .open();
}
public void close() {
con .close();
}
public void log() {
con .log();
}
public void update() {
con .update();
}
}// End of the Controller class.
步骤7:
创建一个StatePatternDemo类。
//This is a class.
public class StatePatternDemo {
Controller controller;
StatePatternDemo(String con) {
controller = new Controller();
//the following trigger should be made by the user
if(con.equalsIgnoreCase("management"))
controller.setManagementConnection();
if(con.equalsIgnoreCase("sales"))
controller.setSalesConnection();
if(con.equalsIgnoreCase("accounting"))
controller.setAccountingConnection();
controller.open();
controller.log();
controller.close();
controller.update();
}
public static void main(String args[]) {
new StatePatternDemo(args[0]);
}
}// End of the StatePatternDemo class.
输出:
评论前必须登录!
注册