1

This is a followup to this question.

Basically I want a container that stores objects and later does something with them. I want to put both, the action performed on the objects (ActionPolicy), and the storage (StoragePolicy), into policy classes. At the end, there should be two functions on the class:

  • addObject() with a signature depending on ActionPolicy, i.e. this function should be defined in there.
  • execute(), which goes over all of the objects stored by StoragePolicy and executes ActionPolicy::evaluate(obj) on all of them.

In (partially pseudo-)code (the places marked with Here are the ones that don't work in this design):

struct ActionPolicy {
  // Signature is dependant on this policy
  void addObject(T obj, /* ... */) {
    // Do something with the object
    StoragePolicy::store(obj); // <--- Here
  }

  void eval(T obj) {
    // Do something with the object
  }
};

struct StoragePolicySingle {
  T obj;

  void store(T obj) {
    this->obj = obj;
  }

  void execute() {
    ActionPolicy::execute(obj); // <--- Here
  }
};


struct StoragePolicyMulti {
  std::vector<T> vec;

  void store(T obj) {
    vec.push_back(obj´);
  }

  void execute() {
    for (obj in vec) {
      ActionPolicy::execute(obj); // <--- Here
    }
  }
};

template <class A, class B> MyClass : public A, public B {
  // ...
};

All of this is performance-critical, so I can't just use a vector with one entry instead of StoragePolicySingle.

How would you solve this? Any pattern I'm missing?

Community
  • 1
  • 1
lucas clemente
  • 6,255
  • 8
  • 41
  • 61
  • 1
    If `execute` is one-by-one, then you make the main class dispatch the call (`exectute(data_begin(), data_end());`). If `exectute` has to know about the entire range, then make the action policy inherit from the storage policy. – Kerrek SB Dec 01 '11 at 00:18
  • Are the things `ActionPolicy` does with an object in `AddObject` and `eval` dependent on each other, or some shared state? If not, maybe separating into an `AddPolicy` and an `EvalPolicy` will help clarify? – tzaman Dec 01 '11 at 01:17

1 Answers1

0

Why does ActionPolicy need to know about StoragePolicy?

Why does an ActionPolicy have objects added to it?

If you pass the ActionPolicy to the StoragePolicy as an argument to execute, then call eval on either the single item or the collection, does this not solve it for you?

Doug Moscrop
  • 4,479
  • 3
  • 26
  • 46
  • As I wrote `ActionPolicy` does some things with the object before it can be stored (it does not store the object). Also the signature of the `addObject` function should depend on which `ActionPolicy` one chooses. Do you mean passing `ActionPolicy` as template parameter? I thought about that, but somehow this doesn't look like the best pattern… – lucas clemente Dec 01 '11 at 01:08
  • What about a seperate ActionPolicy member of the StoragePolicy, eval called pre-store. At least then the ActionPolicy exists solely to act on an object, and the storage policy stores an object. Then pass ActionPolicy to execute on the Storage, which would be a different action. To paraphrase, why does a single action policy need to know about the 'addObject' logic *and* the 'eval' logic? Why can't those be two separate things? – Doug Moscrop Dec 01 '11 at 01:19