lundi 22 novembre 2021

Java state pattern with different properties and sql

I have main domain class Broadcast that can have three states:

public class Broadcast {
    private Long id;
    private String title;
    private Author author;
    private State state;

    // constructor, getters and setters                                                     
}

Each state contains different properties.

public abstract class State {
    private Broadcast stateContext;

    public State(Broadcast stateContext) {
        this.stateContext = stateContext;
    }
}

public class UpcomingState extends State {
    private OffsetDateTime createdAt;
    private OffsetDateTime scheduledAt;

    public UpcomingState(Broadcast stateContext) {
        super(stateContext);
    }

    // getters and setters
}

public class LiveState extends State {
    private OffsetDateTime startedAt;

    public LiveState(Broadcast stateContext) {
        super(stateContext);
    }

    // getters and setters
}

public class EndedState extends State {
    private OffsetDateTime startedAt;
    private OffsetDateTime endedAt;

    public EndedState(Broadcast stateContext) {
        super(stateContext);
    }

    // getters and setters
}

How do I design the tables in the database to store objects of this class (for example, postgresql) while still preserving information about state transitions, assuming some states might be applied multiple times?

For example, upcoming -> live (first data) -> live (second data) -> ended.

Aucun commentaire:

Enregistrer un commentaire