State Pattern

State Pattern with Patterns, design, Creational Design, Abstract Factory Pattern, singleton design patterns, Adapter, Visitor Pattern, Template Pattern, Command Pattern, State Pattern, java etc.

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:


Implementation of State Pattern:

Step 1

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

  1. //This is an interface.  
  2.   
  3. public interface Connection {  
  4.   
  5.        public void open();  
  6.        public void close();  
  7.        public void log();  
  8.        public void update();  
  9. }// End of the Connection interface.  

Step 2:

Create an Accounting class that will implement the Connection interface.

  1. //This is a class.  
  2. public class Accounting implements Connection {  
  3.       
  4.        @Override  
  5.        public void open() {  
  6.           System.out.println("open database for accounting");  
  7.        }  
  8.        @Override  
  9.        public void close() {  
  10.           System.out.println("close the database");  
  11.        }  
  12.          
  13.        @Override  
  14.        public void log() {  
  15.           System.out.println("log activities");  
  16.        }  
  17.          
  18.        @Override  
  19.        public void update() {  
  20.            System.out.println("Accounting has been updated");  
  21.        }  
  22. }// End of the Accounting class.  

Step 3:

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

  1. //This is a class.  
  2. public class Sales implements Connection {  
  3.       
  4.       @Override  
  5.        public void open() {  
  6.           System.out.println("open database for sales");  
  7.        }  
  8.        @Override  
  9.        public void close() {  
  10.           System.out.println("close the database");  
  11.        }  
  12.          
  13.        @Override  
  14.        public void log() {  
  15.           System.out.println("log activities");  
  16.        }  
  17.          
  18.        @Override  
  19.        public void update() {  
  20.            System.out.println("Sales has been updated");  
  21.        }  
  22.   
  23. }// End of the Sales class.  

Step 4:

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

  1. //This is a class.  
  2.   
  3. public class Sales implements Connection {  
  4.       
  5.       @Override  
  6.        public void open() {  
  7.           System.out.println("open database for sales");  
  8.        }  
  9.        @Override  
  10.        public void close() {  
  11.           System.out.println("close the database");  
  12.        }   
  13.        @Override  
  14.        public void log() {  
  15.           System.out.println("log activities");  
  16.        }  
  17.        @Override  
  18.        public void update() {  
  19.            System.out.println("Sales has been updated");  
  20.        }  
  21. }// End of the Sales class.  

Step 5:

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

  1. //This is a class.  
  2.   
  3.   
  4. public class Management implements Connection {  
  5.       
  6.       @Override  
  7.        public void open() {  
  8.           System.out.println("open database for Management");  
  9.        }  
  10.        @Override  
  11.        public void close() {  
  12.           System.out.println("close the database");  
  13.        }  
  14.          
  15.        @Override  
  16.        public void log() {  
  17.           System.out.println("log activities");  
  18.        }  
  19.          
  20.        @Override  
  21.        public void update() {  
  22.            System.out.println("Management has been updated");  
  23.        }  
  24.   
  25. }  
  26.  // End of the Management class.  

Step 6:

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

  1. //This is a class.  
  2.   
  3. public class Controller {  
  4.       
  5.        public static Accounting acct;  
  6.        public static Sales sales;  
  7.        public static Management management;  
  8.          
  9.        private static Connection con;  
  10.          
  11.        Controller() {  
  12.            acct = new Accounting();  
  13.            sales = new Sales();  
  14.            management = new Management();  
  15.        }  
  16.       
  17.        public void setAccountingConnection() {  
  18.            con = acct;  
  19.        }  
  20.        public void setSalesConnection() {  
  21.            con  = sales;  
  22.        }  
  23.        public void setManagementConnection() {  
  24.            con  = management;  
  25.        }  
  26.        public void open() {  
  27.            con .open();  
  28.        }  
  29.        public void close() {  
  30.            con .close();  
  31.        }  
  32.        public void log() {  
  33.            con .log();  
  34.        }  
  35.        public void update() {  
  36.            con .update();  
  37.        }  
  38.          
  39.   
  40. }// End of the Controller class.  

Step 7:

Create a StatePatternDemo class.

  1. //This is a class.  
  2.   
  3.   
  4. public class StatePatternDemo {  
  5.   
  6.        Controller controller;  
  7.        StatePatternDemo(String con) {  
  8.           controller = new Controller();  
  9.           //the following trigger should be made by the user  
  10.           if(con.equalsIgnoreCase("management"))  
  11.              controller.setManagementConnection();  
  12.           if(con.equalsIgnoreCase("sales"))  
  13.              controller.setSalesConnection();  
  14.           if(con.equalsIgnoreCase("accounting"))  
  15.                  controller.setAccountingConnection();  
  16.           controller.open();  
  17.           controller.log();  
  18.           controller.close();  
  19.           controller.update();  
  20.        }  
  21.          
  22.          
  23.        public static void main(String args[]) {  
  24.   
  25.            new StatePatternDemo(args[0]);  
  26.              
  27.        }  
  28.   
  29. }// End of the StatePatternDemo class.