Download presentation
Presentation is loading. Please wait.
1
From Design To Code Oct 25, 2006
2
Recap: Class Diagrams Class diagrams represent design structure
Three parts: name, attribute, operations Visibility, attribute type, multiplicity Association, association multiplicity Generalization i.e. interface impl, subclassing Composition i.e. class A contains class B Applied information hiding, DSM, layering
3
Recap: Interaction Diagrams
Represent functionality Two major parts: Life line boxes (represent life time of an object) Messages between objects (instances) Also looked at some special case notations Instances: parameterized instances, singleton instances, array elements Messages: creation, destruction, conditional messages, mutually exclusive conditional messages, asynchronous messages, polymorphic messages Abstractions: looping, iteration over collection, nesting of frames, decomposition
4
Today’s Lecture Interaction Diagrams Basics & Notations
Responsibility Assignment Principles
5
Example: Switch-Motor System
Scenario: Motor can run, when the Switch state is on Class list: Switch and Motor R1: Store the decision condition R2: Make a decision R3: Run the motor
6
Class Diagram View Switch Motor -state:bool -switch:Switch
1 -state:bool -switch:Switch switch +getState():bool +run() Class Diagram: three parts – class name, attributes, and operations The first part may also contain stereotypes.
7
Interaction Diagram View
:Motor Lifeline boxes switch:Switch run getState state :Motor represents unnamed instance of class motor switch:Switch represents named instance of class Switch Another examples in the book are paremeterized that are written ArrayList <int>, if you were to use generics in your homework 3. ith element in the array “arr” of type IntArray would be written arr[i]: IntArray In case of a singleton, i.e. a class that only has a single instance in the application domain, we would write a 1 in the top right corner. How would you show creation? Figure in book How would you show destruction? Figure in book How would you show looping? (Remember from sequence diagram example) Figure in book How would you show conditional messages? Figure in book How would you show mutually exclusive conditional messages? Figure in book How would you show iteration over a collection ? Figure in book How would you show nesting of frames? Example in Figure shows looping, when a certain condition is true. How would you decompose a larger interaction diagram? Figure shows one way. Also, remember from the sequence diagram examples. Essentially the same technique. How would you show polymorphism? Figure in book. How would you show asynchronous calls? Figure in book.
8
Switch-Motor: CRC Card View
Scenario: Motor can run, when the Switch state is on Class list: Switch and Motor R1: Store the decision condition, R2: Make a decision, and R3: Run the motor Class Name: Switch Subclasses: Super classes: Responsibilities Collaborators Store decision condition Class Name: Motor Subclasses: Super classes: Responsibilities Collaborators Make a decision Switch Run the motor Notice that the Switch is the collaborator of Motor, but not vice versa. Question: What is the coupling relationship in this case?
9
Class Diagram View Switch Motor -state:bool -switch:Switch
1 -state:bool -switch:Switch switch +getState():bool +run():void void run(){ if(switch.getState()){ /* Make a decision */ /* run the motor */ }
10
Impact of Change: Multi-State Switch
Motor 1 -state:bool -switch:Switch -state:int switch +getState():bool +run():void +getState():int void run(){ if(switch.getState()){ /* Make a decision */ /* run the motor */ } Change: Switch can have more than two states.
11
Change Escapes Module Boundary
Switch Motor 1 -state:bool -switch:Switch -state:int switch +getState():bool +run():void +getState():int void run(){ if(switch.getState()){ /* Make a decision */ /* run the motor */ } Change: Switch can have more than two states. Impact of Change
12
Information Expert Which class has the necessary information?
Example – class list: Switch and Motor R1: Store the decision condition, R2: Make a decision, and R3: Run the motor Class Name: Switch Subclasses: Super classes: Responsibilities Collaborators Store decision Condition Make a decision Class Name: Motor Subclasses: Super classes: Responsibilities Collaborators Run the motor Switch
13
Improved Class Diagram View
Switch Motor 1 -state:bool -switch:Switch switch +isOn():bool +run():void void run(){ if(switch.isOn()){ /* run the motor */ }
14
Implementation Changes Encapsulated
Switch Motor 1 -state:bool -switch:Switch -state:int switch +isOn():bool +run():void +isOn():bool void run(){ if(switch.isOn()){ /* run the motor */ } Change: Switch can have more than two states. Remember: Information Hiding
15
Example: User-Level Process Management
Main scenario: A process will maintain necessary information for it to run such as program counter (PC), reference to program segment (.ps), reference to data segments (.ds), registers (regs) Scenario of interest: functionality to create an identical copy of a process should be provided Class list: process, client R1: store information PC, .ps, .ds. Regs R2: create copy of process
16
User-Level Process Management : CRC Card View
Class list: process, client R1: store information pc, .ps, .ds. regs R2: create copy of process Class Name: Process Subclasses: Super classes: Responsibilities Collaborators Store information pc, .ps, .ds. regs Class Name: Client … Subclasses: Super classes: Responsibilities Collaborators Create copy Process of .pc, .ps, .ds, .regs Spawn another process Notice that the Switch is the collaborator of Motor, but not vice versa. Question: What is the coupling relationship in this case?
17
Change: Register-based to Stack-based Machine
Class list: process, client R1: store information pc, .stack, .top, .ps, .ds. R2: create copy of process Class Name: Process Subclasses: Super classes: Responsibilities Collaborators Store information pc, .stack, .top, .ds. regs Class Name: Client … Subclasses: Super classes: Responsibilities Collaborators Create copy Process of .pc, .ps, .ds, .regs Spawn another process Notice that the Switch is the collaborator of Motor, but not vice versa. Question: What is the coupling relationship in this case? Impact on all clients
18
Creator: Which class has the initializing data?
Class list: process, client R1: store information pc, .ps, .ds. regs R2: create copy of process Class Name: Process Subclasses: Super classes: Responsibilities Collaborators Store information pc, .ps, .ds. regs Create copy of .pc, .ps, .ds, . regs and spawn another process Class Name: Client … Subclasses: Super classes: Responsibilities Collaborators Process Notice that the Switch is the collaborator of Motor, but not vice versa. Question: What is the coupling relationship in this case? Change Impact Encap-sulated
19
Adding GUI to the Sorting Application
Integers (>0) Sorting Algorithm Merge Sort : User Sort Which class should handle the Sort Event? Class Name: UI Subclasses: Super classes: Responsibilities Collaborators Handle Sort SortingApp Event Class Name: SortingApp Subclasses: Super classes: Responsibilities Collaborators Create sorting, ISort, IStorage storage instance etc.
20
Adding GUI to the Sorting Application
SortButton … /addActionListener(ActionListener l) javax.swing.DefaultButtonModel … +addActionListener(ActionListener l) SortingApplication java.awt.event.ActionListener … +actionPerformed(ActionEvent e) storage:IStorage sort:ISort +sort(…):IStorage /actionPerformed(ActionEvent e)
21
Summary Interaction Diagrams Responsibility Assignment Principles
Information Expert Creator Controller Polymorphism Pure Fabrication Indirection Protected Variations Most of these principles are derivative of the information hiding principle that we have studied before. In this lecture: - Understand these principles through examples Understand class and interaction diagrams on the way Relate these principles to information hiding principle Also, show examples of the CRC card process
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.