Third lecture Review Visitor pattern Demeter J 10172021

  • Slides: 33
Download presentation
Third lecture • Review • Visitor pattern • Demeter. J 10/17/2021 AOO 1

Third lecture • Review • Visitor pattern • Demeter. J 10/17/2021 AOO 1

Demeter. J in Demeter. J structure (*. cd) class diagrams structure-shy communication (*. ridl)

Demeter. J in Demeter. J structure (*. cd) class diagrams structure-shy communication (*. ridl) distribution compiler/ weaver structure-shy object description (*. input, at runtime) synchronization (*. cool) multi threading 10/17/2021 structure-shy behavior (*. beh) strategies and visitors AOO 2

Example in Demeter. J class graph in program. cd: A = B. X :

Example in Demeter. J class graph in program. cd: A = B. X : B. B : C | D common E. C = “c”. D = “d”. E =. 10/17/2021 AOO 3

Behavior basically in DJ program. beh: Main { {{ // all Java code public

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”); Class. Graph cg = new Class. Graph(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 } 10/17/2021 AOO 4

Output C-object: x b c D-object: Done. 10/17/2021 AOO 5

Output C-object: x b c D-object: Done. 10/17/2021 AOO 5

Design Patterns for Collaborative Behavior • Navigate composite object structure – Iterator • Traversal

Design Patterns for Collaborative Behavior • Navigate composite object structure – Iterator • Traversal through a collection • Define task-specific behavior – Visitor 10/17/2021 AOO 6

Visitor - Intent Represent an operation to be performed on the elements of an

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. 10/17/2021 AOO 7

Visitor - Example 10/17/2021 AOO 8

Visitor - Example 10/17/2021 AOO 8

Visitor - Structure 10/17/2021 AOO 9

Visitor - Structure 10/17/2021 AOO 9

Visitor - Applicability l l l When an object structure contains many classes of

Visitor - Applicability l l l 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. 10/17/2021 AOO 10

Visitor - Consequences ü ü ü It makes it easy to add new operations

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. 10/17/2021 AOO 11

Visitor - Consequences û It makes it hard to add new classes in the

Visitor - Consequences û It makes it hard to add new classes in the object structure. 10/17/2021 AOO 12

How does DJ help? • With Standard Visitor: If k concrete element classes and

How does DJ help? • With Standard Visitor: If k concrete element classes and m abstract element classes involved in traversal. – Need k Visit. Concrete. Element(Concrete. Element) methods in abstract visitor. – Need k+m accept methods, one in each element class. • DJ: no visit method, no accept methods. 10/17/2021 AOO 13

How does DJ help? • DJ makes it easy to change the class structure

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. 10/17/2021 AOO 14

Visitor Pattern • Intent: Modify the behavior of a group of collaborating classes without

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 10/17/2021 AOO 15

Visitor Pattern abstract class Equipment. Visitor{ abstract void visit. Card(Card e); abstract void visit.

Visitor Pattern abstract class Equipment. Visitor{ abstract void visit. Card(Card e); abstract void visit. Chassis(Chassis e); . . . } 10/17/2021 AOO 16

Visitor Pattern class Pricing. Visitor extends Equipment. Visitor{ private Currency total; void Visit. Card(Card

Visitor Pattern class Pricing. Visitor extends Equipment. Visitor{ private Currency total; void Visit. Card(Card e){total. add(e. Net. Price()); } void Visit. Chassis(Chassis e) {total. add(e. Discount. Price()); } … } 10/17/2021 AOO 17

Visitor Design Pattern class Inventory. Visitor extends Equipment. Visitor { Inventory inventory; void Visit.

Visitor Design Pattern class Inventory. Visitor extends Equipment. Visitor { Inventory inventory; void Visit. Card(Card e) {inventory. accumulate(e); } void Visit. Chassis(Chassis e){ inventory. accumulate(e); } … } 10/17/2021 AOO 18

Visitor class Card extends Equipment { void Accept(Equipment. Visitor v) {v. Visit. Card(this); }

Visitor class Card extends Equipment { void Accept(Equipment. Visitor v) {v. Visit. Card(this); } … } 10/17/2021 AOO 19

Visitor class Chassis { void Accept(Equipment. Visitor v){ v. Visit. Chassis(this); Enumeration e =

Visitor class Chassis { void Accept(Equipment. Visitor v){ v. Visit. Chassis(this); Enumeration e = parts. elements(); while (e. has. More. Elements()) ((Equipment) e. next. Element()). Accept(v); } } 10/17/2021 AOO 20

Visitor/Applicability • Collaborative tasks • Add behavior to composite structure 10/17/2021 AOO 21

Visitor/Applicability • Collaborative tasks • Add behavior to composite structure 10/17/2021 AOO 21

Visitor Abstract. Visitor : Concrete. Visitor 1 | Concrete. Visitor 2. Visit. Concrete. A(Concrete.

Visitor Abstract. Visitor : Concrete. Visitor 1 | Concrete. Visitor 2. Visit. Concrete. A(Concrete. A); Visit. Concrete. B(Concrete. B); Concrete. A = … Accept(Visitor v) {v. Visit. Concrete. A(this); /* further traversal */ } 10/17/2021 AOO 22

Visitor/Participants • Abstract. Visitor - empty visit operations • Concrete. Visitor - task specific

Visitor/Participants • Abstract. Visitor - empty visit operations • Concrete. Visitor - task specific visit operation • Concrete. Element - accept operation 10/17/2021 AOO 23

Visitor • Pros – Add behavior to composite class structure without cluttering class definitions

Visitor • Pros – Add behavior to composite class structure without cluttering class definitions • Cons – Explicit traversal, hard-coding structure – Auxiliary structure, many empty methods 10/17/2021 AOO 24

Visitor • hard changes: modify class structure – add new concrete element class: need

Visitor • hard changes: modify class structure – add new concrete element class: need to update all visitors – rename parts • easy changes: add a new visitor 10/17/2021 AOO 25

Improve Visitor by • Selective Visitor – before, after, around, not just accept –

Improve Visitor by • Selective Visitor – before, after, around, not just accept – no empty methods • Structure-shy Traversal – strategies 10/17/2021 AOO 26

Demeter. J programming • Now we focus on the structure and syntax of the

Demeter. J programming • Now we focus on the structure and syntax of the Demeter. J programming language. • It reuses Java unchanged. Java code between (@ and @) or {{ and }}. • Provides syntax to define traversal strategies and visitors and how to glue three together. 10/17/2021 AOO 27

Demeter. J programming • for simple use: prepare program. cd, program. beh, program. input

Demeter. J 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. 10/17/2021 AOO 28

Demeter. J programming • Style for calling Java program: always use a class Main

Demeter. J 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 10/17/2021 AOO 29

Demeter. J programming • program. cd – start it with a package name: package

Demeter. J 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 Demeter. J class dictionary for syntax (see Demeter. J AP Studio resource page) 10/17/2021 AOO 30

Demeter. J programming • can also use look-ahead definitions as in Java Compiler. See

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

Demeter. J programming • Continue with programming of behavior 10/17/2021 AOO 32

Demeter. J programming • Continue with programming of behavior 10/17/2021 AOO 32

End of viewgraphs 10/17/2021 AOO 33

End of viewgraphs 10/17/2021 AOO 33