The observer mode is a commonly used design pattern described in GOF. This article will introduce the use of ASPECTJ to implement the observer mode, and explain its usage by a simple example.
The following is a UML map for AOP implementation of the observer mode
In the AOP version, members of the Subject and Observer interface will be implemented by Abstract ObserverProtocol, and the most important point is abstract PointCut StateChange, which will be defined by different application requirements by specific applications. To achieve the observer mode, you need three basic documents, respectively, Subject.java, Observer.java, ObserverProtocol.java
Interface Subject {// Topic Interface
Public Void AddobServer (Observer Observer);
Public Void RemoveObserver (Observer Observer);
Public Collection getobservers ();
}
Interface Observer {public void update ();} // observer interface
Public Abstract Aspect ObserverProtocol {// Abstract Observer Agreement
Abstract Pointcut Statechange (Subject Subject);
After (Subject Subject): StateChange (Subject) {
Iterator it = Subject.getobservers (). Iterator ();
While (it.hasnext ()) {
Observer Observer = (OBSERVER) it.next;
Observer.Update ();
}
}
Private collection suject.observers = new arraylist ();
Public void subject.addobserver (Observer Observer) {
Observers.Add (Observer);
}
Public void subject.removeobserver (Observer Observer) {
Observers.Remove (OBServer);
}
Public void Subject.getobservers () {Return Observers;
}
The abstract observer agreement in the code is just a simple use of the Inter-Type statement to implement the member function of the Subject interface and is simple, using Observers aggregation, which is also a member of Subject as an abstraction, but it is a private Fields, so you can only access in abstract aspects, and Subject itself does not acquire this variable. After the AFTER implements the notification mechanism in the observer mode, it notifies all registered observer objects after the status of the Subject, and makes it updated UPDATE.
The above three files have implemented the observer mode, relative to different applications, which requires different specific aspects (inheriting from OBSERVERPROTOCOL abstraction). To give a simple example, implement a frame and add a button and a Label. When you click Button, the current time is displayed in the Label.
Button.java
Class Button Extends JButton {
Public Button (String Label) {Super (Label); addActionListener (new actionListener () {public void actionPerformed (ActionEvent E) {Button.Thisclick (); // Call the click method of the Button class}});} public void Click () {}} TimeLabel.java
Class Timelabel Extends Jlabel {Public Timelabel (String Str) {Super (STR);} public void showtime () {this.setText (new date (). Tostring ());}}
Specific aspect OBServerProtocolimpl.java
Public Aspect ObserverProtocoliMPL Extends ObserverProtocol {Declare PARENTS: Button Implements Subject; Declare Parents: TimeLabel Implements Observer;
Public void Timelabel.Update () {showtime ();
Pointcut STATECHANGES (SUBJECT S): Target (s) && call (void button.click ());}
The interface is Subject, Observer, defined for Button and Timelabel, respectively, in specific aspects. Such the Button class is actually the subject in the observer mode, while Timelabel is an UPDATE method for the viewer and implements its interface to respond to the Button's corresponding method to display the current time. The specific behavior of STATECHANGE is defined in this aspect, which will capture the call of the Button.Click method.
Demo.java
Public class demo {public static void main (string [] args) {
JFRAME FRAME = New JFrame ("Show Time"); "Show Time"); Timelabel Label = New Timelabel (New Date (). TOSTRING ()); button.addobserver (Label); // Add theme button observer label frame.getContentPane () setLayout (new FlowLayout ());.. frame.getContentPane () add (button);.. frame.getContentPane () add (label); frame.setDefaultLookAndFeelDecorated (true) Frame.setDefaultcloseOperation (jframe.exit_on_close); frame.pack (); frame.setvisible (true);}}
Operation example, compile using AJC -ARGFILE OBSERVER.LST in the command line
Then use Java Demo to run an example
Every time you click the Show Time button, it will cause time updates.
If you want to know more about AspectJ and AOP, you can visit http://www.eclipse.org/aspectj/ and www.jboss.org to download Aspectj or JBoss-AOP tools.
statement