All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.puremvc.java.patterns.observer.Observer Maven / Gradle / Ivy

Go to download

PureMVC is a lightweight framework for creating applications based upon the classic Model-View-Controller design meta-pattern. This is the specific implementation for the Java language. It does not support modular programming since it uses Singletons as Core actors rather than the Multiton used in the MultiCore Version.

The newest version!
//
//  PureMVC Java Standard
//
//  Copyright(c) 2019 Saad Shams 
//  Your reuse is governed by the Creative Commons Attribution 3.0 License
//

package org.puremvc.java.patterns.observer;

import org.puremvc.java.interfaces.INotification;
import org.puremvc.java.interfaces.IObserver;

import java.util.function.Consumer;

/**
 * 

A base IObserver implementation.

* *

An Observer is an object that encapsulates information * about an interested object with a method that should * be called when a particular INotification is broadcast.

* *

In PureMVC, the Observer class assumes these responsibilities:

* *
    *
  • Encapsulate the notification (callback) method of the interested object.
  • *
  • Encapsulate the notification context (this) of the interested object.
  • *
  • Provide methods for setting the notification method and context.
  • *
  • Provide a method for notifying the interested object.
  • *
* * @see org.puremvc.java.core.View View * @see org.puremvc.java.patterns.observer.Notification Notification */ public class Observer implements IObserver { private Object notifyContext; private Consumer notifyMethod; /** *

Constructor.

* *

The notification method on the interested object should take one * parameter of type INotification

* * @param notifyMethod the notification method of the interested object * @param notifyContext the notification context of the interested object */ public Observer(Consumer notifyMethod, Object notifyContext) { this.notifyMethod = notifyMethod; this.notifyContext = notifyContext; } /** *

Compare an object to the notification context.

* * @param object the object to compare * @return boolean indicating if the object and the notification context are * the same */ public boolean compareNotifyContext(Object object) { return object == this.notifyContext; } /** *

Notify the interested object.

* * @param notification the INotification to pass to the interested * object's notification method. */ public void notifyObserver(INotification notification) { notifyMethod.accept(notification); } /** *

Get the notification context.

* * @return the notification context (this) of the * interested object. */ protected Object getNotifyContext() { return notifyContext; } /** *

Set the notification context.

* * @param notifyContext the notification context (this) of the interested object. */ public void setNotifyContext(Object notifyContext) { this.notifyContext = notifyContext; } /** *

Get the notification method.

* * @return the notification (callback) consumer function of the interested object. */ protected Consumer getNotifyMethod() { return notifyMethod; } /** *

Set the notification method.

* *

The notification method should take one parameter of type * INotification.

* * @param notifyMethod the notification (callback) consumer function of the interested object. */ public void setNotifyMethod(Consumer notifyMethod) { this.notifyMethod = notifyMethod; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy