Choosing a design pattern for a class that can change its internal attributes

I have a class that contains an arbitrary state, and it is defined as follows:

class AbstractFoo
{
};

template <class StatePolicy>
class Foo : public StatePolicy, public AbstractFoo
{
};

A state policy contains only protected attributes that represent state.
The state may be the same for several behaviors, and they can be replaced at run time.
All Foo objects have the same interface for an abstract state and allow you to store Foo objects in containers.
I would like to find the smallest wordiest and most convenient way to express it.

EDIT:
Here is more information about my problem:
Foo is a class that represents the state and behavior of a particular equipment, which can be changed either physically or through the user interface (and there are several user interfaces).
I have four more questions:
1) Will the signal / slot mechanism do?
2) Is it possible to link each emitted signal from a slot in Foo to have a pointer to Foo, as a member class?
3) Should I use a visitor instead and treat Foo as a class visitor?
4) Why is StatePolicy a bad design?

Here's the updated API:

class AbstractFoo
{
public:
  virtual void /*or boost::signal*/ notify() = 0; // Updates the UI.
  virtual void /*or boost::signal*/ updateState() = 0 // Updates the state
};
+3
source share
2

, : , AbstractStatePolicy ? :

class AbstractStatePolicy
{
};

class Foo
{
    AbstractStatePolicy *state_policy;

public:
    Foo(AbstractStatePolicy *state_policy)
        : state_policy(state_policy)
    {
    }
};

, Foo StatePolicy StatePolicy .

state_policy Foo, factory Foo s.

+4

, . , , , , , , . , . , , , ; ( ) ( ), , ( ) boost:: any.

0

Source: https://habr.com/ru/post/1743436/


All Articles