Design Patterns Handbook
  • Introduction
  • Creational Patterns
    • Builder
    • Factory
    • Abstract Factory
    • Factory Method
    • Prototype
    • Singleton
    • Object Pool
    • Revealing Constructor
  • Structural Patterns
    • Adapter
    • Composite
    • Proxy
    • Flyweight
    • Facade
    • Bridge
    • Decorator
    • Private Class Data
  • Behavioral Patterns
    • Template Method
    • Mediator
    • Chain Of Responsibility
    • Observer
    • Strategy
    • Command
    • State
    • Visitor
    • Memento
    • Interpreter
    • Null Object
    • Iterator
    • Middleware
  • Clean Code Patterns
    • Extract Method
    • Clarify Responsibility
    • Remove Duplications
    • Keep Refactoring
    • Always Unit Test
    • Create Data Type
    • Comment to Better Name
    • Consistent Naming
    • If-else over ternary operator
    • Composition over Inheritance
    • Too Many Returns
    • Private to Interface
  • Anti Patterns
    • Big Ball of Mud
    • Singleton
    • Mad Scientist
    • Spaghetti Code
    • It Will Never Happen
    • Error Codes
    • Commented Code
    • Abbreviations
    • Prefixes
    • Over Patternized
    • Generic Interface over Function
Powered by GitBook
On this page

Was this helpful?

  1. Behavioral Patterns

State

  • Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.

  • An object-oriented state machine

  • wrapper + polymorphic wrappee + collaboration

Example - UI states

We are going to use state pattern to implement different states of a login screen.

class TextField {
}

class Button {
}

interface State {
    void usernameTextField(TextField textField);

    void passwordTextField(TextField textField);

    void loginButtonTextField(Button button);
}

class EmptyState implements State {

    @Override
    public void usernameTextField(TextField textField) {
        // textField.setEnabled(true);
    }

    @Override
    public void passwordTextField(TextField textField) {
        // textField.setEnabled(true);
    }

    @Override
    public void loginButtonTextField(Button button) {
        // textField.setEnabled(false);
    }
}

class ReadyState implements State {

    @Override
    public void usernameTextField(TextField textField) {
        // textField.setEnabled(true);
    }

    @Override
    public void passwordTextField(TextField textField) {
        // textField.setEnabled(true);
    }

    @Override
    public void loginButtonTextField(Button button) {
        // textField.setEnabled(true);
    }
}

class UserLoginUI {
    private State state;

    public UserLoginUI() {
        this.state = new EmptyState();
    }

    public void applyState(State state) {
        this.state = state;
        this.state.usernameTextField(null);
        this.state.passwordTextField(null);
        this.state.loginButtonTextField(null);
    }
}

Here is how we would use the states.

UserLoginUI loginUI = new UserLoginUI();

// user just opened the login dialog
loginUI.applyState(new EmptyState());

// when user fills in username and password
loginUI.applyState(new ReadyState());

// when user clicks on Login button and it fails
//loginUI.setState(new ErrorState());
PreviousCommandNextVisitor

Last updated 5 years ago

Was this helpful?