UML Class Diagrams
• 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
UML Class: Different levels of detailBankAccount - balance: int - iRateCharge: float +deposit: int +withdraw: int signature BankAccount balance iRateCharge deposit withdraw BankAccount balance iRateCharge BankAccount
Objects and ClassesClassName
attribute2: data_type attribute3: type = init_val ... operation1() operation2(arg_list) operation3() : returntype ... anObject: ClassName attribute1 = value1 attribute2 = value2 …
Object (instance) ..…is instantiated from ……... Class
Objects and Classes: ExampleCar
registrationNumber: Integer driver: String = “Unspecified” ...
drive (Integer speed) getDriver() : String ... spaceWagon: Car manufacturer = Mitsubishi registrationNumber = R637GNU driver = “Naomi”
Class DiagramsBankAccount Customer 10
• A student takes between 0 and 12 modules • A module is taken by at least 10 students
Student 10..* Module 0..12 • Many-to-many relationship Student * Module *
1-to-1 and 1-to-many Associations
1-to-1 association 1-to-many association * draw() Polygon x:Integer y:Integer Point 1 Has-capital name:String Country name:String City 1 1
Association Notationmarried to wife husband 0..1 0..1 0..* 0..1 parentage offspring 0..1 0..1 husband wife married to Individual Family child
• An aggregationis a special case of association denoting a “consists of” hierarchy.
• The aggregateis the parent class, the componentsare the children class.
Muffler Tailpipe 0..2
“is a part of”
14 weight Car serial number year manufacturer model colour drive purchase
Aggregation ExampleEngine horsepower volume on off 3,4,5 Wheel diameter number of bolts 2,4 Door open close Battery amps volts charge discharge * Brakelight on off 1 1
• A solid diamond denotes composition, a strong form of aggregation where components cannot exist without the aggregate.
Assembly or Composition?• Assembly: component can exist on its own
• Composition: component cannot exist on its own
Generalization/InheritanceSavingsAccount Withdraw() ChequeAccount Withdraw() Account Balance AccountId Deposit() Withdraw() GetBalance() Is a kind of 18
• 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 notbe removed in
Associations: which type?• If in doubt use simple association
• Use aggregation or composition for “has a” relationships
• Use inheritance for “is a” relationships
Visibility and Scope ...
Public ...………. +
Visible to using classes
Protected ……….. #
Visible to subclasses
-Visible only within this class
Window -size : Rectangle -xptr : Xwindow * -visibility : Boolean +Window () +display () : void
Finding Classes• Study use case descriptions
• Grammatical analysis – Nouns – Adjectives – Verbs • Brainstorm • Use CRC cards 22
Example: Scenario From Problem
• 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.
Mapping Parts of Speech to Object
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
Identifying Inappropriate Classes• Redundant
• An event or an operation • Meta-language
• Outside the scope of the system • An attribute
Two OO Design Camps• Data driven design
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
• General mechanism for grouping modeling elements
• Package “owns” its contents • Defines a nested name space
c.f. Java Style: PackageName.ClassName
• Elements belong to one and only onePackage • Packages can be nested.
Packages with Dependencies
• Dependencies should be one-way
• Avoid cyclic dependencies!
• Less stable components depend on more stable (reusable) ones
• Packages reflect
• The art of large scale design is minimising dependencies.
• 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