diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/DeliveredState.java b/patterns/design-patterns/src/main/java/com/baeldung/state/DeliveredState.java new file mode 100644 index 0000000000..9f5e4d8fc4 --- /dev/null +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/DeliveredState.java @@ -0,0 +1,25 @@ +package com.baeldung.state; + +public class DeliveredState implements PackageState { + + @Override + public void next(Package pkg) { + pkg.setState(new ReceivedState()); + } + + @Override + public void prev(Package pkg) { + pkg.setState(new OrderedState()); + } + + @Override + public void printStatus() { + System.out.println("Package delivered to post office, not received yet."); + } + + @Override + public String toString() { + return "DeliveredState{}"; + } + +} \ No newline at end of file diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/OrderedState.java b/patterns/design-patterns/src/main/java/com/baeldung/state/OrderedState.java new file mode 100644 index 0000000000..0642c4c73c --- /dev/null +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/OrderedState.java @@ -0,0 +1,24 @@ +package com.baeldung.state; + +public class OrderedState implements PackageState { + + @Override + public void next(Package pkg) { + pkg.setState(new DeliveredState()); + } + + @Override + public void prev(Package pkg) { + System.out.println("The package is in it's root state."); + } + + @Override + public void printStatus() { + System.out.println("Package ordered, not delivered to the office yet."); + } + + @Override + public String toString() { + return "OrderedState{}"; + } +} \ No newline at end of file diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/Package.java b/patterns/design-patterns/src/main/java/com/baeldung/state/Package.java new file mode 100644 index 0000000000..f3dfbb3fa7 --- /dev/null +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/Package.java @@ -0,0 +1,26 @@ +package com.baeldung.state; + +public class Package { + + private PackageState state = new OrderedState(); + + public PackageState getState() { + return state; + } + + public void setState(PackageState state) { + this.state = state; + } + + public void previousState() { + state.prev(this); + } + + public void nextState() { + state.next(this); + } + + public void printStatus() { + state.printStatus(); + } +} diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/PackageState.java b/patterns/design-patterns/src/main/java/com/baeldung/state/PackageState.java new file mode 100644 index 0000000000..d6656c78ac --- /dev/null +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/PackageState.java @@ -0,0 +1,10 @@ +package com.baeldung.state; + +public interface PackageState { + + void next(Package pkg); + + void prev(Package pkg); + + void printStatus(); +} \ No newline at end of file diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/ReceivedState.java b/patterns/design-patterns/src/main/java/com/baeldung/state/ReceivedState.java new file mode 100644 index 0000000000..84136fa48e --- /dev/null +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/ReceivedState.java @@ -0,0 +1,24 @@ +package com.baeldung.state; + +public class ReceivedState implements PackageState { + + @Override + public void next(Package pkg) { + System.out.println("This package is already received by a client."); + } + + @Override + public void prev(Package pkg) { + pkg.setState(new DeliveredState()); + } + + @Override + public void printStatus() { + System.out.println("Package was received by client."); + } + + @Override + public String toString() { + return "ReceivedState{}"; + } +} \ No newline at end of file diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/StateDemo.java b/patterns/design-patterns/src/main/java/com/baeldung/state/StateDemo.java new file mode 100644 index 0000000000..1a63ea3ddf --- /dev/null +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/StateDemo.java @@ -0,0 +1,19 @@ +package com.baeldung.state; + +public class StateDemo { + + public static void main(String[] args) { + + Package pkg = new Package(); + pkg.printStatus(); + + pkg.nextState(); + pkg.printStatus(); + + pkg.nextState(); + pkg.printStatus(); + + pkg.nextState(); + pkg.printStatus(); + } +} diff --git a/patterns/design-patterns/src/test/java/com/baeldung/state/StatePatternUnitTest.java b/patterns/design-patterns/src/test/java/com/baeldung/state/StatePatternUnitTest.java new file mode 100644 index 0000000000..731974f92b --- /dev/null +++ b/patterns/design-patterns/src/test/java/com/baeldung/state/StatePatternUnitTest.java @@ -0,0 +1,33 @@ +package com.baeldung.state; + +import com.baeldung.state.Package; + +import static org.junit.Assert.assertThat; +import static org.hamcrest.CoreMatchers.instanceOf; + +import org.junit.Test; + +public class StatePatternUnitTest { + + @Test + public void givenNewPackage_whenPackageReceived_thenStateReceived() { + Package pkg = new Package(); + + assertThat(pkg.getState(), instanceOf(OrderedState.class)); + pkg.nextState(); + + assertThat(pkg.getState(), instanceOf(DeliveredState.class)); + pkg.nextState(); + + assertThat(pkg.getState(), instanceOf(ReceivedState.class)); + } + + @Test + public void givenDeliveredPackage_whenPrevState_thenStateOrdered() { + Package pkg = new Package(); + pkg.setState(new DeliveredState()); + pkg.previousState(); + + assertThat(pkg.getState(), instanceOf(OrderedState.class)); + } +}