Java Magazine, July/August 2018
ORACLE COM JAVAMAGAZINE JULY AUGUST 2018 71 patterns @ Override public void pause Do nothing already stopped @ Override public void rewind currentState rewindingState currentState enterState The last two statements in rewind show how an object of the Context class can appear to change its class Lets look at the main programs methods such as stop and start PlayerStateDemo context new PlayerStateDemo System out println Initial state context getState User presses the Start button context start System out println Current state context getState User presses the Stop button context stop System out println Current state context getState These methods invoke or delegate to the current State objects methods Just by having a new value assigned to the currentState variable the program appears to change its class because diferent code will be invoked from the same method call based on whichever State object is current For example if you call stop while playing the player will stop but if you call the very same method a second time it will do nothing because the first invocation invokes the stop method in playingState while the second calls the stop method in stopState
You must have JavaScript enabled to view digital editions.