240 likes | 344 Views
Class Diagrams Oct 20, 2006. Recap: CRC Cards Process. Initial class list Refined class list while (More use case scenarios left) do Take a use case scenario Assign responsibilities to classes Find super- and sub-classes Find collaborators od. Today’s Lecture.
E N D
Recap: CRC Cards Process • Initial class list • Refined class list • while (More use case scenarios left) • do • Take a use case scenario • Assign responsibilities to classes • Find super- and sub-classes • Find collaborators • od
Today’s Lecture • Class Diagrams Basics • Class Diagram Notations • Classifier, attributes, operations • Interface implementation, subclassing • Dependency • Associations (multiplicities) • Application of information hiding and DSMs
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
Class Diagram • UML Design Notation • Shows classes, their attributes and methods • Interfaces • Collaborations • Dependency, Generalization, Relationships • Remember: Design - complete description of structure and partial description of function • A class diagram describes the structure
Class Diagram View Motor Switch 1 -switch:Switch -state:bool switch +run() +getState():bool
Common Stereotypes • <<interface>> • <<type>> structure & behavior • <<enumeration>> collection of discrete values • <<implementationClass>> helper class • <<class>> is assumed by default
Attribute Name, Types Motor - switch : Switch +run() Visibility Attribute Type Attribute Name * Also multiplicity, default value, and property
Visibility • public → + • private → - • protected → # • package → ~ • derived → /
Alternative Notation Motor +run() Switch -state:bool +getState():bool Multiplicity 1 switch Association (Containment)
Operations: Visibility, Args, Return Types Switch -state : bool +getState(): bool +setState(bool) Return Type Visibility Operations Argument Type
A Slightly Complex Example <<interface>> ISort <<interface>> IStorage +sort(IStorage) +add(int) +length():int +itemAt(int):int +setAt(int, int) +createNew():IStorage MergeSort QuickSort … … +sort(IStorage) +sort(IStorage) Array LinkedList … … +add(int) … +add(int) … SortingApplication storage:IStorage sort:ISort 1 sort storage +Main(string[]):int 1
Hiding Design Decision <<interface>> IElement IntElement <<interface>> ISort item:int <<interface>> IStorage +get():int +set(int) +sort(IStorage) +add(IElement) +length():int +itemAt(int):IElement +setAt(int, IElement) +createNew():IStorage MergeSort QuickSort … … +sort(IStorage) +sort(IStorage) Array LinkedList … … +add(IElement) … +add(IElement) … SortingApplication 0..* 1 storage:IStorage sort:ISort 1 sort storage +Main(string[]):int 1
Additional Dependencies <<interface>> IElement IntElement <<interface>> ISort item:int <<interface>> IStorage +get():int +set(int) +sort(IStorage) +add(IElement) +length():int +itemAt(int):IElement +setAt(int, IElement) +createNew():IStorage MergeSort QuickSort … … +sort(IStorage) +sort(IStorage) Array LinkedList … … +add(IElement) … +add(IElement) … 0..* 1 sort SortingApplication 1 1 storage:IStorage sort:ISort storage +Main(string[]):int
Architectural Style: Layered <<interface>> IElement IntElement <<interface>> ISort item:int <<interface>> IStorage +get():int +set(int) SortingApplication +sort(IStorage) storage:IStorage sort:ISort +add(IElement) +length():int +itemAt(int):IElement +setAt(int, IElement) +createNew():IStorage +Main(string[]):int MergeSort QuickSort … … +sort(IStorage) +sort(IStorage) Array LinkedList … … +add(IElement) … +add(IElement) … 0..* 1 1 storage sort 1 Layer 2 Layer 1
Architectural Style: 3-tiered UI <<interface>> IElement app:SortingApplication IntElement +Main(string[]):int <<interface>> ISort item:int <<interface>> IStorage +get():int +set(int) SortingApplication +sort(IStorage) storage:IStorage sort:ISort +add(IElement) +length():int +itemAt(int):IElement +setAt(int, IElement) +createNew():IStorage +sort(…):IStorage MergeSort QuickSort … … +sort(IStorage) +sort(IStorage) Array LinkedList … … +add(IElement) … +add(IElement) … 0..* 1 1 1 app storage sort 1 Layer 3 Layer 2 Layer 1
Summary • 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
R1: Ask the user for an input list of numbers; R2: Ask the user for the choice of storage technique for the numbers. Two storage techniques are supported Array that inherits from java.util.ArrayList and Linked List that inherits from java.util.LinkedList, R3: Ask the user for the choice of sorting algorithm. Two sorting algorithms merge sort and quick sort, are supported. R4: Sort the numbers, and R5: Output the sorted numbers. Problem Description: design and implement a sorting application for positive integers. This application must: Initial Class List: Refined Class List: