220 likes | 444 Views
Lorenz: Visitor Beans: An Aspect-Oriented Pattern. Aspect-oriented pattern: describes a solution to a tangling problem in a particular context. AOP = control of tangling. Demeter/Java controls the following tangling problems structure/behavior, navigation/behavior, behavior/behavior,
E N D
Lorenz: Visitor Beans:An Aspect-Oriented Pattern • Aspect-oriented pattern: describes a solution to a tangling problem in a particular context.
AOP = control of tangling • Demeter/Java controls the following tangling problems • structure/behavior, • navigation/behavior, • behavior/behavior, • object construction/structure, • synchronization/behavior, • remote invocation/behavior.
Java Bean: a Java class • Provides: methods and events • Requires: listens to events from other beans • pcs.firePropertyChange("n", old, new)
Java Beans • Software Component Model for Java • Introspection: what methods and events are available. Builder tool can inspect a bean. • Support for customization • Events: for communication between beans
Java Beans • Three most important features • set of properties it exposes • set of methods it allows other beans to call • set of events it fires • event listeners can be associated with event source • when the event source detects something, it will call an appropriate method on the event listener object
Java Beans • Design-time versus run-time. A bean must be capable of running in a • design environment: design information, customization (code can be large: wizard style customization) • run-time environment
Clear separation:design-time and run-time aspects • Design-time interfaces can be supported in a separate class from the run-time interfaces • It is possible to deploy a bean at run-time without needing to download all its design time code
Links to remote state • Java bean runs in same address space as their container • Network access mechanisms available: • Java RMI • Java IDL: CORBA • JDBC: Java data base API
Alternate type views • First release: each bean a single Java object • Future releases: beans that are implemented as a set of cooperating objects. • Type view: represents a view of a bean as a given type • In future releases: different type views of a bean may be implemented by different Java objects.
Events Source Listener
Goal • Enable discovery of the events that a particular class may generate • Enable the discovery of the events a class may observe • Common event registration mechanism for dynamic manipulation of relationship between sources and listeners
Design patternsused with a different meaning • The standard design pattern for EventListener registration (multicast event source): • public void add<ListenerType>(<ListenerType> listener); • public void remove <ListenerType>(<ListenerType> listener); • should be synchronized methods
Java Beans preferred format • public PropertyType get<PropertyName>(); // simple getter method • public void set<PropertyName>(PropertyType var); // simple setter method • public PropertyType get<PropertyName>(int index); // indexed getter method • public void set<PropertyName>(int index, PropertyType var); // setter method • public PropertyType[] get<PropertyName>(); // indexed getter method • public void set<PropertyName>(PropertyType[] var); // indexed setter method
Defining events and listeners public class ContainerEventObject extends EventObject {…} public interface ContainerListener extends EventListener { void handleContainerEvent(ContainerEventObject ceo); }
Listeners for Container bean private Vector containerListeners = new Vector(); public synchronized void addContainerListener(ContainerListener l) { containerListeners.addElement(l); } public synchronized void removeContainerListener(ContainerListener l){ containerListeners.removeElement(l); }
Broadcasting event public void broadcastContainerEvent() { Vector l; ContainerEventObject ceo = new ContainerEventObject(this); synchronized(this) { l = (Vector)containerListeners.clone(); } for (int i = 0; i < l.size(); i++) { ContainerListener cl = (ContainerListener)l.elementAt(i); cl.handleContainerEvent(ceo); } }
Visitor Bean • Accept methods: use reflection to define a traversal visitor! • Visit methods: Instead of sending a visitor to visit an element, the element is fired to the visitor as part of an event.
In class Visitor public void visit_dispatch(Object o){ Class[] formal = new Class[1]; formal[0] = o.getClass(); try { Method m = getClass().getMethod(“visit”, formal); Object[] actual = new Object[1]; actual[0] = o; m.invoke(this, actual); } catch (Exception e) { } }
Visit events • Visitors report their actions via events • event objects may contain visited objects • visitors may register as event listeners to other visitors • Events are typed: registration is typesafe
VisitEvent Package visitor.beans.event; import java.util.EventObject; public class VisitEvent extends EventObject { protected VisitEvent(Object source, int type, Object visited, Object[] data) { super(source); fieldType = type; fieldVisited = visited; fieldData = data; } private transient int fieldType = 0; … }
Visitor kinds • Traversal visitors: • manipulate the visitee part and pass data without looking at it • Computational visitors: • pass the visitee untouched and manipulate only the data
Adaptive Bean: visitor style strategy in Local adaptive beans out nodes + edges enter + exit terminate node + edge Traversal status events (contain objects traversed)