UML Class Diagrams 1 Lesson Objectives Understand UML class diagrams and object modelling Be able to identify the components needed to produce a class diagram from a specification Be able to produce class diagrams and document them using Eclipse/Together 2
UML Class BankAccount Name balance iratecharge deposit withdraw Attributes Methods/ operations 3 UML Class: Different levels of detail BankAccount BankAccount - balance: int - iratecharge: float +deposit:int +withdraw:int balance iratecharge deposit withdraw signature BankAccount BankAccount balance iratecharge 4
Objects and Classes anobject: ClassName attribute1 = value1 attribute2 = value2 ClassName attribute1 attribute2: data_type attribute3: type = init_val... operation1() operation2(arg_list) operation3() : returntype... Object (instance).. is instantiated from... Class 5 Objects and Classes: Example Car spacewagon: Car manufacturer = Mitsubishi registrationnumber = R637GNU driver = Naomi manufacturer registrationnumber: Integer driver: String = Unspecified... register() drive (Integer speed) getdriver() : String... 6
Class Diagrams BankAccount Customer 9 Associations: Multiplicity Student Module * * Many-to-many relationship Student Module 10..* 0..12 A student takes between 0 and 12 modules A module is taken by at least 10 students 10
1-to-1 and 1-to-many Associations Country name:string 1 Has-capital 1 City name:string 1-to-1 association draw() Polygon 1 * Point x:integer y:integer 1-to-many association 11 Association Notation Family 0..1 parentage offspring child 0..* Individual 0..1 wife 0..1 husband married to 12
Aggregation An aggregation is a special case of association denoting a consists of hierarchy. The aggregate is the parent class, the components are the children class. Exhaust System is a part of Muffler 1 0..2 Tailpipe 13 Aggregation Example Engine horsepower volume on off 1 Car serial number year manufacturer model colour weight drive purchase 3,4,5 * 2,4 1 Wheel Brakelight Door Battery diameter number of bolts on off 14 open close amps volts charge discharge
Composition A solid diamond denotes composition, a strong form of aggregation where components cannot exist without the aggregate. TicketMachine 3 ZoneButton 15 Assembly or Composition? Assembly: component can exist on its own Composition: component cannot exist on its own Football Club * Footballer * Fixtures to play 16
Generalization/Inheritance Account Balance AccountId Deposit() Withdraw() GetBalance() Is a kind of SavingsAccount ChequeAccount Withdraw() Withdraw() 17 Inheritance Classes inherit the attributes and operations of their parents i.e. from the generalization to the specialization Operations and attributes may be re-defined Additional operations or attributes must be defined Operations and attributes may not be removed in the specialization 18
Associations: which type? If in doubt use simple association Use aggregation or composition for has a relationships Use inheritance for is a relationships 19 Visibility and Scope... Public.... + Visible to using classes Protected.. # Visible to subclasses Private... - Visible only within this class -size : Rectangle Window -xptr : Xwindow * -visibility : Boolean +Window () +display () : void +hide () : void #attachxwindow (Xwindow * xwin) : void 20
Finding Classes Study use case descriptions Grammatical analysis Nouns Adjectives Verbs Brainstorm Use CRC cards 21 Example: Scenario From Problem Statement Jim enters a store with the intention of buying a toy for his 3 year old child. Help must be available within less than one minute. The store owner gives advice to the customer. The advice depends on the age range of the child and the attributes of the toy. Jim selects a dangerous toy which is unsuitable for the child. The store owner recommends a more suitable toy - a doll. 22
Mapping Parts of Speech to Object Model Components Part of speech Model component Example Proper noun object Jim Smith Improper noun class Toy, doll Doing verb method Buy, recommend being verb inheritance is-a (kind-of) having verb aggregation has an modal verb constraint must be adjective attribute 3 years old transitive verb method enter intransitive verb method (event) depends on 23 Identifying Inappropriate Classes Redundant Vague An event or an operation Meta-language Outside the scope of the system An attribute Only operations are Set() and Get(). 24
CRC Card 25 Two OO Design Camps Data driven design Responsibility driven design 26
Package It All Up Pack up design into discrete physical units that can be edited, compiled, linked, reused Construct physical modules Ideally use one package for each subsystem System decomposition might not be good for implementation Two design principles for packaging Minimize coupling Maximize cohesiveness 27 UML Packages General mechanism for grouping modeling elements Package owns its contents Defines a nested name space e.g. PackageName::ClassName c.f. Java Style: PackageName.ClassName Elements belong to one and only one Package Packages can be nested. 29
Packages with Dependencies Dependencies should be one-way Avoid cyclic dependencies! Less stable components depend on more stable (reusable) ones Packages reflect development responsibility The art of large scale design is minimising dependencies. 30 Summary Class diagrams are the central feature of UML Class diagrams enable us to model objects, classes and associations between classes. At more detailed level you may need to consider special types of association like aggregation and composition Inheritance is an especially important type of association Finding classes is difficult - techniques like noun identification and CRC cards are helpful 32