Design Pattern 20 -- Observer pattern of behavior pattern

Source: Internet
Author: User

Definition: ObserverPattern defines a one-to-many dependency between objects. When the status of an object changes, all objects dependent on it are notified and automatically updated. Type: object behavior pattern overview: The Observer pattern is also hard to understand. The observer pattern is very similar to the intermediary pattern when you look at the class graph. If you do not really understand the differences between the two, it is difficult to really understand the observer model. If you do not understand the observer mode, you are more likely to use the observer mode. The mediation mode mainly solves the coupling problem of a series of interactive objects, and puts forward the intermediary class. Several previous interaction objects are now integrated through intermediary classes for communication. The observer mode focuses on the one-to-many notification function, that is, when something happens, the topic object will send a notification message to all registered observer objects. The mediation mode focuses on communication through intermediary classes, while the observer mode focuses on one-to-many notifications. In the mediation mode, you do not need to know the specific objects of the intermediary class. You only need to know the interface of the notification intermediary object for the interaction object. Which intermediary is used to complete the task of notifying the interaction object, you do not need to know the interaction object. However, the interaction object must know what interaction objects exist so that object A can notify object B of what object A needs. In observer mode, the Subject object does not need to know who the specific observer is. Instead, it only needs to know the notification interface of the observer to notify the observer of the message. As for the specific messages, the observer needs to view them in the specific Subject. For example, an e-commerce company has a large sales promotion and sends a promotion message to the consumer via email. However, if you want to know the specific promotion information, you must go to the e-commerce website to view the specific promotion information. Class diagram: Participant: Vender, which provides an interface to complete the registration notification to the observer. YiXun, a specific e-commerce business, provides consumers with the specific information they need to view. Observer, which provides the Update interface. CustomerA and CustomerB, specific observer, view their favorite products in e-commerce when there is an update. Sample Code: copy the code using System; using System. collections. generic; using System. text; namespace Pattern21 {// abstract State public abstract class State {public abstract void Eat (); public abstract void Walk () ;}// State public class Happy when Happy: state {public override void Eat () {human. eat (); Console. writeLine ("a lot! ");} Public override void Walk () {human. Walk (); Console. WriteLine (" dancing! ");} Public void Attach (Human _ human) {human = _ human;} private Human human;} // public class Sad: state {public override void Eat () {human. eat (); Console. writeLine ("very few! ");} Public override void Walk () {human. Walk (); Console. WriteLine (" budget-consuming! ");} Public void Attach (Human _ human) {human = _ human;} private Human human;} // a person public class Human {private State current; public void SetState (State s) {current = s;} public void Eat () {Console. write ("eat");} public void Walk () {Console. write ("START");} public void Show () {current. eat (); current. walk () ;}} class Program {static void Main (string [] args) {// defines a Human huma object with many States N = new Human (); // defines a Happy state Happy hState = new Happy (); hState. attach (human); human. setState (hState); human. show (); // defines a Sad state, sad Sad = new sad (); Sad. attach (human); human. setState (sad); human. show (); // You can also add a sick state. You only need to add a new class without modifying the Human class //...... console. read () ;}} replication code Applicability: A group of objects communicate in a well-defined but complex manner. The resulting dependency structure is confusing and hard to understand. An object references many other objects and communicates directly with these objects, making it difficult to reuse the object. We want to customize a behavior distributed in multiple classes without generating too many child classes. Advantages and disadvantages: it reduces the coupling between the command initiator and the command receiver, and the publisher notifies all observers through the interface. Disadvantage: The specific observer depends on the specific publisher.

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.