Presentation is loading. Please wait.

Presentation is loading. Please wait.

Third lecture Review Visitor pattern DemeterJ 2/17/2019 SD.

Similar presentations


Presentation on theme: "Third lecture Review Visitor pattern DemeterJ 2/17/2019 SD."— Presentation transcript:

1 Third lecture Review Visitor pattern DemeterJ 2/17/2019 SD

2 DemeterJ in DemeterJ structure (*.cd) class diagrams structure-shy
compiler/ weaver structure-shy behavior (*.beh) strategies and visitors structure-shy communication (*.ridl) distribution more details on Demeter components and aspects structure-shy object description (*.input, at runtime) synchronization (*.cool) multi threading 2/17/2019 SD

3 Example in DemeterJ class graph in program.cd: A = B. X : B.
B : C | D common E. C = “c”. D = “d”. E = . 2/17/2019 SD

4 Behavior basically in DJ
program.beh: Main { {{ // all Java code public static void main(Sring args[] … { A c = A.parse(“c”); A d = A.parse(“d”); ClassGraph cg = new ClassGraph(true,false); Visitor v = new Visitor() { void before (X host) {System.out.println(“x”);} void before (B host) {System.out.println(“b”);} void before (C host) {System.out.println(“c”);} }; System.out.println(“C-object:”); cg.traverse(c,”from A to C”,v); System.out.println(“D-object:”); cg.traverse(d,”from A to C”,v); } System.out.println(“Done.”); }} // end all Java code } 2/17/2019 SD

5 Output C-object: x b c D-object: Done. 2/17/2019 SD

6 Design Patterns for Collaborative Behavior
Navigate composite object structure Iterator Traversal through a collection Define task-specific behavior Visitor 2/17/2019 SD

7 Visitor - Intent 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. 2/17/2019 SD

8 Visitor - Example 2/17/2019 SD

9 Visitor - Structure 2/17/2019 SD

10 Visitor - Applicability
When an object structure contains many classes of objects with different interfaces, and you want to perform operations on objects that depend on their concrete classes. To avoid cluttering of classes. Visitor lets you keep related operations together. When the classes defining the object structure rarely change. 2/17/2019 SD

11 Visitor - Consequences
It makes it easy to add new operations to an object structure. It gathers related operations and separates unrelated ones. Visitor can visit objects that do not have a common parent class. 2/17/2019 SD

12 Visitor - Consequences
It makes it hard to add new classes in the object structure. 2/17/2019 SD

13 How does DJ help? With Standard Visitor: If k concrete element classes and m abstract element classes involved in traversal. Need k VisitConcreteElement(ConcreteElement) methods in abstract visitor. Need k+m accept methods, one in each element class. DJ: no abstract visit methods, no accept methods. 2/17/2019 SD

14 How does DJ help? DJ makes it easy to change the class structure when visitor pattern is used. DJ provides one universal visitor class. We don’t need a separate abstract visitor class for each behavior. DJ provides accept methods through the strategy. DJ is finer grained: before and after methods. 2/17/2019 SD

15 Visitor Pattern Intent: Modify the behavior of a group of collaborating classes without changing the classes. Examples: Inventory: accumulate list of equipment compute price In the following: Only Java, no Demeter 2/17/2019 SD

16 Visitor Pattern abstract class EquipmentVisitor{
abstract void visitCard(Card e); abstract void visitChassis(Chassis e); ... } 2/17/2019 SD

17 Visitor Pattern class PricingVisitor extends EquipmentVisitor{
private Currency total; void VisitCard(Card e){total.add(e.NetPrice());} void VisitChassis(Chassis e) {total.add(e.DiscountPrice());} } 2/17/2019 SD

18 Visitor Design Pattern
class InventoryVisitor extends EquipmentVisitor { Inventory inventory; void VisitCard(Card e) {inventory.accumulate(e);} void VisitChassis(Chassis e){ inventory.accumulate(e);} } 2/17/2019 SD

19 Visitor class Card extends Equipment { void Accept(EquipmentVisitor v)
{v.VisitCard(this);} } 2/17/2019 SD

20 Visitor class Chassis { void Accept(EquipmentVisitor v){
v.VisitChassis(this); Enumeration e = parts.elements(); while (e.hasMoreElements()) ((Equipment) e.nextElement()). Accept(v); } 2/17/2019 SD

21 Visitor/Applicability
Collaborative tasks Add behavior to composite structure 2/17/2019 SD

22 Visitor AbstractVisitor : ConcreteVisitor1 | ConcreteVisitor2.
VisitConcreteA(ConcreteA); VisitConcreteB(ConcreteB); ConcreteA = … Accept(Visitor v) {v.VisitConcreteA(this); /* further traversal */ } 2/17/2019 SD

23 Visitor/Participants
AbstractVisitor - empty visit operations ConcreteVisitor - task specific visit operation ConcreteElement - accept operation 2/17/2019 SD

24 Visitor Pros Add behavior to composite class structure without cluttering class definitions Cons Explicit traversal, hard-coding structure Auxiliary structure, many empty methods 2/17/2019 SD

25 Visitor hard changes: modify class structure
add new concrete element class: need to update all visitors rename parts easy changes: add a new visitor 2/17/2019 SD

26 Improve Visitor by Selective Visitor Structure-shy Traversal
before, after, around, not just accept no empty methods Structure-shy Traversal strategies 2/17/2019 SD

27 DemeterJ programming Now we focus on the structure and syntax of the DemeterJ programming language. It reuses Java unchanged. Java code between or {{ and }}. Provides syntax to define traversal strategies and visitors and how to glue the three together. 2/17/2019 SD

28 DemeterJ programming for simple use: prepare program.cd, program.beh, program.input call demeterj new (on UNIX and Windows): generates program.prj: be careful: program.prj~ demeterj test demeterj clean if you (and the system) are confused. Regenerates and recompiles. 2/17/2019 SD

29 DemeterJ programming Style for calling Java program: always use a class Main which contains a static public void main function. java -classpath gen/classes :… demjava.jar ... Main < program.input (Windows: /->\ :->; ) demeterj test will do the right thing 2/17/2019 SD

30 DemeterJ programming program.cd
start it with a package name: package X; add package name to program.prj file. Call java X.Main < program.input import classes: import java.util.* terminate the start class with EOF (make sure class is not recursive) See DemeterJ class dictionary for syntax (see DemeterJ AP Studio resource page) 2/17/2019 SD

31 DemeterJ programming can also use look-ahead definitions as in Java Compiler Compiler. See Java class dictionary off DemeterJ resource page. Also Java Compiler Compiler home page. use look-ahead definitions only when absolutely needed. Most of the time you don’t need them. I avoid them. 2/17/2019 SD

32 DemeterJ programming Continue with programming of behavior 2/17/2019
SD

33 End of viewgraphs 2/17/2019 SD


Download ppt "Third lecture Review Visitor pattern DemeterJ 2/17/2019 SD."

Similar presentations


Ads by Google