Cocos observer design pattern and notification mechanism, cocos observer

Source: Internet
Author: User
Tags notification center

Cocos observer design pattern and notification mechanism, cocos observer
The Observer (Observer) mode is also called the Publish/Subscribe mode. It is an important part of the MVC (Model-View-Controller) mode.
Weather has always been a topic that British people like to discuss, and changes in weather have become a topic of great interest to Chinese people in recent years. I will decide whether to take the subway or drive to work based on the weather forecast. My daughter will also decide which clothes to wear tomorrow Based on the weather forecast. So I customized the weather forecast Text Message notification service for my mobile phone at the mobile company, and its working model.



Every day, the Meteorological Bureau sends the weather forecast information to the mobile operator. The text message center of the mobile operator is responsible for sending the weather forecast to the mobile phone that has customized the service.
In a software system, changing the state of an object may also affect the state of many other objects. There are many design solutions that can meet this requirement, but they can achieve high reusability and anonymous communication between objects. The observer mode is the most suitable one.
Implementation Principle



It has four roles, as shown below.
Abstract topic (Subject ). Abstract topic is an interface that is a collection of observer containers. It defines the function for adding the observer (add) function, removing the observer (remove) function, and sending notifications to all observers (notifyObserver ).
Abstract Observer (Observer ). An abstract observer is an interface that has an update function.
ConcreteObserver ). The specific implementation of the Observer interface.
ConcreteSubject ). The specific implementation of the Subject interface.
The introduction of the Subject and Observer interfaces not only improves the reusability of the system, but also reduces the coupling degree. Because ConcreteSubject only needs one instance, we use the single-sample design mode. The observer mode can also have other variants. For more information, see GoF.
Notification Mechanism
In the Cocos2d-x framework, the observer pattern has a notification mechanism for specific applications. The notification mechanism differs from the delegate mechanism in that the former is the communication between "one-to-many" objects, and the latter is the communication between "one-to-one" objects.
All objects that interest a notification in the notification mechanism can become recipients. First, these objects need to be registered with the notification center (_ icationicationcenter) by calling the addObserver function. when an object is delivered to the notification center, the notification center broadcasts the notification to the registered receiver. All recipients do not know who delivered the notification, and do not care about its details. The relationship between the delivered object and the recipient is one-to-multiple. If the recipient no longer pays attention to the notification, the notification center will call the removeObserver or removeAllObservers function to cancel registration and will not receive the notification later.

In terms of parameter return, the notification mechanism is much better than the delegate mode. The notification mechanism can implement one-to-many parameter transmission, while the delegation mode is only one-to-one parameter transmission.



More content please pay attention to the first domestic Cocos2d-x 3.2 version of the book "Cocos2d-x practice: C ++ volume" book exchange discussion site: http://www.cOcoagame.net
For more exciting video courses, please follow the Cocos course in Zhijie class: http: // v.51wOrk6.com
Welcome to the Cocos2d-x Technology Discussion Group: 257760386 welcome to the knowledge of the iOS classroom public platform


JAVA asynchronous observer mode source code (complete), do not synchronize, good to add points

Package TestObserver;

Import java. util. Iterator;
Import java. util. Vector;

/**
*
* @ Author Seastar
*/
Interface Observed {

Public void addObserver (Observer o );

Public void removeObserver (Observer o );

Public void update ();
}

Interface Observer {

Public void takeAction ();
}

Class Invoker {

Private Observer o;
Handler handler;

Public Invoker (Observer o ){
New Handler ();
This. o = o;
}

Private class Handler extends Thread {

Public Handler (){
Handler = this;
}

@ Override
Public void run (){
O. takeAction ();
}
}

Public boolean TestSameObserver (Observer o ){
Return o = this. o;
}

Public void invoke (){
Handler. start ();
}
}

Class ObservedObject implements Observed {

Private Vector <Invoker> observerList = new Vector <Invoker> ();

Public void addObserver (Observer o ){
ObserverList. add (new Invoker (o ));
}

Public void removeObserver (Observer o ){
Iterator <Invoker> it = observerList. iterator ();
While (it. hasNext ()){
Invoker I = it. next ();
If (I. TestSameObserver (o )){
ObserverList. remove (I );
Break;
}
}
}

Public void update (){
For (Invoker I: observerList ){
I. invoke ();
}
}
}

Class ObserverA implements Observer {

Public void takeAction () ...... remaining full text>

Simple design patterns ····

What is in WF only? Workflow?

The observer mode is the relationship between the subject object and the observer object 0. n,

This relationship is very common in the design of domain models, such as the relationship between teachers and students. Whether or not to add a notification mechanism depends on the business requirements.

Related Article

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.