BAEL-1997 state design pattern in Java (#4827)
* BAEL-1997 state design pattern in Java * BAEL-1997 different example code * BAEL-1997 add additional method to the states * BAEL-1997 clean up in ReceivedState
This commit is contained in:
parent
4cd349f533
commit
d3a02b789e
|
@ -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{}";
|
||||
}
|
||||
|
||||
}
|
|
@ -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{}";
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package com.baeldung.state;
|
||||
|
||||
public interface PackageState {
|
||||
|
||||
void next(Package pkg);
|
||||
|
||||
void prev(Package pkg);
|
||||
|
||||
void printStatus();
|
||||
}
|
|
@ -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{}";
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue