The Observer pattern defines a one-to-many dependency between objects, allowing one or more observer objects to observe a Subject object. When the state of the subject object changes, the system's all-in-one notification relies on this object observer object, allowing the Observer object to be automatically updated.
In the observer pattern, the observed object tasting is called the target or subject (Subject), and the dependent object is called the Observer (Observer).
Interface:observer
Package Edu.pri.lime._9_3_9.bean; // Observer interface, which should be implemented by all observers in the program Public Interface Observer {// Observable: Represents the object being observed, that is, the target or topic /// Observable: is an abstract base class , the abstract base class should be inherited by the observer in the program. // arg: void update (Observable o,object arg);}
Class:observable
PackageEdu.pri.lime._9_3_9.bean;Importjava.util.ArrayList;Importjava.util.List;/** The abstract class is the base class for all the observers (subject), * It provides a registobserver () method for registering a new observer; * and provides a removeobserver () method for deleting a registered observer; When the state of the observed object (subject) changes, the object (subject) is called by the Notifyobservers () method to notify all observers. */ Public Abstract classObservable {//use a list to save all the bound event listeners on the objectList<observer> observers =NewArraylist<observer>();//used to register the observer from this topic Public voidregistobserver (Observer o) {observers.add (o); }//used to register the observer from this topic Public voidremoveobserver (Observer o) {observers.remove (o); }//notifies all observers registered on the topic Public voidnotifyobservers (Object value) {//Traverse all the observers registered to the topic for(Observer o:observers) {//explicitly invoking the update () method for each observerO.update ( This, value); } }}
Class:product
PackageEdu.pri.lime._9_3_9.bean; Public classProductextendsObservable {PrivateString name; Private DoublePrice ; PublicProduct () {Super(); //TODO auto-generated Constructor stub } PublicProduct (String name,DoublePrice ) { Super(); This. Name =name; This. Price =Price ; }//when the program calls the setter method of name to modify the name member variable of product,//The program naturally triggers all the observers registered on the object Public voidsetName (String name) { This. Name =name; Notifyobservers (name); }//when the program calls the price setter method to modify the product's price member variable, the//The program naturally triggers all the observers registered on the object Public voidSetprice (DoublePrice ) { This. Price =Price ; Notifyobservers (price); } PublicString GetName () {returnname; } Public DoubleGetPrice () {returnPrice ; } }
Class:nameobserver
PackageEdu.pri.lime._9_3_9.bean;ImportJavax.swing.JFrame;ImportJavax.swing.JLabel; Public classNameobserverImplementsObserver {//implementing an Update method that the observer must implement Public voidUpdate (Observable o, Object Arg) {if(ARGinstanceofString) {String name=(String) arg; JFrame F=NewJFrame ("Viewer"); JLabel L=NewJLabel ("name changed to:" +name); F.add (l); F.pack (); F.setvisible (true); System.out.println ("Name Viewer:" + O + "item name changed to:" +name); } }}
Class:priceobserver
Package Edu.pri.lime._9_3_9.bean; Public class Implements Observer {// Implement the update () method that the observer must implement public void Update (Observable o, Object Arg) { ifinstanceof Double) { System.out.println ("Price Observer:" + O + "Item price has been changed to:" + arg); }}
Class:test
PackageEdu.pri.lime._9_3_9.main;ImportEdu.pri.lime._9_3_9.bean. Nameobserver;ImportEdu.pri.lime._9_3_9.bean. Priceobserver;ImportEdu.pri.lime._9_3_9.bean. Product; Public classTest { Public Static voidMain (string[] args)throwsinterruptedexception{//Create a Theme objectProduct Pro =NewProduct ("Table", 300);//Create two Observer objectsNameobserver nameobs =NewNameobserver (); Priceobserver Priobs=Newpriceobserver ();//registering two observer objects on a Subject objectPro.registobserver (nameobs); Pro.registobserver (priobs);//program calls setter method to change product's name and price member variablesPro.setname ("Advanced desk"); Thread.Sleep (3000); Pro.setprice (321D); }}
gof--Observer pattern