DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Object Relational Behavioral Design Patterns in Java
  • Distribution Design Patterns in Java - Data Transfer Object (DTO) And Remote Facade Design Patterns
  • Context Object Design Pattern in Java: Introduction and Key Points
  • Java: Object Pool Design Pattern

Trending

  • Start Coding With Google Cloud Workstations
  • Why I Started Using Dependency Injection in Python
  • Testing SingleStore's MCP Server
  • It’s Not About Control — It’s About Collaboration Between Architecture and Security
  1. DZone
  2. Coding
  3. Languages
  4. State Design Pattern In Java

State Design Pattern In Java

State Design Pattern — a behavioral design pattern that allows an object to change its behavior when its internal state changes.

By 
Brijesh Saxena user avatar
Brijesh Saxena
DZone Core CORE ·
Oct. 19, 20 · Analysis
Likes (6)
Comment
Save
Tweet
Share
5.4K Views

Join the DZone community and get the full member experience.

Join For Free

State Design Pattern — a behavioral design pattern that allows an object to change its behavior when its internal state changes.

State Design Pattern

  • The State Design Pattern is a Behavioral Design Pattern and one of the Gang of Four design patterns. 
  • The State allows an object to alter its behavior when its internal state changes.
  • The State pattern is similar to the concept of finite-state machines.
  • The State pattern is also similar to the Strategy Design Pattern which provides a way to switch a strategy through invocations of methods defined in the pattern's interface.
  • The State pattern encapsulates varying behavior for the object based on its internal state change.
  • The State pattern provides a cleaner way for an object to change its behavior at runtime.
  • By using the State pattern, the object changes its behavior when its internal state changes.
  • If we implement State-Specific behavior directly in the class, then we will not be able to change it without modifying the class.
  • In-State pattern, State-specific behavior should be defined independently because adding new states should not affect the behavior of existing states.
  • The context class delegates state-specific behavior to its current state object instead of implementing state-specific behavior directly.
  • This allows us to make our context class independent of how state-specific behavior is implemented. New state classes can be added without modifying context class.
  • The context class can change its behavior at run-time by changing its current state object.
  • To implement the State Design Pattern, we create a State interface to define some action. And then concrete classes that represent various states and a context object whose behavior varies as its state object changes.
  • The mixer in the kitchen is a good example of a state pattern, which has a motor and a control interface. Using the knob we can increase/decrease the speed of the mixer. Based on the speed state the behavior changes.
  • The TV which can be operated with a remote controller is another example of a State pattern. We can change the state of the TV by pressing buttons on the remote. But the state of TV will change or not, it depends on the current state of the TV. If the TV is switched OFF then only possible next state can be switch ON. And if TV is ON, we can switch it OFF, mute, or change aspects and source. But if TV is OFF, nothing will happen when we press the remote buttons. 
  • Java Threads are another good example of State pattern since they have defined states as New, Runnable, Blocked, Waiting, Timed Waiting and Terminated. 

To understand this better — let's take an example of Shipment Processing where the order status changes from place to receive. There can also be exceptions while shipping and the customer can also return it if he doesn't like it. I will try to keep the example as simple as possible and aligned to the use of State Pattern.

Shipment Processing Example Using State Design Pattern

In this example, we mainly focus on the Shipment of Order. So, let's define a class called Shipment:

Java
 




x
50


 
1
package org.trishinfotech.state;
2

          
3
public class Shipment {
4

          
5
    protected String orderNumber;
6
    protected String orderItem;
7
    protected String shipmentNumber;
8
    protected Location deliveryAddress;
9

          
10
    public Shipment(String orderNumber, String orderItem, Location deliveryAddress) {
11
        super();
12
        this.orderNumber = orderNumber;
13
        this.orderItem = orderItem;
14
        this.deliveryAddress = deliveryAddress;
15
    }
16

          
17
    public String getOrderNumber() {
18
        return orderNumber;
19
    }
20

          
21
    public void setOrderNumber(String orderNumber) {
22
        this.orderNumber = orderNumber;
23
    }
24

          
25
    public String getOrderItem() {
26
        return orderItem;
27
    }
28

          
29
    public void setOrderItem(String orderItem) {
30
        this.orderItem = orderItem;
31
    }
32

          
33
    public String getShipmentNumber() {
34
        return shipmentNumber;
35
    }
36

          
37
    public void setShipmentNumber(String shipmentNumber) {
38
        this.shipmentNumber = shipmentNumber;
39
    }
40

          
41
    public Location getDeliveryAddress() {
42
        return deliveryAddress;
43
    }
44

          
45
    public void setDeliveryAddress(Location deliveryAddress) {
46
        this.deliveryAddress = deliveryAddress;
47
    }
48

          
49
}
50

          



Since we need Location of shipment while we transport, let's define Location class:

Java
 




xxxxxxxxxx
1
79


 
1
package org.trishinfotech.state;
2

          
3
public class Location {
4

          
5
    protected String code;
6
    protected String address;
7
    protected String area;
8
    protected String city;
9
    protected String country;
10
    protected String zipCode;
11

          
12
    public Location(String code, String address, String area, String city, String country, String zipCode) {
13
        super();
14
        this.code = code;
15
        this.address = address;
16
        this.area = area;
17
        this.city = city;
18
        this.country = country;
19
        this.zipCode = zipCode;
20
    }
21

          
22
    public String getCode() {
23
        return code;
24
    }
25

          
26
    public void setCode(String code) {
27
        this.code = code;
28
    }
29

          
30
    public String getAddress() {
31
        return address;
32
    }
33

          
34
    public void setAddress(String address) {
35
        this.address = address;
36
    }
37

          
38
    public String getArea() {
39
        return area;
40
    }
41

          
42
    public void setArea(String area) {
43
        this.area = area;
44
    }
45

          
46
    public String getCity() {
47
        return city;
48
    }
49

          
50
    public void setCity(String city) {
51
        this.city = city;
52
    }
53

          
54
    public String getCountry() {
55
        return country;
56
    }
57

          
58
    public void setCountry(String country) {
59
        this.country = country;
60
    }
61

          
62
    public String getZipCode() {
63
        return zipCode;
64
    }
65

          
66
    public void setZipCode(String zipCode) {
67
        this.zipCode = zipCode;
68
    }
69

          
70
    @Override
71
    public String toString() {
72
        StringBuilder builder = new StringBuilder();
73
        builder.append(code).append(" (").append(address).append(" ").append(area).append(" ").append(city).append(" ")
74
                .append(country).append(" ").append(zipCode).append(")");
75
        return builder.toString();
76
    }
77

          
78
}
79

          



Now its time to define our state interface called ShipmentState which will allow us to have different shipment status (depending on our logic and input parameters) time to time.

Java
 




xxxxxxxxxx
1


 
1
package org.trishinfotech.state;
2

          
3
public interface ShipmentState {
4
    public String name();
5
    public void processShipment(ShipmentContext context);
6
}
7

          



Now lets define our context class called ShipmentContext which will have changing Status and hence the changing behavior as well.

Java
 




xxxxxxxxxx
1
53


 
1
package org.trishinfotech.state;
2

          
3
public class ShipmentContext {
4

          
5
    protected Shipment shipment;
6
    protected ShipmentState currentState;
7
    protected boolean customerAtLocation = false;
8
    protected int currentLocationIndex = 0;
9

          
10
    public ShipmentContext(Shipment shipment) {
11
        super();
12
        this.shipment = shipment;
13
        this.currentState = new OrderPlaced();
14
    }
15

          
16
    public Shipment getShipment() {
17
        return shipment;
18
    }
19

          
20
    public void setShipment(Shipment shipment) {
21
        this.shipment = shipment;
22
    }
23

          
24
    public ShipmentState getCurrentState() {
25
        return currentState;
26
    }
27

          
28
    public void setCurrentState(ShipmentState currentState) {
29
        this.currentState = currentState;
30
    }
31

          
32
    public int getCurrentLocationIndex() {
33
        return currentLocationIndex;
34
    }
35

          
36
    public void setCurrentLocationIndex(int currentLocationIndex) {
37
        this.currentLocationIndex = currentLocationIndex;
38
    }
39

          
40
    public boolean isCustomerAtLocation() {
41
        return customerAtLocation;
42
    }
43

          
44
    public void setCustomerAtLocation(boolean customerAtLocation) {
45
        this.customerAtLocation = customerAtLocation;
46
    }
47

          
48
    public void processShipment() {
49
        currentState.processShipment(this);
50
    }
51

          
52
}
53

          



Now we will define concrete status classes to represent different order/shipment status.

Below status class we will create:

  • Code for OrderPlaced class:
Java
 




xxxxxxxxxx
1
23


 
1
package org.trishinfotech.state;
2

          
3
public class OrderPlaced implements ShipmentState {
4

          
5
    public OrderPlaced() {
6
        super();
7
    }
8

          
9
    @Override
10
    public void processShipment(ShipmentContext context) {
11
        Shipment shipment = context.getShipment();
12
        System.out.printf("Order#'%s' for '%s' has status '%s'.\nDelivery Address is '%s'\n", shipment.getOrderNumber(),
13
                shipment.getOrderItem(), name(), shipment.getDeliveryAddress());
14
        System.out.println("--------------------------------------------------------------");
15
        context.setCurrentState(new ProcessingStock());
16
    }
17

          
18
    @Override
19
    public String name() {
20
        return "Order Placed";
21
    }
22
}
23

          



  • Code for ProcessingStock class:
Java
 




xxxxxxxxxx
1
23


 
1
package org.trishinfotech.state;
2

          
3
public class ProcessingStock implements ShipmentState {
4

          
5
    public ProcessingStock() {
6
        super();
7
    }
8

          
9
    @Override
10
    public void processShipment(ShipmentContext context) {
11
        Shipment shipment = context.getShipment();
12
        System.out.printf("Order#'%s' for '%s' has status '%s'.\nDelivery Address is '%s'\n", shipment.getOrderNumber(),
13
                shipment.getOrderItem(), name(), shipment.getDeliveryAddress());
14
        System.out.println("--------------------------------------------------------------");
15
        context.setCurrentState(new ReadyForPacking());
16
    }
17

          
18
    @Override
19
    public String name() {
20
        return "Processing Stock";
21
    }
22
}
23

          



  • Code for ReadyForPacking class:
Java
 




xxxxxxxxxx
1
23


 
1
package org.trishinfotech.state;
2

          
3
public class ReadyForPacking implements ShipmentState {
4

          
5
    public ReadyForPacking() {
6
        super();
7
    }
8

          
9
    @Override
10
    public void processShipment(ShipmentContext context) {
11
        Shipment shipment = context.getShipment();
12
        System.out.printf("Order#'%s' for '%s' has status '%s'.\nDelivery Address is '%s'\n", shipment.getOrderNumber(),
13
                shipment.getOrderItem(), name(), shipment.getDeliveryAddress());
14
        System.out.println("--------------------------------------------------------------");
15
        context.setCurrentState(new ReadyToDeliver());
16
    }
17

          
18
    @Override
19
    public String name() {
20
        return "Ready For Packing";
21
    }
22
}
23

          



  • Code for ReadyToDeliver class:
Java
 




xxxxxxxxxx
1
26


 
1
package org.trishinfotech.state;
2

          
3
public class ReadyToDeliver implements ShipmentState {
4

          
5
    public ReadyToDeliver() {
6
        super();
7
    }
8

          
9
    @Override
10
    public void processShipment(ShipmentContext context) {
11
        Shipment shipment = context.getShipment();
12
        System.out.printf("Order#'%s' for '%s' has status '%s'.\nDelivery Address is '%s'\n", shipment.getOrderNumber(),
13
                shipment.getOrderItem(), name(), shipment.getDeliveryAddress());
14
        System.out.println("--------------------------------------------------------------");
15
        // shipment tracking number will be generated.
16
        shipment.setShipmentNumber("ST6749398FLNY26");
17
        Location originLocation = new Location("LocA", "54 Essex Rd.", "Palm Bay", "FL", "US", "32907");
18
        context.setCurrentState(new DeliveryInProgress(ItineraryFinder.findItinerry(originLocation, context.getShipment().getDeliveryAddress())));
19
    }
20

          
21
    @Override
22
    public String name() {
23
        return "Ready To Deliver";
24
    }
25
}
26

          



