27
Winter 2007 ACS-3913 Ron McFa dyen 1 Classes Represented by a rectangle with possibly 3 compartments Customer Customer Name Address Customer Name Address getName() checkCreditRating() Customer getName() checkCreditRating()

Winter 2007ACS-3913 Ron McFadyen1 Classes Represented by a rectangle with possibly 3 compartments Customer Name Address Customer Name Address getName()

  • View
    217

  • Download
    0

Embed Size (px)

Citation preview

Winter 2007 ACS-3913 Ron McFadyen 1

Classes

Represented by a rectangle with possibly 3 compartments

Customer

Customer

NameAddress

Customer

NameAddress

getName()checkCreditRating()

Customer

getName()checkCreditRating()

Winter 2007 ACS-3913 Ron McFadyen 2

Classes

«Singleton»dbFacade

Some classes are stereotyped:

Later in the course we study the Singleton design pattern.

Some methodologies have 3 stereotypes for “analysis” classes: boundary, control, entity

Winter 2007 ACS-3913 Ron McFadyen 3

Class Diagram

Navigability

In the example below, student could be implemented with a history attribute:

Student History *

-history[0..*] : History

history[0..*] provides for the association to be navigated – we can send a message to a History object

Winter 2007 ACS-3913 Ron McFadyen 4

Instances

Joe: Customer

Class instances

An individual object (instance of a class) is shown with naming information underlined

An unnamed customer: Customer

A customer named Joe

Winter 2007 ACS-3913 Ron McFadyen 5

Diagramming notation

:sale

s4:sale

A sale object named s4

An unnamed sale object

s4

An object named s4

Sale

The class Sale

Winter 2007 ACS-3913 Ron McFadyen 6

Attributes

an object contains data which are defined as part of the Class definition

examples:• Students have names, addresses, etc; • Courses have titles, descriptions, prerequisite information.

Rectangle

corner: Point

Student

nameaddress

Level of detail present will depend on whether you are in analysis or design, and your purposes at the time

Winter 2007 ACS-3913 Ron McFadyen 7

Attributes

To what degree is an attribute visible to other classes?Private –Public +Protected #Package ~

Student

-name-address

Winter 2007 ACS-3913 Ron McFadyen 8

Attributes

Default values =Derived values /Multiplicity [ ]Ordering {ordered}Uniqueness {unique}

Invoice

-date:Date = today-/total: Currency-payments[0..*]: Currency

Student

-name-address[1..3] {unique}

Winter 2007 ACS-3913 Ron McFadyen 9

Operations. What are the responsibilities of a class? What can it do?

Visibility

Parameters

Signature the name, parameters, and return type of the operation

Student

+getName()+getGPA(term :Term, gpaType: String)

Winter 2007 ACS-3913 Ron McFadyen 10

Associations

• correspond to verbs expressing a relationship between classes

• example a Library Member borrows a Copy of a Book

•Multiplicities• we indicate via multiplicities the range of allowable cardinalities for participation in an association• examples: 1, 1..*, 0..*, 1..3

Winter 2007 ACS-3913 Ron McFadyen 11

Associations

• Names and roles

• you can name the relationship and indicate how to read it• you can give role names for participating objects

Person CompanyWorks for1..* 1

employer employee

The role of a Person in this relationship The role of a Company in this relationship

The name of the relationship and thedirection for reading the name

Winter 2007 ACS-3913 Ron McFadyen 12

Associations

•example: a Library Member borrows a Copy of a Book

Member Book* *

borrowerborrows

Winter 2007 ACS-3913 Ron McFadyen 13

Associations

• example: An employee is supervised by an employee

*

0,1Employee

reports to

supervised

supervisor

A reflexive association

Winter 2007 ACS-3913 Ron McFadyen 14

Interfaces

An interface is special type of class that cannot be instantiated. An application can never instantiate an interface.

An interface defines a set of public attributes and operations that some class must use (depends on)

There is no behaviour defined, no method coded

Some other class inherits the interface and provides the implementation

Winter 2007 ACS-3913 Ron McFadyen 15

From Head First …

package headfirst.observer.weatherobservable;import java.util.Observable;import java.util.Observer;

