More about Aspects - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

More about Aspects

Description:

{ drawTextBox('Greetings!'); InformationScreen. import java.util.Date; ... Design pattern. implementation in Java and AspectJ. OOPSLA 2002. (find pdf on citeseer.com) ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 27
Provided by: web2Com
Category:
Tags: aspects | com | greetings | more

less

Transcript and Presenter's Notes

Title: More about Aspects


1
More about Aspects
2
How it works
base program
aspect
compiler
adds new declarations to base program's classes
woven base program
includes a class for the aspect code
3
Visitor Design Pattern
Represent an operation to be performed on the
elements of an object structure. Visitor lets you
define a new operation without changing the
classes of the elements on which it operates.
participants Visitor (NodeVisitor) Concrete
Visitor (TypeCheckingVisitor) Element
(Node) ConcreteElement (AssignmentNode,VarNode
) ObjectStructure (Program)
4
Visitor Protocol (1)
public abstract aspect VisitorProtocol //
Element role public interface VisitorNode
// Concrete element role for nonterminal
nodes protected interface VRegularNode extends
VisitorNode // Concrete element role for
terminal nodes protected interface VLeaf extends
VisitorNode
5
Visitor Protocol continued
// implemented by ConcreteVisitor public
interface NodeVisitor public void
visitRegularNode(VisitorNode node) public void
visitLeaf(VisitorNode node) public String
report()
6
Visitor Protocol (3)
// default implementation for accept
method public void VisitorNode.accept(NodeVisitor
nv) // accept on a nonterminal node public
void VRegularNode.accept(NodeVisitor nv)
nv.visitRegularNode(this) // accept on a
terminal node public void Vleaf.accept(NodeVisito
r nv) nv.visitLeaf(this) // end of
VisitorProtocol aspect
introduce on all classes with the relevant
interface
7
Application to binary trees
Node interface for node classes Leaf
implements Node leaves of tree RegularNode
implements Node internal nodes
public aspect MyVisitor extends VisitorProtocol
declare parents Node implements
VisitorNode declare parents RegularNode
implements VRegularNode declare parents
Leaf implements VLeaf
8
SumVisitor (1)
public class SumVisitor implements
VisitorProtocol.NodeVisitor protected int sum
0 public void visitRegularNode(VisitorProtoco
l.VisitorNode node) if (node
instanceof RegularNode) RegularNode rnode
(RegularNode) node rnode.left.accept(thi
s) rnode.right.accept(this)
9
SumVisitor (2)
public void visitLeaf(VisitorProtocol.VisitorNode
node) if (node instanceof Leaf)
Leaf leaf (Leaf) node sum
leaf.getValue() public String
report() return "gtgtgt SumVisitor collected a
sum of "sum
10
TraversalVisitor (1)
public class TraversalVisitor implements
VisitorProtocol.NodeVisitor protected
String result "" public void
visitRegularNode(VisitorProtocol.VisitorNode
node) if (node instanceof
RegularNode) RegularNode rnode
(RegularNode) node result
"" rnode.left.accept(this)
result ","
rnode.right.accept(this) result
""
11
TraversalVisitor (2)
public void visitLeaf(VisitorProtoco
l.VisitorNode node) if (node
instanceof Leaf) Leaf leaf
(Leaf) node result
leaf.value
public String report() return
"gtgtgt TraversalVisitor traversed the tree to
" result
12
Composite Pattern
Compose objects into tree structures to
represent part-whole hierarchies. Composite lets
clients treat individual objects and compositions
of objects uniformly.
Component Operation() Add(Component)
Remove(Component) getChild(int)
children
Composite Add(Component) Remove(Component)
GetChild(int)
Leaf Operation()
forall g in children g.Operation()
13
CompositionProtocol
public abstract aspect CompositionProtocol
protected interface Component protected
interface Composite extends Component
protected interface Leaf extends Component
private WeakHashMap perComponentChildren
new WeakHashMap() private Vector
getChildren(Component s) Vector
children children (Vector)
perComponentChildren.get(s) if (children
null) children new Vector() perComponen
tChildren.put(s,children) return
children private Iterator
getAllChildren(Component c) return
getChildren(c).iterator() ...
14
FileSystemComposite (1)
public aspect FileSystemComposite extends
CompositeProtocol declare parents Directory
implements Composite declare parents
File implements Leaf private abstract int
Component.sizeOnDisk() private int
File.sizeOnDisk() return size
15
CompositionProtocol
retrieve singleton instance of aspect class
private int Directory.sizeOnDisk() int
diskSize 0 for (Iterator it
CompositeProtocol.aspectOf(). getAllChil
dren(this) it.hasNext() ) diskSize
((Component) it.next()).sizeOnDisk() return
diskSize // end of aspect
16
Bridge Pattern
Decouple an abstraction from its implementation
so that the two can vary independently.
Have separate hierarchies for Abstractions (here
Screens) and Implementors (here
ScreenImplementation), so that both can change
independently of each other Participants
Screen Abstract Abstraction that defines an
interface for printing text and boxedText
to stdout. GreetingScreen RefinedAbstraction
that prints a boxed greeting message
InformationScreen RefinedAbstraction that
prints the system time (boxed)
ScreenImplementation Implementor interface,
defines basic operations to output
formatted strings StarImplementation
ConcreteImplementation that creates textBoxes
of stars CrossCapitalImplementation
ConcreteImplementation that creates textBoxes
of double crosses (hashes) and prints all
text capitalized
17
Screen
public interface Screen public void
drawText(String text) public void
drawTextBox(String text)
Note Screen is not an abstract class!
18
ScreenImplementation
public interface ScreenImplementation
void printLine() void printDecor()
void printText(String text)
19
AbstractionImplementation
public aspect AbstractionImplementation
private ScreenImplementation Screen.implementor
public void Screen.drawText(String text)
implementor.printText(text)
implementor.printLine() public void
Screen.setImplementor(ScreenImplementation
implementor) this.implementor
implementor
20
AbstractionImplementation (2)
public void Screen.drawTextBox(String text)
int length text.length() for(int
i0 iltlength4 i)
implementor.printDecor()
implementor.printLine()
implementor.printDecor()
implementor.printText(" "text" ")
implementor.printDecor()
implementor.printLine() for(int i0
iltlength4 i) implementor.printDe
cor() implementor.printLine()
// end of aspect
21
GreetingScreen
public class GreetingScreen implements Screen
public GreetingScreen(ScreenImplementation si)
setImplementor(si) public
void drawGreeting() drawTextBox("Greetin
gs!")
22
InformationScreen
import java.util.Date public class
InformationScreen implements Screen public
InformationScreen(ScreenImplementation si)
setImplementor(si) public void
drawInfo() Date date new Date()
drawTextBox("Current system time "date)

23
StarImplementation
public class StarImplementation implements
ScreenImplementation public void
printLine() System.out.println()
public void printDecor()
System.out.print("")
public void printText(String text)
System.out.print(text)
24
CrossCapitalImplementation
public class CrossCapitalImplementation
implements ScreenImplementation public
void printLine() System.out.println(
) public void printDecor()
System.out.print("X")
public void printText(String text)
System.out.print(text.toUpperCase())
25
Subtle difference with Java
public class Screen private
ScreenImplementation implementor public
Screen(ScreenImplementation implementor)
this.implementor implementor
public void drawText(String text)
implementor.printText(text)
implementor.printLine() ...
a class instead of an interface GreetingScreen,
InformationScreen must extend this class
26
Reading
Gamma, Helm, Johnson and Vlissides Design
Patterns. Addison-Wesley 1995.
Jan Hannemann and Gregor Kiczales. Design
pattern implementation in Java and AspectJ.
OOPSLA 2002. (find pdf on citeseer.com)
Write a Comment
User Comments (0)
About PowerShow.com