To make the example little interesting without making it complex, I added shipment itinerary required for moving the order from warehouse to the customer address. And to achieve that I wrote a class called ItineraryFinder which has hard-coded intermediate locations (routing/interchange points) while we move the order. 

Java
 




xxxxxxxxxx
1
19


 
1
package org.trishinfotech.state;
2

          
3
import java.util.Arrays;
4
import java.util.List;
5
import java.util.stream.Collectors;
6

          
7
public class ItineraryFinder {
8

          
9
    public static List<Location> findItinerry(Location originLocation, Location destinationLocation) {
10
        // hard-coded itinerary locations to keep the code simple.
11
        return Arrays.stream(new Location[] { originLocation,
12
                new Location("LocB", "335 Hall Street", "Pelham", "AL", "US", "35124"),
13
                new Location("LocC", "409 Gates St.", "Hightstown", "NJ", "", "08520"),
14
                new Location("LocD", "540 Cemetery Street", "Brooklyn", "NY", "US", "11203"), destinationLocation })
15
                .collect(Collectors.toList());
16
    }
17

          
18
}
19

          



  • Code for DeliveryInProgress class:
Java
 




xxxxxxxxxx
1
38


 
1
package org.trishinfotech.state;
2

          
3
import java.util.ArrayList;
4
import java.util.List;
5

          
6
public class DeliveryInProgress implements ShipmentState {
7

          
8
    protected List<Location> shipmentItinerary = new ArrayList<Location>();
9

          
10
    public DeliveryInProgress(List<Location> itinerary) {
11
        super();
12
        this.shipmentItinerary.addAll(itinerary);
13
    }
14

          
15
    @Override
16
    public void processShipment(ShipmentContext context) {
17
        int currentLocationIndex = context.getCurrentLocationIndex();
18
        Shipment shipment = context.getShipment();
19
        System.out.printf(
20
                "Order#'%s' for '%s' has status '%s'.\nDelivery Address is '%s'\nCurrent shipment location is '%s'\n",
21
                shipment.getOrderNumber(), shipment.getOrderItem(), name(), shipment.getDeliveryAddress(),
22
                shipmentItinerary.get(currentLocationIndex++));
23
        System.out.println("--------------------------------------------------------------");
24
        // since destination address will be part of OutForDelivery; We skip last
25
        // location of itinerary
26
        if (currentLocationIndex == (shipmentItinerary.size() - 1)) {
27
            context.setCurrentState(new OutForDelivery());
28
        } else {
29
            context.setCurrentLocationIndex(currentLocationIndex);
30
        }
31
    }
32

          
33
    @Override
34
    public String name() {
35
        return "Delivery In Progress";
36
    }
37
}
38

          



  • Code for OutForDelivery class:
Java
 




x


 
 
1
package org.trishinfotech.state;
2

          
3
public class OutForDelivery implements ShipmentState {
4

          
5
    public OutForDelivery() {
6
        super();
7
    }
8

          
9
    @Override
10
    public void processShipment(ShipmentContext context) {
11
        Shipment shipment = context.getShipment();
12
        System.out.printf("Order#'%s' for '%s' has status '%s'.\nDelivery Address is '%s'\n", shipment.getOrderNumber(),
13
                shipment.getOrderItem(), name(), shipment.getDeliveryAddress());
14
        System.out.println("--------------------------------------------------------------");
15
        if (context.isCustomerAtLocation()) {
16
            context.setCurrentState(new Delivered());
17
        } else {
18
            context.setCurrentState(new DeliveryAttempted("Customer not at Home!"));
19
        }
20
    }
21

          
22
    @Override
23
    public String name() {
24
        return "Out For Delivery";
25
    }
26
}
27

          



To demonstrate how we can have different states based on condition, I made customer not available for 1st time we do OutForDelivery. So, that will go to DeliveryAttempted status.

  • Code for DeliveryAttempted class:
Java
 




xxxxxxxxxx
1
38


 
1
package org.trishinfotech.state;
2

          
3
public class DeliveryAttempted implements ShipmentState {
4

          
5
    protected String reasonForUndelivered;
6

          
7
    public DeliveryAttempted(String reasonForUndelivered) {
8
        super();
9
        this.reasonForUndelivered = reasonForUndelivered;
10
    }
11

          
12
    @Override
13
    public void processShipment(ShipmentContext context) {
14
        Shipment shipment = context.getShipment();
15
        System.out.printf("Order#'%s' for '%s' has status '%s (%s)'.\nDelivery Address is '%s'\n",
16
                shipment.getOrderNumber(), shipment.getOrderItem(), name(), reasonForUndelivered,
17
                shipment.getDeliveryAddress());
18
        System.out.println("--------------------------------------------------------------");
19
        // setting flag to make the delivery at next attempt.
20
        // we can set it logically as well instead of hard coding.
21
        context.setCustomerAtLocation(true);
22
        context.setCurrentState(new OutForDelivery());
23
    }
24

          
25
    public String getReasonForUndelivered() {
26
        return reasonForUndelivered;
27
    }
28

          
29
    public void setReasonForUndelivered(String reasonForUndelivered) {
30
        this.reasonForUndelivered = reasonForUndelivered;
31
    }
32

          
33
    @Override
34
    public String name() {
35
        return "Delivery Attempted";
36
    }
37
}
38

          



From DeliveryAttempted it will again go for OutForDelivery status and this time I made customer available at home to ensure the delivery.

  • Code for Delivered class:
Java
 




xxxxxxxxxx
1
34


 
1
package org.trishinfotech.state;
2

          
3
public class Delivered implements ShipmentState {
4

          
5
    protected String deliveryNote;
6
    public Delivered() {
7
        super();
8
    }
9

          
10
    @Override
11
    public void processShipment(ShipmentContext context) {
12
        Shipment shipment = context.getShipment();
13
        deliveryNote = "Order Package Handover to Customer";
14
        System.out.printf("Order#'%s' for '%s' has status '%s (%s)'.\nDelivery Address is '%s'\n", shipment.getOrderNumber(),
15
                shipment.getOrderItem(), name(), deliveryNote, shipment.getDeliveryAddress());
16
        System.out.println("--------------------------------------------------------------");
17
        context.setCurrentState(new Received());
18
    }
19

          
20
    public String getDeliveryNote() {
21
        return deliveryNote;
22
    }
23

          
24
    public void setDeliveryNote(String deliveryNote) {
25
        this.deliveryNote = deliveryNote;
26
    }
27

          
28
    @Override
29
    public String name() {
30
        return "Delivered";
31
    }
32

          
33
}
34

          



  • Code for Received class (The status for acknowledgement provided from customer upon receiving of the ordered item): 
Java
 




xxxxxxxxxx
1
25


 
1
package org.trishinfotech.state;
2

          
3
public class Received implements ShipmentState {
4

          
5
    public Received() {
6
        super();
7
    }
8

          
9
    @Override
10
    public void processShipment(ShipmentContext context) {
11
        Shipment shipment = context.getShipment();
12
        System.out.printf("Order#'%s' for '%s' has status '%s'.\nDelivery Address is '%s'\n", shipment.getOrderNumber(),
13
                shipment.getOrderItem(), name(), shipment.getDeliveryAddress());
14
        System.out.println("--------------------------------------------------------------");
15
        // this is the end of the order processing.
16
        // if we like to make it further to return the item we can uncomment the below line.
17
        // context.setCurrentState(new Returned());
18
    }
19

          
20
    @Override
21
    public String name() {
22
        return "Received";
23
    }
24
}
25

          



Below two shipment-status Exception (package lost/damage) and Returned (customer does not like), I didn't use in the example. But provided the code to cover commonly used shipment status. Please feel free to use in the use cases you have.

  • Code for Exception class:
Java
 




xxxxxxxxxx
1
35


 
1
package org.trishinfotech.state;
2

          
3
public class Exception implements ShipmentState {
4

          
5
    protected String exceptionMsg;
6

          
7
    public Exception(String exceptionMsg) {
8
        super();
9
        this.exceptionMsg = exceptionMsg;
10
    }
11

          
12
    @Override
13
    public void processShipment(ShipmentContext context) {
14
        Shipment shipment = context.getShipment();
15
        System.out.printf("Order#'%s' for '%s' has status '%s (%s)'.\nDelivery Address is '%s'\n",
16
                shipment.getOrderNumber(), shipment.getOrderItem(), name(), exceptionMsg,
17
                shipment.getDeliveryAddress());
18
        System.out.println("--------------------------------------------------------------");
19
        context.setCurrentState(new Returned());
20
    }
21

          
22
    public String getExceptionMsg() {
23
        return exceptionMsg;
24
    }
25

          
26
    public void setExceptionMsg(String exceptionMsg) {
27
        this.exceptionMsg = exceptionMsg;
28
    }
29

          
30
    @Override
31
    public String name() {
32
        return "Exception";
33
    }
34
}
35

          



  • Code for Returned class:
Java
 




xxxxxxxxxx
1
25


 
1
package org.trishinfotech.state;
2

          
3
public class Returned implements ShipmentState {
4

          
5
    public Returned() {
6
        super();
7
    }
8

          
9
    @Override
10
    public void processShipment(ShipmentContext context) {
11
        Shipment shipment = context.getShipment();
12
        System.out.printf("Order#'%s' for '%s' has status '%s'.\nDelivery Address is '%s'\n", shipment.getOrderNumber(),
13
                shipment.getOrderItem(), name(), shipment.getDeliveryAddress());
14
        System.out.println("--------------------------------------------------------------");
15
        // If we like to ship replacement we can uncomment the below line and implement
16
        // status for replacement.
17
        // context.setCurrentState(...));
18
    }
19

          
20
    @Override
21
    public String name() {
22
        return "Returned";
23
    }
24
}
25

          



Now, its time to write Main class to execute and test the output:

Java
 




xxxxxxxxxx
1
22


 
1
package org.trishinfotech.state;
2

          
3
public class Main {
4

          
5
    public static void main(String[] args) {
6
        Shipment shipment = new Shipment("OD12345FLNY17", "Apple iPhone 11 Pro (Midnight Green, 256GB)",
7
                new Location("LocE", "101 W. Sage Ave.", "Brooklyn", "NY", "US", "11234"));
8
        ShipmentContext context = new ShipmentContext(shipment);
9
        do {
10
            context.processShipment();
11
        } while (notReceived(context.getCurrentState()));
12
        // shipment received. so, we need to process the received status to get
13
        // acknowledgement from customer as 'item received'.
14
        context.processShipment();
15
    }
16

          
17
    private static boolean notReceived(ShipmentState currentState) {
18
        return !"Received".equalsIgnoreCase(currentState.name());
19
    }
20

          
21
}
22

          



Below is the output of the program:

Java
 




