42
1 Object-oriented design Part 2: intro to UML

Object-oriented design tools & UML Intro to UML • UML: unified modeling language • System for graphically representing & manipulating an object -oriented software system • Both

Embed Size (px)

Citation preview

Page 1: Object-oriented design tools & UML Intro to UML • UML: unified modeling language • System for graphically representing & manipulating an object -oriented software system • Both

1

Object-oriented design

Part 2: intro to UML

Page 2: Object-oriented design tools & UML Intro to UML • UML: unified modeling language • System for graphically representing & manipulating an object -oriented software system • Both

2

Intro to UML

• UML: unified modeling language • System for graphically representing &

manipulating an object-oriented software system

• Both a representation of design & tool to assist in design process

Page 3: Object-oriented design tools & UML Intro to UML • UML: unified modeling language • System for graphically representing & manipulating an object -oriented software system • Both

3

Class Diagram

• Detail diagrams: – 3 parts: class name, attributes, methods -

generally listed in that order – Don’t have to list all attributes or methods -

usually just the most important • Can use abbreviated diagrams for depicting

relationships among classes: omit all but the class name

Page 4: Object-oriented design tools & UML Intro to UML • UML: unified modeling language • System for graphically representing & manipulating an object -oriented software system • Both

4

Attributes

• Attributes generally correspond to data members of a class

• Can include the following information in a class diagram: – access designation:

+ means public - means private # means protected

– name – data type

Page 5: Object-oriented design tools & UML Intro to UML • UML: unified modeling language • System for graphically representing & manipulating an object -oriented software system • Both

5

Methods in Class Diagram

• Constructor – same name as class – has no return type & no access designation

• Other methods – access designation (+, - or #) – method name – parameter list, if needed - in parentheses – return type after colon

Page 6: Object-oriented design tools & UML Intro to UML • UML: unified modeling language • System for graphically representing & manipulating an object -oriented software system • Both

Methods

• The general syntax for a method’s signature in a class diagram is: X methodName: returnType(parameter(s)) Where: – X is either +, - or # (or nothing, if constructor) – returnType and parameter(s) represent data category,

but not necessarily an actual data type (since UML is not language-specific); void is still used to mean no return value

– If there are no parameters, omit the () 6

Page 7: Object-oriented design tools & UML Intro to UML • UML: unified modeling language • System for graphically representing & manipulating an object -oriented software system • Both

7

Example: musical Note

Class name

} Attributes

Methods

Page 8: Object-oriented design tools & UML Intro to UML • UML: unified modeling language • System for graphically representing & manipulating an object -oriented software system • Both

8

Example from ATM

Page 9: Object-oriented design tools & UML Intro to UML • UML: unified modeling language • System for graphically representing & manipulating an object -oriented software system • Both

9

Depicting class relationships

• Relationships between classes are represented by lines between (abbreviated) class diagrams

• Line type (solid vs. dotted) and arrowhead type distinguish between various kinds of relationships

Page 10: Object-oriented design tools & UML Intro to UML • UML: unified modeling language • System for graphically representing & manipulating an object -oriented software system • Both

10

Aggregation

• Aggregation is used when a class is made up of class components – Acar has an engine, an electrical system, etc. – A musical score has notes, time signatures, dynamics,

etc. • Examples of things that are not aggregations:

– A store has customers, but doesn’t own them – A student has a list of classes, but isn’t made up of

them

Page 11: Object-oriented design tools & UML Intro to UML • UML: unified modeling language • System for graphically representing & manipulating an object -oriented software system • Both

11

Aggregation & UML

• Aggregations are represented with diamond-headed lines connecting an aggregate class to its components

• The diamond is at the aggregate end • Can use multiplicity notation to depict the

number of instances of each component

Page 12: Object-oriented design tools & UML Intro to UML • UML: unified modeling language • System for graphically representing & manipulating an object -oriented software system • Both

12

ATM Example Although we didn’t choose to model it this way, an ATM can be thought of as an aggregate of several things, for example, display device, keypad, deposit slot, receipt printer and bank card reader

Page 13: Object-oriented design tools & UML Intro to UML • UML: unified modeling language • System for graphically representing & manipulating an object -oriented software system • Both

13

Multiplicity notation

• A number or symbol near either end point of a connecting line indicates multiplicity

• Common notations include: – 0 or more: * – 1 or more: 1 .. * – 0 or 1: 0 .. 1 – exactly 1: 1

Page 14: Object-oriented design tools & UML Intro to UML • UML: unified modeling language • System for graphically representing & manipulating an object -oriented software system • Both

14

Multiplicity examples

From voice mail system: a message queue can contain several messages

JukeBox: A SongList (songs picked by an individual user) can have 1-4 songs, depending on the amount of money deposited; the PlayList consists of all the SongLists queued up as other users’ songs are played

