"Design mode Design Pattern" reading notes 7

xiaoxiao2021-03-06  46

"Design mode Design Pattern" reading notes 7

Observer mode

structure

purpose

In a one-to-MANY, when an object (one) is updated, all of its related objects (MANY) will be notified.

discuss

1, it may bring a large state update cost, and a small status update of Subject may make a lot of OBServer to update. More serious is that if the Subject does not provide this state is updated, Observer may be forced to find out what is updated.

2. If an OBServer may correspond to multiple Subject, a parameter can be provided in the UPDATE interface, which is incoming the address of the Subject, which makes the OBServer know that the Subject is updated.

3. For when to trigger notify, there are two options:

A. Whenever the STATE of the Subject is updated, NOTIFY is automatically triggered. Disadvantages are that it may bring constant updates overhead.

B. The client is responsible for triggering notify so that the client can trigger Notify after setting a series of State, avoid too much update overhead. The disadvantage is that the Client may forget to do Notify.

4, Subject When updating Observer via Update, there is a trade-off:

A. Extremely, Subject provides a sufficiently detailed intelligence, which is a Push Model. The disadvantage is that the reuse of Observer is reduced because Subject has a hypothesis for Observer.

B. Another extreme, Subject does not provide any information, this is a Pull Model. The disadvantage is that Observer needs a lot of effort to find out what is updated.

5, can make an extension, Observer is at ATTACH, provide a parameter for Subject, telling the Subject it is only interested in what it is updated; this, Subject is updated in an EVENT Only Update is the OBServer interested in the event, and the update also has the aspect parameter, tells Observer what Event is updated.

6. If the relationship between Subject and Observer is too complicated, you can use Mediator Mode to create a ChangeManager to coordinate the relationship between them.

Reference

"Design Pattern" Gang-Of-4 1997

转载请注明原文地址:https://www.9cbs.com/read-40789.html

New Post(0)