xxxxxxxxxx
1
44


 
1
Order#'OD12345FLNY17' for 'Apple iPhone 11 Pro (Midnight Green, 256GB)' has status 'Order Placed'.
2
Delivery Address is 'LocE (101 W. Sage Ave. Brooklyn NY US 11234)'
3
--------------------------------------------------------------
4
Order#'OD12345FLNY17' for 'Apple iPhone 11 Pro (Midnight Green, 256GB)' has status 'Processing Stock'.
5
Delivery Address is 'LocE (101 W. Sage Ave. Brooklyn NY US 11234)'
6
--------------------------------------------------------------
7
Order#'OD12345FLNY17' for 'Apple iPhone 11 Pro (Midnight Green, 256GB)' has status 'Ready For Packing'.
8
Delivery Address is 'LocE (101 W. Sage Ave. Brooklyn NY US 11234)'
9
--------------------------------------------------------------
10
Order#'OD12345FLNY17' for 'Apple iPhone 11 Pro (Midnight Green, 256GB)' has status 'Ready To Deliver'.
11
Delivery Address is 'LocE (101 W. Sage Ave. Brooklyn NY US 11234)'
12
--------------------------------------------------------------
13
Order#'OD12345FLNY17' for 'Apple iPhone 11 Pro (Midnight Green, 256GB)' has status 'Delivery In Progress'.
14
Delivery Address is 'LocE (101 W. Sage Ave. Brooklyn NY US 11234)'
15
Current shipment location is 'LocA (54 Essex Rd. Palm Bay FL US 32907)'
16
--------------------------------------------------------------
17
Order#'OD12345FLNY17' for 'Apple iPhone 11 Pro (Midnight Green, 256GB)' has status 'Delivery In Progress'.
18
Delivery Address is 'LocE (101 W. Sage Ave. Brooklyn NY US 11234)'
19
Current shipment location is 'LocB (335 Hall Street Pelham AL US 35124)'
20
--------------------------------------------------------------
21
Order#'OD12345FLNY17' for 'Apple iPhone 11 Pro (Midnight Green, 256GB)' has status 'Delivery In Progress'.
22
Delivery Address is 'LocE (101 W. Sage Ave. Brooklyn NY US 11234)'
23
Current shipment location is 'LocC (409 Gates St. Hightstown NJ  08520)'
24
--------------------------------------------------------------
25
Order#'OD12345FLNY17' for 'Apple iPhone 11 Pro (Midnight Green, 256GB)' has status 'Delivery In Progress'.
26
Delivery Address is 'LocE (101 W. Sage Ave. Brooklyn NY US 11234)'
27
Current shipment location is 'LocD (540 Cemetery Street Brooklyn NY US 11203)'
28
--------------------------------------------------------------
29
Order#'OD12345FLNY17' for 'Apple iPhone 11 Pro (Midnight Green, 256GB)' has status 'Out For Delivery'.
30
Delivery Address is 'LocE (101 W. Sage Ave. Brooklyn NY US 11234)'
31
--------------------------------------------------------------
32
Order#'OD12345FLNY17' for 'Apple iPhone 11 Pro (Midnight Green, 256GB)' has status 'Delivery Attempted (Customer not at Home!)'.
33
Delivery Address is 'LocE (101 W. Sage Ave. Brooklyn NY US 11234)'
34
--------------------------------------------------------------
35
Order#'OD12345FLNY17' for 'Apple iPhone 11 Pro (Midnight Green, 256GB)' has status 'Out For Delivery'.
36
Delivery Address is 'LocE (101 W. Sage Ave. Brooklyn NY US 11234)'
37
--------------------------------------------------------------
38
Order#'OD12345FLNY17' for 'Apple iPhone 11 Pro (Midnight Green, 256GB)' has status 'Delivered (Order Package Handover to Customer)'.
39
Delivery Address is 'LocE (101 W. Sage Ave. Brooklyn NY US 11234)'
40
--------------------------------------------------------------
41
Order#'OD12345FLNY17' for 'Apple iPhone 11 Pro (Midnight Green, 256GB)' has status 'Received'.
42
Delivery Address is 'LocE (101 W. Sage Ave. Brooklyn NY US 11234)'
43
--------------------------------------------------------------
44

          



I hope this tutorial helped and demonstrate the concept and implementation of the State Design Pattern.

Source Code can be found here: State-Design-Pattern-Sample-Code 

Need more articles, please visit my profile: Brijesh Saxena

Liked the article? Please don't forget to press that like button. Happy coding!

Java (programming language) Design State pattern Object (computer science)

Opinions expressed by DZone contributors are their own.

Related

  • Object Relational Behavioral Design Patterns in Java
  • Distribution Design Patterns in Java - Data Transfer Object (DTO) And Remote Facade Design Patterns
  • Context Object Design Pattern in Java: Introduction and Key Points
  • Java: Object Pool Design Pattern

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!