Third lecture Review Visitor pattern Demeter J 3122021

  • Slides: 33
Download presentation
Third lecture • Review • Visitor pattern • Demeter. J 3/12/2021 SD 1

Third lecture • Review • Visitor pattern • Demeter. J 3/12/2021 SD 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 3/12/2021 structure-shy behavior (*. beh) strategies and visitors SD 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 =. 3/12/2021 SD 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 } 3/12/2021 SD 4

Output C-object: x b c D-object: Done. 3/12/2021 SD 5

Output C-object: x b c D-object: Done. 3/12/2021 SD 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 3/12/2021 SD 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. 3/12/2021 SD 7

Visitor - Example 3/12/2021 SD 8

Visitor - Example 3/12/2021 SD 8

Visitor - Structure 3/12/2021 SD 9

Visitor - Structure 3/12/2021 SD 9

Visitor - Applicability l l l 3/12/2021 When an object structure contains many classes

Visitor - Applicability l l l 3/12/2021 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. SD 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. 3/12/2021 SD 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. 3/12/2021 SD 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 abstract visit methods, no accept methods. 3/12/2021 SD 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. 3/12/2021 SD 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 3/12/2021 SD 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); . . . } 3/12/2021 SD 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()); } … } 3/12/2021 SD 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); } … } 3/12/2021 SD 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); } … } 3/12/2021 SD 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); } } 3/12/2021 SD 20

Visitor/Applicability • Collaborative tasks • Add behavior to composite structure 3/12/2021 SD 21

Visitor/Applicability • Collaborative tasks • Add behavior to composite structure 3/12/2021 SD 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 */ } 3/12/2021 SD 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 3/12/2021 SD 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 3/12/2021 SD 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 3/12/2021 SD 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 3/12/2021 SD 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. 3/12/2021 SD 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. 3/12/2021 SD 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 3/12/2021 SD 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) 3/12/2021 SD 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. 3/12/2021 SD 31

Demeter. J programming • Continue with programming of behavior 3/12/2021 SD 32

Demeter. J programming • Continue with programming of behavior 3/12/2021 SD 32

End of viewgraphs 3/12/2021 SD 33

End of viewgraphs 3/12/2021 SD 33