RSS feed
[root]
/
document
/
software design
/
Design pattern example in aspect programming
/
library
/
pattern
/
spl
/
cs
/
ubc
/
ca
login:
password:
title search:
Search this site
Enter your search terms
Web
www.carfield.com.hk
Submit search form
Prev
Next
Wed Mar 24 16:00:00 GMT 2004
ObserverProtocol
package ca.ubc.cs.spl.pattern.library; /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- * * This file is part of the design patterns project at UBC * * The contents of this file are subject to the Mozilla Public License * Version 1.1 (the "License"); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/. * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is ca.ubc.cs.spl.patterns. * * Contributor(s): */ import java.util.WeakHashMap; import java.util.List; import java.util.LinkedList; import java.util.Iterator; /** * Defines the general behavior of the observer design pattern. * * Each concrete sub-aspect of ObserverProtocol defines one kind of observing * relationship. Within that kind of relationship, there can be any number * of subjects, each with any number of observers. * * The sub-aspect defines three things: <ol> * * <li> what types can be subjects or observers <br> * this is done using +implements * * <li> what operations on the subject require updating the observers <br> * this is done by concretizing the changes(Subject) pointcut * * <li> how to update the observers <br> * this is done by defining a method on * updateObserver(Subject, Observer) * </ol> * * Note that in this implementation, the work of updating is a method * on the sub-aspect, not a method introduced on the observer. This * allows one class of object to be the observer in different kinds of * observing relationships, each of which has a different updating * behavior. For observers that just have a single generic update * behavior, the method on updateObserver will just be a simple call * that generic updater. * * @author Gregor Kiczales * @author Jan Hannemann * @version 1.0, 05/13/02 * */ public abstract aspect ObserverProtocol { /** * This interface is used by extending aspects to say what types * can be subjects. It models the subject role. */ protected interface Subject { } /** * This interface is used by extending aspects to say what types * can be observers. It models the observer role. */ protected interface Observer { } /** * Stores the mapping between <code>Subject</code>s and <code> * Observer</code>s. For each subject, a <code>LinkedList</code> * is of its observers is stored. */ private WeakHashMap perSubjectObservers; /** * Returns a <code>Collection</code> of the observers of * a particular subject. Used internally. * * @param s the subject for which to return the observers * @return a <code>Collection</code> of s's observers */ protected List getObservers(Subject s) { if (perSubjectObservers == null) { perSubjectObservers = new WeakHashMap(); } List observers = (List)perSubjectObservers.get(s); if ( observers == null ) { observers = new LinkedList(); perSubjectObservers.put(s, observers); } return observers; } /** * Adds an observer to a subject. This is the equivalent of <i> * attach()</i>, but is a method on the pattern aspect, not the subject. * * @param s the subject to attach a new observer to * @param o the new observer to attach */ public void addObserver(Subject s, Observer o) { getObservers(s).add(o); } /** * Removes an observer from a subject. This is the equivalent of <i> * detach()</i>, but is a method on the pattern aspect, not the subject. * * @param s the subject to remove the observer from * @param o the observer to remove */ public void removeObserver(Subject s, Observer o) { getObservers(s).remove(o); } /** * The join points after which to do the update. * It replaces the normally scattered calls to <i>notify()</i>. To be * concretized by sub-aspects. */ protected abstract pointcut subjectChange(Subject s); /** * Call updateObserver after a change of interest to update each observer. * * @param s the subject on which the change occured */ after(Subject s): subjectChange(s) { Iterator iter = getObservers(s).iterator(); while ( iter.hasNext() ) { updateObserver(s, ((Observer)iter.next())); } } /** * Defines how each <code>Observer</code> is to be updated when a change * to a <code>Subject</code> occurs. To be concretized by sub-aspects. * * @param s the subject on which a change of interest occured * @param o the observer to be notifed of the change */ protected abstract void updateObserver(Subject s, Observer o); }
(google search)
(amazon search)
1
2
3
second
download zip of files only
Prev
Next