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:
Denis
2018-08-11 00:31:45 +02:00
committed by Predrag Maric
parent 4cd349f533
commit d3a02b789e
7 changed files with 161 additions and 0 deletions

View File

@@ -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));
}
}