Pergunta

The idea is that I have a set of properties that represent possible options in the app. These options can be modified through a variety of different ways, such as from the UI, remotely, third party, etc...

Each time one of these modifiers alters the state, the other modifiers need to be notified. E.g.: If the user marks a checkbox in the UI, the remote modifier and the third party need to be notified, so that they update their internal state.

There is no priority involved, and the state changes cannot be rejected. The only concern is to prevent the third party from receiving a notification when itself modifies the state.

Is there a good design pattern to solve these requirments?

Foi útil?

Solução

The question is a bit vague (e.g. what exactly do you mean by a third party?), but to try and answer anyway:

I think you're looking for the Observer pattern. The intent of the pattern is to allow entities to be notified when the state of some object changes, while decoupling the two sides. I.e., the observers don't know anything about the subject except that it is observable, and the subject knows nothing about the observers except that they can be notified.

You can register all the modifiers as observers to your app (whatever part of it makes the most sense and is relevant to your situation: the GUI, the Model facade if you have one, etc...), and then you can send them a notification whenever the app's state changes, while keeping all of them decoupled and keeping things flexible.

Edit: If you're using the MVC pattern, then the Model class (more accurately the facade class to the backend system) is probably your Subject that the observers will listen to, because it stores the state of the app and can notify the observers when anything changes.

Licenciado em: CC-BY-SA com atribuição
scroll top