[Design Mode] - Mode listener and observer mode differences and relations

Preface: listener and observer mode in the normal mode or in development often encountered in the Spring source code, - it seems similar, but why will be divided into two different design patterns? What is twisted or moral evil of humanity? Let us into the manual carefully depth analysis of both the similarities and differences and usage scenarios.


table of Contents

 

1 Introduction

1.1 Introduction listener mode

1.2 Introduction observer mode

2. similarities

3. The difference at

4. Scene


1 Introduction

1.1 Introduction listener mode

When the range of the state of the object changes, the server automatically listener object method invocation.

https://blog.csdn.net/lovexiaotaozi/article/details/102579880

1.2 Introduction observer mode

Defines dependencies between objects of many, so that, when an object changes state, its dependents will be notified and updated automatically.

See this: [Design Mode] -09 observer mode

https://blog.csdn.net/lovexiaotaozi/article/details/85161368

Further reading: difference on observer mode and subscriber model, know almost this summary very good -> https://zhuanlan.zhihu.com/p/51357583

2. similarities

Mode listener and observer mode can be achieved when something changes state, trigger follow-up to do, are more similar in function and implementation, and have done a couple of low, low dependence, so that the code elegance.

3. The difference at

For the differences, let's look at listener and observer mode model class diagram:

Listener mode:

Observer Pattern:

Event Source listener mode corresponding viewer mode is a viewer, listener in the listener mode corresponding to the observer observer implementation class, in addition to the multi-mode listener event objects than the observer pattern different event object is the root of two design patterns, I have introduced in listener mode essay, different listener mode vector is introduced into an event object, to act as the event can be more flexible to implement event trigger different listeners perform different logic compared to observer mode for the scene mode listener wider, but the implementation is slightly more complicated than the observer mode.

4. Scene

The observer pattern applicable to similar publish and subscribe to this scenario, when a publishing content, will be released to push content to subscribers without the need to distinguish between the type of event, be treated differently.

Listener mode is suitable for the type of event and need more than one kinds of discrimination scene, such as listening boss's instructions, the boss may have a variety of instructions, instructions issued under the program to owner overtime ape, ape procedures have to work overtime, the employer issued instructions to deliver the program ape, ape procedures have to deliver ...

The above is my little understanding of the listener and observer mode mode, if any omission or error message treatise place thanks!

Published 89 original articles · won praise 69 · views 40000 +

Guess you like

Origin blog.csdn.net/lovexiaotaozi/article/details/102579360