public class ForecastDisplay implements Observer, …

public void update(Observable observable, Object arg) {if (observable instanceof WeatherData) {

WeatherData weatherData = (WeatherData)observable;lastPressure = currentPressure;currentPressure = weatherData.getPressure();display();

}}public void display() {

System.out.print("Forecast: ");if (currentPressure > lastPressure) {

System.out.println("Improving weather on the way!");} …

Winter 2007 ACS-3913 Ron McFadyen 16

From Head First …

package headfirst.observer.weatherobservable;

import java.util.Observable;import java.util.Observer;

public class StatisticsDisplay implements Observer, DisplayElement {private float maxTemp = 0.0f;

…public void update(Observable observable, Object arg) {

if (observable instanceof WeatherData) {WeatherData weatherData = (WeatherData)observable;float temp = weatherData.getTemperature();…

display();}

}public void display() {

System.out.println("Avg/Max/Min temperature = " …

Winter 2007 ACS-3913 Ron McFadyen 17

From Head First …

<<interface>>Observer

ForecastDisplay

update()

These classes implement the update operation

StatisticsDisplay

Winter 2007 ACS-3913 Ron McFadyen 18

Sequence Diagram

•Objects are represented horizontally across the top of the diagram

•Each object has a lifeline

•some exist before and/or after

•some are created during

•some are destroyed during

•An active object is indicated by a narrow rectangle

•Focus of control

•Time is represented vertically down the diagram. Time moves forward as you go downwards

•Iteration is shown with a frame

Winter 2007 ACS-3913 Ron McFadyen 19

Sequence Diagram

Message types

•Synchronous

•Asynchronous

•Creation

•Reply

SynchronousA message from one object to another object, and where the first object must wait until the resulting action completes.ReplyRepresents an explicit return of control. Not often used.

Winter 2007 ACS-3913 Ron McFadyen 20

: register : sale

: payment

makePayment()

makePayment()payment()

sd Make payment

Sequence Diagram named Make payment

Object is created

Winter 2007 ACS-3913 Ron McFadyen 21

X

: register : sale

: payment

makePayment()

makePayment()payment()

sd Make Payment

Object is destroyed

Winter 2007 ACS-3913 Ron McFadyen 22

Message to ‘self’

A reflexive message

: sale

calcTotal()getTotal()

Winter 2007 ACS-3913 Ron McFadyen 23

Iteration

:Sale :SalesLineItem

t=getTotal()

st = getSubTotal()loop [I < numItems]

loop is one of the keywords you can use to specify the nature of the fragment

Winter 2007 ACS-3913 Ron McFadyen 24

client

getInstance()alt

Behaviour in Singleton Pattern

:CarMatchOfficeCarMatchOffice()

Inst=CarMatchOffice()

getInstance()

[inst == null]

[else]

getAddress()

When you ask for the instance of CarMatchOffice, there are two ways it can complete. CarMatchOffice

Winter 2007 ACS-3913 Ron McFadyen 25

Collaborations

Collaboration : an arrangement of classes, links, roles in a context to implement some behaviour.

Useful for showing a system’s applications of patterns.

Name of pattern appears in a dashed oval.

Links to classes shown with participation roles.

The value of using collaborations on your class diagram is to show what patterns you have used and the roles that classes and objects will be playing.

Winter 2007 ACS-3913 Ron McFadyen 26

Weather Station Class Diagram

«interface»

Subject

attach()

detach()

notify()

«interface»

Observer

update()

WeatherDataattach()detach()notify()

CurrentConditionsDisplayupdate()display()

*

StatisticsDisplayupdate()display()

ForecastDisplayupdate()display()

«interface»

DisplayElement

display()

Winter 2007 ACS-3913 Ron McFadyen 27

Class Diagram showing Observer Collaboration

«interface»

Subject

attach()

detach()

notify()

«interface»

Observer

update()

WeatherDataattach()detach()notify()

CurrentConditionsDisplayupdate()display()

*

StatisticsDisplayupdate()display()

ForecastDisplayupdate()display()

«interface»

DisplayElement

display()

Observer

subject obse

rver

Con

cret

e su

bjec

t

Concrete observer

Concrete observer

Concrete observer