Behavioural Design patterns: State
The state pattern deals with altering an object’s behaviour when its state changes.
Imagine the case of a class responsible for generating user interface based on the state. You got anonymous, logged-in and admin users.
We shall create an interface called GreetingState which defines the action of drawing a html text with a welcome message to the user. There is going to be a different implementation according to the states that we have.
package com.gkatzioura.design.behavioural.state;
public interface GreetingState {
String create();
}We shall implement the GreetingState for the anonymous user.
package com.gkatzioura.design.behavioural.state;
public class AnonymousGreetingState implements GreetingState {
private static final String FOOTER_MESSAGE = "<p><Hello anonymous user!</p>";
@Override
public String create() {
return FOOTER_MESSAGE;
}
}Then we shall implement the GreetingState for the logged in user. This one would create a personalised message.
package com.gkatzioura.design.behavioural.state;
public class LoggedInGreetingState implements GreetingState {
private static final String FOOTER_MESSAGE = "<p><Hello %s!</p>";
private final String username;
public LoggedInGreetingState(final String username) {
this.username = username;
}
@Override
public String create() {
return String.format(FOOTER_MESSAGE,username);
}
}And at last the admin Footer.
package com.gkatzioura.design.behavioural.state;
import java.util.Date;
public class AdminGreetingState implements GreetingState {
private static final String FOOTER_MESSAGE = "<p><Hello %s, last login was at %s</p>";
private final String username;
private final Date lastLogin;
public AdminGreetingState(final String username, Date lastLogin) {
this.username = username;
this.lastLogin = lastLogin;
}
@Override
public String create() {
return String.format(FOOTER_MESSAGE,username,lastLogin);
}
}The we shall create the stateui context.
package com.gkatzioura.design.behavioural.state;
import java.io.PrintWriter;
public class StateUIContext {
private GreetingState greetingState;
public void setGreetingState(GreetingState greetingState) {
this.greetingState = greetingState;
}
public void create(PrintWriter printWriter) {
printWriter.write(greetingState.create());
}
}Let’s put them all together.
package com.gkatzioura.design.behavioural.state;
import java.io.PrintWriter;
import java.util.Date;
public class StateMain {
public static void main(String[] args) {
StateUIContext stateUIContext = new StateUIContext();
try(PrintWriter printWriter = new PrintWriter(System.out)) {
stateUIContext.setGreetingState(new AnonymousGreetingState());
stateUIContext.create(printWriter);
printWriter.write("\n");
stateUIContext.setGreetingState(new LoggedInGreetingState("someone"));
stateUIContext.create(printWriter);
printWriter.write("\n");
stateUIContext.setGreetingState(new AdminGreetingState("admin",new Date()));
stateUIContext.create(printWriter);
printWriter.write("\n");
}
}
}You can find the sourcecode on github.
| Published on Java Code Geeks with permission by Emmanouil Gkatziouras, partner at our JCG program. See the original article here: Behavioural Design patterns: State Opinions expressed by Java Code Geeks contributors are their own. |




I think you should come up with more realistic use cases