The observer pattern of the design pattern

Source: Internet
Author: User
Observer pattern

definition: defines a one-to-many dependency between objects so that when each object changes state, all objects that depend on it are notified and updated automatically.

Class Diagram:

Several [observers] observe the [observed] state, and when the [observed] state changes, the [observer's] own state changes as well.

The Observer has three methods: The Attach method is to add the Observer object to the container, the detach method is to remove the observer object from the container, and the Notify method is to call the corresponding method of the Observer object in turn.

The observer pattern is a common trigger mechanism that forms a trigger chain and then processes each observer's method in turn.

One advantage here is that the observer does not need to know what the observer is and how many observers it has, but notifies the observer to subscribe to its own state when it is changed.

Add: The difference between a vector and a list

The vector is equivalent to the sequential table in the data structure, the physical address is continuous, the query speed is fast, can be directly subscript index, but when the insertion of data is deleted, the back of all the data to move, less efficient

List is a doubly linked list, the physical address is not contiguous, must be queried from the beginning or the tail, slow, but can dynamically insert delete data

The observer pattern of the design pattern

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.