State Pattern

State Pattern

A State Pattern says that “the class behavior changes based on its state”. In State Pattern, we create objects which represent various states and a context object whose behavior varies as its state object changes.

The State Pattern is also known as Objects for States.

Benefits:

  • It keeps the state-specific behavior.
  • It makes any state transitions explicit.

Usage:

  • When the behavior of object depends on its state and it must be able to change its behavior at runtime according to the new state.
  • It is used when the operations have large, multipart conditional statements that depend on the state of an object.

UML for State Pattern:

State Pattern

Implementation of State Pattern:

Step 1:

Create a Connection interface that will provide the connection to the Controller class.

public interface Connection {

  public void open();

  public void close();

  public void log();

  public void update();

}// End of the Connection interface.

Step 2:

Create an Accounting class that will implement to the Connection interface.

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.

 

Step 3:

Create a Sales class that will implement to the Connection interface.

//This is a class.  

public class Sales implements Connection {

@Override

publicvoidopen() {

System.out.println(“open database for sales”);

}

@Override

publicvoidclose() {

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.

Step 4:

Create a Sales class that will implement to the Connection interface.

public class Sales implements Connection {

@Override

publicvoidopen() {

System.out.println(“open database for sales”);

}

@Override

publicvoidclose() {

System.out.println(“close the database”);

}

@Override

publicvoidlog() {

System.out.println(“log activities”);

}

@Override

publicvoidupdate() {

System.out.println(“Sales has been updated”);

}

}// End of the Sales class.

Step 5:

Create a Management class that will implement to the Connection interface.

public class Management implements Connection {

@Override

publicvoidopen() {

System.out.println(“open database for Management”);

}

@Override

publicvoidclose() {

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”);

}

}

Step 6:

Create a Controller class that will use the Connection interface for connecting with different types of connection.

public class Controller {

publicstaticAccounting acct;

publicstaticSales sales;

publicstaticManagement management;

privatestaticConnection 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;

}

publicvoidopen() {

con.open();

}

publicvoidclose() {

con.close();

}

publicvoidlog() {

con.log();

}

publicvoidupdate() {

con.update();

}

}// End of the Controller class.

Step 7:

Create a StatePatternDemo 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

State Pattern

 

Popular posts from this blog

What is Garbage collection in Spark and its impact and resolution

Window function in PySpark with Joins example using 2 Dataframes (inner join)

Complex SQL: fetch the users who logged in consecutively 3 or more times (lead perfect example)