I have a question about design. There is class A that owns a map of objects of type Foo. Objects of A create Foo objects and store them in the map, but when a certain indication comes, I want to do some work in class B and create further Foo objects there. A is aware of B and delegates the task to it, but not the other way round. Foo objects created in B still need to go into the map in A.
Possible solutions:
- Return a map of shared_ptrs to created objects from a method in B.
- Pass the map from A to a method in B as a non-const reference.
Somehow, I have a feeling that both of these approaches are bad and class B shouldn't really create objects that it doesn't own. Is there some design pattern that would help in this situation? I was thinking about some factory, but it's just adding a proxy to the problem and I don't really think it helps much. Or maybe I should get rid of class B altogether and have all the logic in A? The problem here, however, is that I don't want A to do too much.
Aucun commentaire:
Enregistrer un commentaire