Algo-Sim › Design Patterns

State: a document workflow

Easy · Design Patterns · step through it in Java, Python and C++ on your own input

Each state of an object is its own class implementing every action; the context forwards actions to its current state, which returns the next state. Transitions live with the state they leave.

Problem

A document moves through Draft → Review → Published. publish() moves Draft to Review and Review to Published; reject() sends a document under review back to Draft; edit() turns Review or Published back into a Draft. Actions that make no sense in a state leave it unchanged. Return the state’s name after every action. Implement each state as a class, with no switch on a status field.

Constraints

Edge cases to test

Follow-up questions

When is a switch statement good enough?
With two or three states and few actions; the pattern pays off when each new state would otherwise touch every switch.
How would you persist the state?
Store a state id and rebuild the state object on load; the classes stay stateless singletons.

Solution

Java

interface DocState {                    // each state returns the next one
    DocState publish();
    DocState reject();
    DocState edit();
    String name();
}

class Draft implements DocState {
    public DocState publish() { return new Review(); }
    public DocState reject() { return this; }
    public DocState edit() { return this; }
    public String name() { return "Draft"; }
}

class Review implements DocState {
    public DocState publish() { return new Published(); }
    public DocState reject() { return new Draft(); }
    public DocState edit() { return new Draft(); }
    public String name() { return "Review"; }
}

class Published implements DocState {
    public DocState publish() { return this; }
    public DocState reject() { return this; }
    public DocState edit() { return new Draft(); }
    public String name() { return "Published"; }
}

class Document {
    private DocState state = new Draft();

    public String publish() { state = state.publish(); return state.name(); }
    public String reject() { state = state.reject(); return state.name(); }
    public String edit() { state = state.edit(); return state.name(); }
}

Python

class Draft:                            # each state returns the next one
    def publish(self):
        return Review()

    def reject(self):
        return self

    def edit(self):
        return self

    def name(self):
        return "Draft"


class Review:
    def publish(self):
        return Published()

    def reject(self):
        return Draft()

    def edit(self):
        return Draft()

    def name(self):
        return "Review"


class Published:
    def publish(self):
        return self

    def reject(self):
        return self

    def edit(self):
        return Draft()

    def name(self):
        return "Published"


class Document:
    def __init__(self):
        self.state = Draft()

    def publish(self):
        self.state = self.state.publish()
        return self.state.name()

    def reject(self):
        self.state = self.state.reject()
        return self.state.name()

    def edit(self):
        self.state = self.state.edit()
        return self.state.name()

C++

#include <bits/stdc++.h>
using namespace std;
struct DocState : enable_shared_from_this<DocState> {
    virtual shared_ptr<DocState> publish() = 0;
    virtual shared_ptr<DocState> reject() = 0;
    virtual shared_ptr<DocState> edit() = 0;
    virtual string name() const = 0;
    virtual ~DocState() = default;
};
struct Draft : DocState {
    shared_ptr<DocState> publish() override;
    shared_ptr<DocState> reject() override {
        return shared_from_this();
    }
    shared_ptr<DocState> edit() override {
        return shared_from_this();
    }
    string name() const override {
        return "Draft";
    }
};
struct Review : DocState {
    shared_ptr<DocState> publish() override;
    shared_ptr<DocState> reject() override {
        return make_shared<Draft>();
    }
    shared_ptr<DocState> edit() override {
        return make_shared<Draft>();
    }
    string name() const override {
        return "Review";
    }
};
struct Published : DocState {
    shared_ptr<DocState> publish() override {
        return shared_from_this();
    }
    shared_ptr<DocState> reject() override {
        return shared_from_this();
    }
    shared_ptr<DocState> edit() override {
        return make_shared<Draft>();
    }
    string name() const override {
        return "Published";
    }
};
shared_ptr<DocState> Draft::publish() {
    return make_shared<Review>();
}
shared_ptr<DocState> Review::publish() {
    return make_shared<Published>();
}
class Document {
    shared_ptr<DocState> state = make_shared<Draft>();
  public:
    string publish() {
        state = state->publish();
        return state->name();
    }
    string reject() {
        state = state->reject();
        return state->name();
    }
    string edit() {
        state = state->edit();
        return state->name();
    }
};

More Design Patterns problems