dimanche 21 janvier 2018

How to notify classes higher in the hierarchy for changes?

Lets assume I have two classes based on composition:

Class A which holds a reference to Class B.

Now B shouldnt know anything about his A but needs to notify A to make an update on certain events. What needs to be updated is an object that A holds. Im wondering how to solve this in a clean way.

Solution one: A observes if event occures on B and makes update on the object.

Solution two: A injects the object in the constructor of B. The latter handles the update.

Solution three: Adapter class where B is a registered publisher and A the subscriber that receives the messages of the publisher.

Which of these do you think is the better solution? Is there a better way which I havent mentioned yet?

Aucun commentaire:

Enregistrer un commentaire