lundi 29 avril 2019

How to adapt the Chain of Responsibility pattern to pass state

I'm looking for a design that satisfies the following requirements:
1. There is a single, publicly-visible object that has the following interface:

class Whatever
{
    Whatever& GetIntstance();

    void UpdateData1(POD d1); // POD: Plain old datatype
    void UpdateData2(POD d2);
    // ...
    void UpdateDataN(POD dN);

    bool Question1();
    bool Question2();
    // ...
    bool QuestionM();
}

Where void UpdateDataX(POD dX) (1 <= X <= N) sets some internal state. Where bool QuestionY() (1 <= Y <= M) asks a yes/no question and is computed from internal state.
2. The Whatever instance may not know how to answer all Questions. In the case that it does not know how to answer a question, it needs to delegate the responsibility.
3. Different questions may have different delegates.
4. A delegate may have a delegate.
5. When delegating a question, an object needs to let a delegate have read-access to its state and the state of anything that delegated to it.
6. A delegate may take a long time to answer a question (don't ask why), so its answer may be cached. This means that the delegate needs to be notified if state, on which it is dependent, changes. This also means that delegates need to be notified on a call to UpdateDataX.

Assume that the tree structure (DAG structure, or whatever structure) of delegates is fixed after it is constructed.

Aucun commentaire:

Enregistrer un commentaire