Page 15: Object-oriented design tools & UML Intro to UML • UML: unified modeling language • System for graphically representing & manipulating an object -oriented software system • Both

Aggregation vs. Association

15

• Aggregation is sometimes subdivided into the categories of true aggregation (where a thing is the sum of its (independent) parts) and composition, where the parts don’t have an independent existence

• An association diagram is a more general depiction of the relationship between classes

Page 16: Object-oriented design tools & UML Intro to UML • UML: unified modeling language • System for graphically representing & manipulating an object -oriented software system • Both

16

Associations

• Represent services provided between classes - a.k.a. collaborations

• Association is represented by a line between client & server class diagrams

• There is no indication of direction of flow of service

Page 17: Object-oriented design tools & UML Intro to UML • UML: unified modeling language • System for graphically representing & manipulating an object -oriented software system • Both

17

Associations

• Can add role designations to lines in association

• Helps clarify bidirectional relationship before a final decision is made about which class actually manages the pertinent information

Page 18: Object-oriented design tools & UML Intro to UML • UML: unified modeling language • System for graphically representing & manipulating an object -oriented software system • Both

18

Association Example from ATM

Each line represents a collaboration:

• ATM collaborates with transaction in fulfillment of ATM responsibility: create & initiate transactions • Transaction collaborates with Account in fulfillment of transaction responsibility: commit transaction to database • Various transaction types collaborate with Account to perform their responsibilities

Page 19: Object-oriented design tools & UML Intro to UML • UML: unified modeling language • System for graphically representing & manipulating an object -oriented software system • Both

19

Notes on Graph Complexity

• A diagram needs to depict system, but also needs to be readable

• All collaborations could be represented on a single association graph, but in a complex system the diagram would be so complicated as to be unusable

Page 20: Object-oriented design tools & UML Intro to UML • UML: unified modeling language • System for graphically representing & manipulating an object -oriented software system • Both

20

Notes on Graph Complexity

• Best approach is to create multiple diagrams that represent collaborations in fulfillment of a set of related responsibilities

• Previous example represented all collaborations in support of 3 contracts

Page 21: Object-oriented design tools & UML Intro to UML • UML: unified modeling language • System for graphically representing & manipulating an object -oriented software system • Both

21

Inheritance

• UML represents inheritance relationships between classes as a line beginning from a subclass diagram and ending in an arrow pointing to the superclass

• Abstract classes are represented using {} around their names

Page 22: Object-oriented design tools & UML Intro to UML • UML: unified modeling language • System for graphically representing & manipulating an object -oriented software system • Both

22

Example from ATM Design

Page 23: Object-oriented design tools & UML Intro to UML • UML: unified modeling language • System for graphically representing & manipulating an object -oriented software system • Both

Interfaces

• An interface is a language construct specific to Java

• Java does not support multiple inheritance • Interfaces provide some of the same

functionality – A Java class can only inherit from one class – But can implement many interfaces

Page 24: Object-oriented design tools & UML Intro to UML • UML: unified modeling language • System for graphically representing & manipulating an object -oriented software system • Both

ATM Example

This hierarchy chart represents the ATM device classes as envisioned by the preliminary design Each of the concrete subclasses represents an actual physical device; the abstract classes are used to abstract their common behavior The design calls for Bank Card Reader and Receipt Printer to inherit from multiple superclasses, but this can’t be done in Java

Page 25: Object-oriented design tools & UML Intro to UML • UML: unified modeling language • System for graphically representing & manipulating an object -oriented software system • Both

Using Interfaces

• Instead of redoing the design, can make the abstract classes into interfaces

• An interface specifies, but does not implement, specific methods; the implementation is left up to the subclasses that “inherit” the interface

Page 26: Object-oriented design tools & UML Intro to UML • UML: unified modeling language • System for graphically representing & manipulating an object -oriented software system • Both

Inheritance vs. Interfaces

• We can think of “real” inheritance as implementation inheritance – Subclasses inherit and may extend functionality

of superclass – Superclasses pass on structure and behavior

Page 27: Object-oriented design tools & UML Intro to UML • UML: unified modeling language • System for graphically representing & manipulating an object -oriented software system • Both

Inheritance vs. Interfaces

• Interfaces provide only definition inheritance – Methods may be specified, but there is no

superclass implementation – Instead, the subclasses define what is specified

in the interface – Interfaces pass on structure, but not behavior

Page 28: Object-oriented design tools & UML Intro to UML • UML: unified modeling language • System for graphically representing & manipulating an object -oriented software system • Both

Interfaces & UML

• Interface relationships look like inheritance relationships, except a dashed line is used for connecting subclasses to superclasses

• Each interface’s name is enclosed in a double set of angle brackets: << >>

