Observer Mode (OBSERVER)
The Observer pattern is the behavior pattern of the object and is called the model-view mode. This pattern defines a one-to-many dependency that allows multiple observer objects to listen to a role object at the same time. Once the status of the Role object has changed, all observer objects are notified, allowing them to automatically update themselves.
The following is a look at the structure of the observer pattern, as follows:
As you can see from the above figure, the observer pattern involves four roles, as follows:
A, abstract entity role: This role is an abstract role, it holds all references to the Observer object in a single aggregation, each subject object can have a number of observer objects, the subject role is called abstract object of the observer, generally expressed as an abstract class or interface.
At the same time, this role contains a specific management method for the observer and notifies all observer objects when the subject role changes.
B, specific entity roles: This role is specific to the specific role of the entity that implements the abstract. It contains a reference to the status of the entity role, and once the entity role changes, the notification method is called to notify all observer objects.
C, abstract Observer object: This role provides a common interface for all specific observer objects, updating themselves in real time when notified of entity changes, a role typically implemented with abstract classes and interfaces.
D, the specific Observer object: Put the relevant state into the specific observer object, in the specific subject of the internal state changes, to all observers to send notifications. This role is also called the role of the specific observer.
The following are specific examples of the use of the Observer pattern. The example is this: users now want to buy several different styles of the same brand of clothing, if the user has selected different styles of clothing, now need to implement add to the shopping cart and pay; After successful payment, it is now necessary to refresh the user's shopping cart in time so that users can know the results of their payment in time, It is convenient to pay from new payment or select other styles. OK, the specific need to describe, next see the feature detailed class diagram description:
Look directly at the code below! I will list the core code of the Observer pattern below, others please refer to the Project code package I uploaded, as follows:
Abstract entity Role (Cart):
/**
* @description :
* abstract Entity roles-object entities that the observer is interested in
*/
Public abstract class Cart {
Private Vector<salesobserver>Observers = new vector<salesobserver> ();
Public Cart () {
Super ();
}
/**
* @description :
* register an Observer object
*/
Public voidAttach (Salesobserver observer) {
observers. AddElement (observer);
}
/**
* @description :
* Delete an already registered observer object
*/
Public voidDetach (Salesobserver observer) {
observers. Removeelement (observer);
}
Public void detach (intindex) {
observers. Remove (index);
}
/**
* @description :
* notifies all observer objects that have been registered
*/
Public void notifyallbills () {
enumeration<?>Enumes = observers ();
while (enumes. hasmoreelements ()) {
Salesobserverobj = (salesobserver)enumes. nextelement ();
obj. Updatesale (obj);
}
}
/**
* @description :
* after successful payment, update the payment status of all items in the cart
* Note: The update here is local cache data (not via interface update)
*/
Public booleanBillspay () {
Boolean Paystatus = true;
// Asynchronous Network Payment
// TODO
// here the simulation payment succeeds
if (paystatus) {
// update local cache data
Updatelocalcache ();
}
return true;
}
/**
* @description :
* Update Cache
*/
private voidUpdatelocalcache () {
for (Salesobserverobs : observers) {
SaleSale = obs. Getsale ();
Sale. Setpaystatus (" payment complete ");
}
}
Public Enumeration<?> observers () {
return observers. elements ();
}
}
Specific entity roles (ShoppingCart):
/**
* @description :
* specific entity roles-specific notifications for individual observer objects update status
*/
Public class ShoppingCart extends Cart {
Public ShoppingCart () {
Super ();
}
protected void Billpayresult (booleanflag) {
if (! flag) {
// Payment Failure hint (this is not considered here)
return;
}
//Notify obervers
Notifyallbills ();
}
}
Abstract observer Role (OBSERVER):
/**
* @description :
* Abstract Observer-Provides an update method for all specific observers
*/
Public interface observer{
Public Object Updatesale (Objectobj);
}
Specific observer Role (SALESOBSERVER):
/**
* @description :
* specific observer-action to change state
*/
Public class Salesobserver implements Observer {
Private SaleSale = null;
Public Salesobserver (SaleSale) {
this. Sale =sale;
}
@Override
Public Object Updatesale (Objectobj) {
this. Sale = (sale)obj;
return Sale;
}
Public Sale Getsale () {
return Sale;
}
Public void setsale (Sale Sale) {
this. Sale =sale;
}
}
Well, the core code is already attached, and the following is a demonstration of the entire process:
Add Shopping Cart:
To remove a shopping cart:
After payment effect (the same as the previous shopping cart and the removal cart effect is slightly):
Well, to here the Observer mode has been introduced, I hope to help you, in addition, the original work hard-won, reproduced please indicate thank you.
Click I download the code!
Technology Exchange Group:179914858
Viewer mode is used in Android