Skip to main content
add language hints for code syntax highlighter.
Source Link
user40980
user40980
interface Statelike {
    void writeName(StateContext context, String name);
}
 
class StateLowerCase implements Statelike {
    @Override
    public void writeName(final StateContext context, final String name) {
        System.out.println(name.toLowerCase());
        context.setState(new StateMultipleUpperCase());
    }
}
 
class StateMultipleUpperCase implements Statelike {
    /** Counter local to this state */
    private int count = 0;
 
    @Override
    public void writeName(final StateContext context, final String name) {
        System.out.println(name.toUpperCase());
        /* Change state after StateMultipleUpperCase's writeName() gets invoked twice */
        if(++count > 1) {
            context.setState(new StateLowerCase());
        }
    }
}
interface Statelike {
    void writeName(StateContext context, String name);
}
 
class StateLowerCase implements Statelike {
    @Override
    public void writeName(final StateContext context, final String name) {
        System.out.println(name.toLowerCase());
        context.setState(new StateMultipleUpperCase());
    }
}
 
class StateMultipleUpperCase implements Statelike {
    /** Counter local to this state */
    private int count = 0;
 
    @Override
    public void writeName(final StateContext context, final String name) {
        System.out.println(name.toUpperCase());
        /* Change state after StateMultipleUpperCase's writeName() gets invoked twice */
        if(++count > 1) {
            context.setState(new StateLowerCase());
        }
    }
}
class StateContext {
    private Statelike myState;
    StateContext() {
        setState(new StateLowerCase());
    }
 
    /**
     * Setter method for the state.
     * Normally only called by classes implementing the State interface.
     * @param newState the new state of this context
     */
    void setState(final Statelike newState) {
        myState = newState;
    }
 
    public void writeName(final String name) {
        myState.writeName(this, name);
    }
}
class StateContext {
    private Statelike myState;
    StateContext() {
        setState(new StateLowerCase());
    }
 
    /**
     * Setter method for the state.
     * Normally only called by classes implementing the State interface.
     * @param newState the new state of this context
     */
    void setState(final Statelike newState) {
        myState = newState;
    }
 
    public void writeName(final String name) {
        myState.writeName(this, name);
    }
}
interface Statelike {
    void writeName(StateContext context, String name);
}
 
class StateLowerCase implements Statelike {
    @Override
    public void writeName(final StateContext context, final String name) {
        System.out.println(name.toLowerCase());
        context.setState(new StateMultipleUpperCase());
    }
}
 
class StateMultipleUpperCase implements Statelike {
    /** Counter local to this state */
    private int count = 0;
 
    @Override
    public void writeName(final StateContext context, final String name) {
        System.out.println(name.toUpperCase());
        /* Change state after StateMultipleUpperCase's writeName() gets invoked twice */
        if(++count > 1) {
            context.setState(new StateLowerCase());
        }
    }
}
class StateContext {
    private Statelike myState;
    StateContext() {
        setState(new StateLowerCase());
    }
 
    /**
     * Setter method for the state.
     * Normally only called by classes implementing the State interface.
     * @param newState the new state of this context
     */
    void setState(final Statelike newState) {
        myState = newState;
    }
 
    public void writeName(final String name) {
        myState.writeName(this, name);
    }
}
interface Statelike {
    void writeName(StateContext context, String name);
}
 
class StateLowerCase implements Statelike {
    @Override
    public void writeName(final StateContext context, final String name) {
        System.out.println(name.toLowerCase());
        context.setState(new StateMultipleUpperCase());
    }
}
 
class StateMultipleUpperCase implements Statelike {
    /** Counter local to this state */
    private int count = 0;
 
    @Override
    public void writeName(final StateContext context, final String name) {
        System.out.println(name.toUpperCase());
        /* Change state after StateMultipleUpperCase's writeName() gets invoked twice */
        if(++count > 1) {
            context.setState(new StateLowerCase());
        }
    }
}
class StateContext {
    private Statelike myState;
    StateContext() {
        setState(new StateLowerCase());
    }
 
    /**
     * Setter method for the state.
     * Normally only called by classes implementing the State interface.
     * @param newState the new state of this context
     */
    void setState(final Statelike newState) {
        myState = newState;
    }
 
    public void writeName(final String name) {
        myState.writeName(this, name);
    }
}
deleted 22 characters in body
Source Link
Robert Harvey
  • 200.7k
  • 55
  • 470
  • 683

The Wikipedia article for State Pattern has a Java example that illustrates two states, involving two different methods. Those methods can be arbitrarily complex, so I consider a two-state solution (no pun intended) perfectly valid.

interface Statelike {
 
    void writeName(StateContext context, String name);
 
}
 
class StateLowerCase implements Statelike {
 
    @Override
    public void writeName(final StateContext context, final String name) {
        System.out.println(name.toLowerCase());
        context.setState(new StateMultipleUpperCase());
    }
 
}
 
class StateMultipleUpperCase implements Statelike {
    /** Counter local to this state */
    private int count = 0;
 
    @Override
    public void writeName(final StateContext context, final String name) {
        System.out.println(name.toUpperCase());
        /* Change state after StateMultipleUpperCase's writeName() gets invoked twice */
        if(++count > 1) {
            context.setState(new StateLowerCase());
        }
    }
}

Note that writeName swaps out its own implementation by handing a new StateLike object to the StateContext when the i count exceeds one.

class StateContext {
    private Statelike myState;
    StateContext() {
        setState(new StateLowerCase());
    }
 
    /**
     * Setter method for the state.
     * Normally only called by classes implementing the State interface.
     * @param newState the new state of this context
     */
    void setState(final Statelike newState) {
        myState = newState;
    }
 
    public void writeName(final String name) {
        myState.writeName(this, name);
    }
}

A State Pattern would be indicated if your "machinery" substantially changes between states. The complexity of the condition needed to choose the correct processing object doesn't matter; it's the complexity of the state objects themselves that are the deciding factor. Otherwise, you could just write all of the logic into a single class.

Think about what happens when you build a car. The chassis moves along an assembly line and stops at a station where the welding takes place. Once that state has completed, the chassis moves to the next station on the assembly line, where a different set of robots with completely different programming installs the engine.

The Wikipedia article for State Pattern has a Java example that illustrates two states, involving two different methods. Those methods can be arbitrarily complex, so I consider a two-state solution (no pun intended) perfectly valid.

interface Statelike {
 
    void writeName(StateContext context, String name);
 
}
 
class StateLowerCase implements Statelike {
 
    @Override
    public void writeName(final StateContext context, final String name) {
        System.out.println(name.toLowerCase());
        context.setState(new StateMultipleUpperCase());
    }
 
}
 
class StateMultipleUpperCase implements Statelike {
    /** Counter local to this state */
    private int count = 0;
 
    @Override
    public void writeName(final StateContext context, final String name) {
        System.out.println(name.toUpperCase());
        /* Change state after StateMultipleUpperCase's writeName() gets invoked twice */
        if(++count > 1) {
            context.setState(new StateLowerCase());
        }
    }
}

Note that writeName swaps out its own implementation by handing a new StateLike object to the StateContext when the i count exceeds one.

class StateContext {
    private Statelike myState;
    StateContext() {
        setState(new StateLowerCase());
    }
 
    /**
     * Setter method for the state.
     * Normally only called by classes implementing the State interface.
     * @param newState the new state of this context
     */
    void setState(final Statelike newState) {
        myState = newState;
    }
 
    public void writeName(final String name) {
        myState.writeName(this, name);
    }
}

A State Pattern would be indicated if your "machinery" substantially changes between states. The complexity of the condition needed to choose the correct processing object doesn't matter; it's the complexity of the objects themselves that are the deciding factor. Otherwise, you could just write all of the logic into a single class.

