motivating problem 2
play

Motivating Problem (2) Design for tree structures with whole-part - PowerPoint PPT Presentation

Motivating Problem (2) Design for tree structures with whole-part hierarchies . The Composite Design Pattern CABINET CHASSIS CHASSIS EECS3311 A: Software Design POWER_SUPPLY Fall 2018 C HEN -W EI W ANG DVD-CDROM HARD_DRIVE CARD 2


  1. Motivating Problem (2) Design for tree structures with whole-part hierarchies . The Composite Design Pattern CABINET CHASSIS CHASSIS EECS3311 A: Software Design POWER_SUPPLY Fall 2018 C HEN -W EI W ANG DVD-CDROM HARD_DRIVE CARD 2 Challenge : There are base and recursive modelling artifacts. 3 of 21 Motivating Problem (1) Multiple Inheritance: Sharing vs. Replication A class may have two more parent classes. ● Many manufactured systems, such as computer systems or stereo systems, are composed of individual components and sub-systems that contain components. e.g., A computer system is composed of: ● Individual pieces of equipment ( hard drives , cd-rom drives ) Each equipment has properties : e.g., power consumption and cost. ● Composites such as cabinets , busses , and chassis Each cabinet contains various types of chassis , each of which in turn containing components ( hard-drive , power-supply ) and busses that contain cards . ○ Features not renamed along the inheritance paths will be shared . ● Design a system that will allow us to easily build systems and [ e.g., age ] ○ Features renamed along the inheritance paths will be replicated . calculate their total cost and power consumption. [ e.g., tax id , address , pay taxes ] Exercise : Design the class for a smart watch, both a watch and an activity tracker. 2 of 21 4 of 21

  2. MI: Combining Abstractions (1) MI: Combining Abstractions (2) A : Separating Graphical features and Hierarchical features class RECTANGLE feature -- Queries class TREE[G] width , height : REAL feature -- Queries xpos , ypos : REAL parent : TREE [ G ] feature -- Commands descendants : LIST [ TREE [ G ]] make ( w , h : REAL ) feature -- Commands change_width add_child ( c : TREE [ G ]) change_height end move end test_window : BOOLEAN class WINDOW local w1 , w2 , w3 , w4 : WINDOW inherit do RECTANGLE create w1 . make (8, 6) ; create w2 . make (4, 3) TREE [ WINDOW ] create w3 . make (1, 1) ; create w4 . make (1, 1) feature w2 . add ( w4 ) ; w1 . add ( w2 ) ; w1 . add ( w3 ) add ( w : WINDOW ) Result := w1 . descendants . count = 2 end end 5 of 21 7 of 21 MI: Combining Abstractions (2.1) MI: Name Clashes Q : How do you design class(es) for nested windows? In class C , feature foo inherited from ancestor class A clashes with feature foo inherited from ancestor class B . Hints : height, width, xpos, ypos, change width, change height, move, parent window, descendant windows, add child window 6 of 21 8 of 21

  3. MI: Resolving Name Clashes Composite Architecture: Design (1.1) price: VALUE add(child: EQUIPMENT) children: LIST[EQUIPMENT] class C o.foo o.fog o.zoo inherit ✓ × × o: A A rename foo as fog end ✓ × × o: B B rename foo as zoo end o: C × ✓ ✓ . . . 9 of 21 11 of 21 Solution: The Composite Pattern Composite Architecture: Design (1.2) ● Design : Categorize into base artifacts or recursive artifacts. The client uses Class EQUIPMENT defines an interface for all abstract class objects in the composition: both the composite Programming : ● EQUIPMENT to and leaf nodes. manipulate objects May implement default behavior for add(child) Build a tree structure representing the whole-part hierarchy . etc. in the composition. price: VALUE add(child: EQUIPMENT) ● Runtime : children: LIST[EQUIPMENT] Class Allow clients to treat base objects (leafs) and recursive COMPOSITE � s role is (a) compositions (nodes) uniformly . implement leaf related ops such as price Polymorphism : leafs and nodes are “substitutable”. ⇒ and (b) to define component Dynamic Binding : Different versions of the same ⇒ behaviour such A leaf has no children. as storing a operation is applied on individual objects and composites . child. Note that the leaf also inherits features like e.g., Given e: EQUIPMENT : children and add that don � t necessarily make ○ e.price may return the unit price of a DISK DRIVE . all that sense for a leaf node. ○ e.price may sum prices of a CHASIS ’ containing equipments. 7 10 of 21 12 of 21

  4. Composite Architecture: Design (1.3) Composite Architecture: Design (2.2) Put the tree behavior such as adding a child and list of children Put the price & here where it is needed power consumption behavior here Q : Any flaw of this first design? A : Two “composite” features defined at the EQUIPMENT level: ○ children: LIST[EQUIPMENT] ○ add(child: EQUIPMENT) ⇒ Inherited to all base equipments (e.g., HARD DRIVE ) that do not apply to such features. 9 13 of 21 15 of 21 Composite Architecture: Design (2.1) Implementing the Composite Pattern (1) deferred class EQUIPMENT feature name : STRING price : REAL -- uniform access principle end class CARD inherit EQUIPMENT feature make ( n : STRING ; p : REAL ) do name := n price := p -- price is an attribute end end 8 14 of 21 16 of 21

  5. Implementing the Composite Pattern (2.1) Testing the Composite Pattern test_composite_equipment : BOOLEAN local card , drive : EQUIPMENT deferred class cabinet : CABINET -- holds a CHASSIS COMPOSITE [ T ] chassis : CHASSIS -- contains a BUS and a DISK_DRIVE feature bus : BUS -- holds a CARD children : LINKED_LIST [ T ] do create { CARD } card . make ("16Mbs Token Ring", 200) add ( c : T ) create { DISK_DRIVE } drive . make ("500 GB harddrive", 500) do create bus . make ("MCA Bus") children . extend ( c ) -- Polymorphism create chassis . make ("PC Chassis") end create cabinet . make ("PC Cabinet") end bus . add ( card ) chassis . add ( bus ) Exercise : Make the COMPOSITE class iterable . chassis . add ( drive ) cabinet . add ( chassis ) Result := cabinet . price = 700 end 17 of 21 19 of 21 Implementing the Composite Pattern (2.2) Index (1) Motivating Problem (1) class Motivating Problem (2) COMPOSITE_EQUIPMENT inherit Multiple Inheritance: Sharing vs. Replication EQUIPMENT MI: Combining Abstractions (1) COMPOSITE [ EQUIPMENT ] create MI: Combining Abstractions (2.1) make MI: Combining Abstractions (2) feature make ( n : STRING ) MI: Name Clashes do name := n ; create children . make end price : REAL -- price is a query MI: Resolving Name Clashes -- Sum the net prices of all sub-equipments Solution: The Composite Pattern do across Composite Architecture: Design (1.1) children as cursor Composite Architecture: Design (1.2) loop Result := Result + cursor . item . price -- dynamic binding Composite Architecture: Design (1.3) end Composite Architecture: Design (2.1) end end Composite Architecture: Design (2.2) 18 of 21 20 of 21

  6. Index (2) Implementing the Composite Pattern (1) Implementing the Composite Pattern (2.1) Implementing the Composite Pattern (2.2) Testing the Composite Pattern 21 of 21

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend