uml class diagrams. represent the (static) structure of the system generalin javain c++ namenamename...

42
UML Class Diagrams

Upload: valeria-killingsworth

Post on 14-Dec-2015

227 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: UML Class Diagrams. Represent the (static) structure of the system GeneralIn JavaIn C++ NameNameName StateVariablesMembers BehaviorMethodsFunctions

UML Class Diagrams

Page 2: UML Class Diagrams. Represent the (static) structure of the system GeneralIn JavaIn C++ NameNameName StateVariablesMembers BehaviorMethodsFunctions

UML Class Diagrams

Represent the (static) structure of the system

General In Java In C++Name Name Name

State Variables Members

Behavior Methods Functions

Page 3: UML Class Diagrams. Represent the (static) structure of the system GeneralIn JavaIn C++ NameNameName StateVariablesMembers BehaviorMethodsFunctions

Relationships Between Classes

AssociationPermanent, structural, “has a”Solid line (arrowhead optional)

AggregationPermanent, structural, a whole created from partsSolid line with diamond from whole

DependencyTemporary, “uses a”Dotted line with arrowhead

GeneralizationInheritance, “is a”Solid line with open (triangular) arrowhead

ImplementationDotted line with open (triangular) arrowhead

OR

Page 4: UML Class Diagrams. Represent the (static) structure of the system GeneralIn JavaIn C++ NameNameName StateVariablesMembers BehaviorMethodsFunctions

Association

Denotes permanent, structural relationship

State of class A contains class B

Represented by solid line (arrowhead optional)

Car and Engine classes know about each other

Page 5: UML Class Diagrams. Represent the (static) structure of the system GeneralIn JavaIn C++ NameNameName StateVariablesMembers BehaviorMethodsFunctions

Associations w/ Navigation Information

Can indicate direction of relationship

Represented by solid line with arrowhead

Gas Pedal class knows about Engine class Engine class doesn’t know about Gas Pedal class

Page 6: UML Class Diagrams. Represent the (static) structure of the system GeneralIn JavaIn C++ NameNameName StateVariablesMembers BehaviorMethodsFunctions

Associations w/ Navigation Information

Denotes “has-a” relationship between classes

“Gas Pedal” has an “Engine”

State of Gas Pedal class contains instance of Engine class can invoke its methods

Page 7: UML Class Diagrams. Represent the (static) structure of the system GeneralIn JavaIn C++ NameNameName StateVariablesMembers BehaviorMethodsFunctions

Multiplicity of Associations

Some relationships may be quantified

Multiplicity denotes how many objects the source object can legitimately reference

Notation* 0, 1, or more

5 5 exactly

5..8 between 5 and 8, inclusive

5..* 5 or more

Page 8: UML Class Diagrams. Represent the (static) structure of the system GeneralIn JavaIn C++ NameNameName StateVariablesMembers BehaviorMethodsFunctions

Multiplicity of AssociationsMany-to-one

Bank has many ATMs, ATM knows only 1 bank

One-to-manyInventory has many items, items know 1 inventory

Page 9: UML Class Diagrams. Represent the (static) structure of the system GeneralIn JavaIn C++ NameNameName StateVariablesMembers BehaviorMethodsFunctions

Aggregation and Composition

A special kind of association

Models whole-part relationship between things

Whole is usually referred to as composite

Page 10: UML Class Diagrams. Represent the (static) structure of the system GeneralIn JavaIn C++ NameNameName StateVariablesMembers BehaviorMethodsFunctions

Composite aggregation

Also referred to as composition

Composite solely owns the part and they are in a tree structure parts hierarchy

Most common form of aggregation

In UML, represented by filled diamond

1 0..7Hand Finger

Page 11: UML Class Diagrams. Represent the (static) structure of the system GeneralIn JavaIn C++ NameNameName StateVariablesMembers BehaviorMethodsFunctions

Shared Aggregation

Part may be in many composite instances

In UML, represented as hollow diamond

Network

Node Arc

Page 12: UML Class Diagrams. Represent the (static) structure of the system GeneralIn JavaIn C++ NameNameName StateVariablesMembers BehaviorMethodsFunctions

How to identify aggregation

Lifetime of part is bound within lifetime of composite

There is a create-delete dependency

There is an obvious whole-part physical or logical assembly

Some properties of composite propagate to parts (e.g., location)

Operations applied to composite propagate to parts (e.g., destruction, movement, recording)

Page 13: UML Class Diagrams. Represent the (static) structure of the system GeneralIn JavaIn C++ NameNameName StateVariablesMembers BehaviorMethodsFunctions

Why show aggregation

Clarifies domain constraints regarding part- whole relationship

Assists in identification of a creator

Operations applied to whole should usually propagate to parts

Identifying whole wrt a part supports encapsulation

Page 14: UML Class Diagrams. Represent the (static) structure of the system GeneralIn JavaIn C++ NameNameName StateVariablesMembers BehaviorMethodsFunctions

Dependency

Denotes dependence between classes

Always directed (Class A depends on B)

Represented by dotted line with arrowhead

A depends on B

A B

Page 15: UML Class Diagrams. Represent the (static) structure of the system GeneralIn JavaIn C++ NameNameName StateVariablesMembers BehaviorMethodsFunctions

Dependency

Caused by class methods

Method in Class A temporarily “uses a” object of type Class B

Change in Class B may affect class A

A uses object of class B

A B

Page 16: UML Class Diagrams. Represent the (static) structure of the system GeneralIn JavaIn C++ NameNameName StateVariablesMembers BehaviorMethodsFunctions

Dependency

Dependence may be caused byLocal variable

Parameter

Return value

Example

Class A { Class B {

B Foo(B x) { …

B y = new B(); …

return y; …

} } }

Page 17: UML Class Diagrams. Represent the (static) structure of the system GeneralIn JavaIn C++ NameNameName StateVariablesMembers BehaviorMethodsFunctions

Dependency Example

Class Driver depends on Class Car

Page 18: UML Class Diagrams. Represent the (static) structure of the system GeneralIn JavaIn C++ NameNameName StateVariablesMembers BehaviorMethodsFunctions

Generalization

Denotes inheritance between classes

Can view as “is-a” relationship

Represented by line ending in (open) triangle

Laptop, Desktop, PDA inherit state & behavior from Computers

Page 19: UML Class Diagrams. Represent the (static) structure of the system GeneralIn JavaIn C++ NameNameName StateVariablesMembers BehaviorMethodsFunctions

Implementation

Denotes class implements Java interface

Represented by dotted line ending in (open) triangle

A implements interface B

A «B»

Page 20: UML Class Diagrams. Represent the (static) structure of the system GeneralIn JavaIn C++ NameNameName StateVariablesMembers BehaviorMethodsFunctions

UML Examples

Read UML class diagram

Try to understand relationships

ExamplesPets & owners

Computer disk organization

Banking system

Home heating system

Printing system

Page 21: UML Class Diagrams. Represent the (static) structure of the system GeneralIn JavaIn C++ NameNameName StateVariablesMembers BehaviorMethodsFunctions

UML Example – Veterinary System

Try to read & understand UML diagram

Page 22: UML Class Diagrams. Represent the (static) structure of the system GeneralIn JavaIn C++ NameNameName StateVariablesMembers BehaviorMethodsFunctions

UML Example – Veterinary System

Try to read & understand UML diagram

• 1 or more Pets associated with 1 PetOwner

Page 23: UML Class Diagrams. Represent the (static) structure of the system GeneralIn JavaIn C++ NameNameName StateVariablesMembers BehaviorMethodsFunctions

UML Example – Computer System

Try to read & understand UML diagram

Page 24: UML Class Diagrams. Represent the (static) structure of the system GeneralIn JavaIn C++ NameNameName StateVariablesMembers BehaviorMethodsFunctions

UML Example – Computer System

Try to read & understand UML diagram

• 1 CPU associated with 0 or more Controllers

• 1-4 DiskDrives associated with 1 SCSIController

• SCSIController is a (specialized) Controller

Page 25: UML Class Diagrams. Represent the (static) structure of the system GeneralIn JavaIn C++ NameNameName StateVariablesMembers BehaviorMethodsFunctions

UML Example – Banking System

Try to read & understand UML diagram

Page 26: UML Class Diagrams. Represent the (static) structure of the system GeneralIn JavaIn C++ NameNameName StateVariablesMembers BehaviorMethodsFunctions

UML Example – Banking System

• 1 Bank associated with 0 or more Accounts

• Checking, Savings, MoneyMarket are Accounts

Page 27: UML Class Diagrams. Represent the (static) structure of the system GeneralIn JavaIn C++ NameNameName StateVariablesMembers BehaviorMethodsFunctions

UML Example – Home Heating System

Try to read & understand UML diagram

Page 28: UML Class Diagrams. Represent the (static) structure of the system GeneralIn JavaIn C++ NameNameName StateVariablesMembers BehaviorMethodsFunctions

UML Example – Home Heating System

• Each Thermostat has 1 Room

• Each Thermostat associated with 0 or more Heaters

• ElectricHeater is a specialized Heater

• AubeTH101D is a specialized Thermostat

Page 29: UML Class Diagrams. Represent the (static) structure of the system GeneralIn JavaIn C++ NameNameName StateVariablesMembers BehaviorMethodsFunctions

UML Class Diagrams Java

Different representation of same informationName, state, behavior of class

Relationship(s) between classes

Practice deriving one from the other Accurately depicting relationship between classes

Page 30: UML Class Diagrams. Represent the (static) structure of the system GeneralIn JavaIn C++ NameNameName StateVariablesMembers BehaviorMethodsFunctions

UML Java : Veterinary System

UML

Java

Page 31: UML Class Diagrams. Represent the (static) structure of the system GeneralIn JavaIn C++ NameNameName StateVariablesMembers BehaviorMethodsFunctions

UML Java : Veterinary System

UML

Javaclass Pet {

PetOwner myOwner; // 1 owner for each pet}class PetOwner {

Pet [ ] myPets; // multiple pets for each owner}

Page 32: UML Class Diagrams. Represent the (static) structure of the system GeneralIn JavaIn C++ NameNameName StateVariablesMembers BehaviorMethodsFunctions

Java UML : Veterinary System

Javaclass Pet {

PetOwner myOwner; // 1 owner for each pet}class PetOwner {

Pet [ ] myPets; // multiple pets for each owner}

UML

Page 33: UML Class Diagrams. Represent the (static) structure of the system GeneralIn JavaIn C++ NameNameName StateVariablesMembers BehaviorMethodsFunctions

Java UML : Veterinary System

Javaclass Pet {

PetOwner myOwner; // 1 owner for each pet}class PetOwner {

Pet [ ] myPets; // multiple pets for each owner}

UML

Page 34: UML Class Diagrams. Represent the (static) structure of the system GeneralIn JavaIn C++ NameNameName StateVariablesMembers BehaviorMethodsFunctions

UML Class Diagrams Java

UML

Javaclass Pet {

PetOwner myOwner; // 1 owner for each pet}class PetOwner {

Pet [ ] myPets; // multiple pets for each owner}

Page 35: UML Class Diagrams. Represent the (static) structure of the system GeneralIn JavaIn C++ NameNameName StateVariablesMembers BehaviorMethodsFunctions

UML Java : Computer System

UML

Java

Page 36: UML Class Diagrams. Represent the (static) structure of the system GeneralIn JavaIn C++ NameNameName StateVariablesMembers BehaviorMethodsFunctions

UML Java : Computer System

UML

Javaclass Controller {}class SCSIController extends Controller {}

Page 37: UML Class Diagrams. Represent the (static) structure of the system GeneralIn JavaIn C++ NameNameName StateVariablesMembers BehaviorMethodsFunctions

UML Java : Computer System

UML

JavaDesign code using all available information in UML…

Page 38: UML Class Diagrams. Represent the (static) structure of the system GeneralIn JavaIn C++ NameNameName StateVariablesMembers BehaviorMethodsFunctions

UML Java : Computer System

Javaclass CPU {

Controller [ ] myCtlrs;}class Controller {

CPU myCPU;}class SCSIController extends Controller {

DiskDrive [ ] myDrives = new DiskDrive[4];}Class DiskDrive {

SCSIController mySCSI;}

Page 39: UML Class Diagrams. Represent the (static) structure of the system GeneralIn JavaIn C++ NameNameName StateVariablesMembers BehaviorMethodsFunctions

Java UML : Printing System

Javaclass Registry {

PrintQueue findQueue();}class PrintQueue {

List printJobs;Printer myPrinter;Registry myRegistry;void newJob();int length();Resources getResource();

}

Page 40: UML Class Diagrams. Represent the (static) structure of the system GeneralIn JavaIn C++ NameNameName StateVariablesMembers BehaviorMethodsFunctions

Java UML : Printing System

JavaClass Printer {

Resources myResources;Job curJob;void print();boolean busy();boolean on();

}class Job {

Job(Registry r) {…

}}

Page 41: UML Class Diagrams. Represent the (static) structure of the system GeneralIn JavaIn C++ NameNameName StateVariablesMembers BehaviorMethodsFunctions

Java UML : Printing System

Page 42: UML Class Diagrams. Represent the (static) structure of the system GeneralIn JavaIn C++ NameNameName StateVariablesMembers BehaviorMethodsFunctions

UML Summary

Graphics modeling language

Visually represents design of software system

Focus: Class diagramsContents of a class

Relationship between classes

You should be able toDraw UML class diagram given code

Write code given UML class diagram