Think about what happens when you build a car. The chassis moves along an assembly line and stops at a station where the welding takes place. Once that state has completed, the chassis moves to the next station on the assembly line, where a different set of robots with completely different programming installs the engine.

The Wikipedia article for State Pattern has a Java example that illustrates two states, involving two different methods. Those methods can be arbitrarily complex, so I consider a two-state solution (no pun intended) perfectly valid.

interface Statelike {
    void writeName(StateContext context, String name);
}
 
class StateLowerCase implements Statelike {
    @Override
    public void writeName(final StateContext context, final String name) {
        System.out.println(name.toLowerCase());
        context.setState(new StateMultipleUpperCase());
    }
}
 
class StateMultipleUpperCase implements Statelike {
    /** Counter local to this state */
    private int count = 0;
 
    @Override
    public void writeName(final StateContext context, final String name) {
        System.out.println(name.toUpperCase());
        /* Change state after StateMultipleUpperCase's writeName() gets invoked twice */
        if(++count > 1) {
            context.setState(new StateLowerCase());
        }
    }
}

Note that writeName swaps out its own implementation by handing a new StateLike object to the StateContext when the i count exceeds one.

class StateContext {
    private Statelike myState;
    StateContext() {
        setState(new StateLowerCase());
    }
 
    /**
     * Setter method for the state.
     * Normally only called by classes implementing the State interface.
     * @param newState the new state of this context
     */
    void setState(final Statelike newState) {
        myState = newState;
    }
 
    public void writeName(final String name) {
        myState.writeName(this, name);
    }
}

A State Pattern would be indicated if your "machinery" substantially changes between states. The complexity of the condition needed to choose the correct processing object doesn't matter; it's the complexity of the state objects themselves that are the deciding factor. Otherwise, you could just write all of the logic into a single class.

Think about what happens when you build a car. The chassis moves along an assembly line and stops at a station where the welding takes place. Once that state has completed, the chassis moves to the next station on the assembly line, where a different set of robots with completely different programming installs the engine.

Source Link
Robert Harvey
  • 200.7k
  • 55
  • 470
  • 683

The Wikipedia article for State Pattern has a Java example that illustrates two states, involving two different methods. Those methods can be arbitrarily complex, so I consider a two-state solution (no pun intended) perfectly valid.

interface Statelike {
 
    void writeName(StateContext context, String name);
 
}
 
class StateLowerCase implements Statelike {
 
    @Override
    public void writeName(final StateContext context, final String name) {
        System.out.println(name.toLowerCase());
        context.setState(new StateMultipleUpperCase());
    }
 
}
 
class StateMultipleUpperCase implements Statelike {
    /** Counter local to this state */
    private int count = 0;
 
    @Override
    public void writeName(final StateContext context, final String name) {
        System.out.println(name.toUpperCase());
        /* Change state after StateMultipleUpperCase's writeName() gets invoked twice */
        if(++count > 1) {
            context.setState(new StateLowerCase());
        }
    }
}

Note that writeName swaps out its own implementation by handing a new StateLike object to the StateContext when the i count exceeds one.

class StateContext {
    private Statelike myState;
    StateContext() {
        setState(new StateLowerCase());
    }
 
    /**
     * Setter method for the state.
     * Normally only called by classes implementing the State interface.
     * @param newState the new state of this context
     */
    void setState(final Statelike newState) {
        myState = newState;
    }
 
    public void writeName(final String name) {
        myState.writeName(this, name);
    }
}

A State Pattern would be indicated if your "machinery" substantially changes between states. The complexity of the condition needed to choose the correct processing object doesn't matter; it's the complexity of the objects themselves that are the deciding factor. Otherwise, you could just write all of the logic into a single class.

Think about what happens when you build a car. The chassis moves along an assembly line and stops at a station where the welding takes place. Once that state has completed, the chassis moves to the next station on the assembly line, where a different set of robots with completely different programming installs the engine.