Page 29: Object-oriented design tools & UML Intro to UML • UML: unified modeling language • System for graphically representing & manipulating an object -oriented software system • Both

ATM Example

Page 30: Object-oriented design tools & UML Intro to UML • UML: unified modeling language • System for graphically representing & manipulating an object -oriented software system • Both

Dependency

• Dashed lines with closed arrowheads, as shown in the previous diagram, indicate definition inheritance via interfaces

• A dashed line with an open arrowhead simply indicates a dependency - that is, one class depending on another to fulfill a responsibility

Page 31: Object-oriented design tools & UML Intro to UML • UML: unified modeling language • System for graphically representing & manipulating an object -oriented software system • Both

Dependency example Several slides ago, this diagram appeared:

This is an association diagram, but the names strongly suggest a dependency:

Page 32: Object-oriented design tools & UML Intro to UML • UML: unified modeling language • System for graphically representing & manipulating an object -oriented software system • Both

Sequence Diagrams

• Sequence diagrams describe the interaction of classes as their methods are called

• Where class diagrams provide a static picture of class relationships (basically just telling us that a relationship exists), sequence diagrams provide a dynamic picture of a system in action

• Time is represented by the vertical dimension; the further down in the diagram, the later a method is invoked

Page 33: Object-oriented design tools & UML Intro to UML • UML: unified modeling language • System for graphically representing & manipulating an object -oriented software system • Both

Sequence diagram: example 1

aMailbox is the client object - calls newMessages.add(message)

Page 34: Object-oriented design tools & UML Intro to UML • UML: unified modeling language • System for graphically representing & manipulating an object -oriented software system • Both

Elements of a sequence diagram

• Rectangles representing classes and objects – object names are underlined, and may appear in

several forms: • full description: objectname: Classname • no class specified: objectname • class specified without object name; read as “some

instance of this class”: : Classname – class names are not underlined

• Method names and other text

Page 35: Object-oriented design tools & UML Intro to UML • UML: unified modeling language • System for graphically representing & manipulating an object -oriented software system • Both

Elements of a sequence diagram • Lifelines: dashed vertical lines

representing the period of time during which an object exists (can be left indeterminate)

• Activation bars: rectangles laid over lifelines, representing a method’s execution in process

• Call arrows: horizontal arrows pointing from the calling object or class to the object or class that owns the called method

Page 36: Object-oriented design tools & UML Intro to UML • UML: unified modeling language • System for graphically representing & manipulating an object -oriented software system • Both

Elements of a sequence diagram

• Data tokens: short lines with an arrowhead on one end and a circle on the other, representing data items flowing between objects – Tokens pointing to the right represent arguments passed

to methods – Tokens pointing the left represent return values

Page 37: Object-oriented design tools & UML Intro to UML • UML: unified modeling language • System for graphically representing & manipulating an object -oriented software system • Both

Sequence diagram: example 2

This diagram represents a self call: the unnamed MailSystem object is calling its own locateMailbox method

Page 38: Object-oriented design tools & UML Intro to UML • UML: unified modeling language • System for graphically representing & manipulating an object -oriented software system • Both

Sequence diagram: example 3

In this diagram, a MailSystem object calls the constructor for a Mailbox object. The <<create>> stereotype indicates the constructor call; note that the Mailbox object is slightly below the MailSystem object, because its lifeline doesn’t start until MailSystem creates it

Page 39: Object-oriented design tools & UML Intro to UML • UML: unified modeling language • System for graphically representing & manipulating an object -oriented software system • Both

Sequence diagram: example 4 • User pressing key

spawns call to dial() • Connection object must

find Mailbox object: Calls findMailbox on MailSystem object

• Connection wants to get greeting to play: Each Mailbox knows its greeting

• Note that connection holds on to that mailbox over multiple calls

Note: parameters and return values are not displayed in this diagram; may be omitted to reduce complexity

Page 40: Object-oriented design tools & UML Intro to UML • UML: unified modeling language • System for graphically representing & manipulating an object -oriented software system • Both

State diagrams

• Some objects have a discrete set of states that affect their behavior; this is particularly true of objects that interact with the user

• A state diagram shows the various states of a system while a particular process is carried out

• State diagrams can be used to illustrate variations in a use case

Page 41: Object-oriented design tools & UML Intro to UML • UML: unified modeling language • System for graphically representing & manipulating an object -oriented software system • Both

State diagram example - ATM

Page 42: Object-oriented design tools & UML Intro to UML • UML: unified modeling language • System for graphically representing & manipulating an object -oriented software system • Both

State diagram example - voice mail system

• Rounded rectangles represent the various states of the system (from user’s point of view)

• Arrows show transitions between states – text labels indicate actions that move system from state to state