uml and patterns dr. zhen jiang west chester university e-mail: [email protected] url: zjiang

69
UML and Patterns UML and Patterns Dr. Zhen Jiang West Chester University E-mail: [email protected] url: www.cs.wcupa.edu/~zjiang

Upload: benedict-hutchinson

Post on 31-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

UML and PatternsUML and Patterns

Dr. Zhen JiangWest Chester University

E-mail: [email protected]: www.cs.wcupa.edu/~zjiang

Page 2: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

OutlineOutline

Introduction to UMLObjects and ClassesClass Diagrams

– Class Icon– Relationships– Constraints

Page 3: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

Introduction to UMLIntroduction to UML

What’s UMLGoals of UMLOverview

Page 4: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

UML: Unified Modeling UML: Unified Modeling LanguageLanguage

The Unified Modeling Language (UML) is an industry-standard language for specifying, visualizing, constructing, and documenting the artifacts of software systems

The UML definition was led by Grady Booch, Ivar Jacobson, and Jim Rumbaugh (all now at Rational Software)

Page 5: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

In Essence: a tool used through the analysis and design phases of system development for expressing the constructs and relationships of complex systems

Target Usage: for building object-oriented and component-based systems

UML simplifies the process of software design, making a "blueprint" for construction

Page 6: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

1. Provide users with a ready-to-use, expressive visual modeling language so they can develop and exchange meaningful models

2. Provide extensibility and specialization mechanisms to extend the core concepts.

3. Be independent of particular programming languages and development processes.

4. Provide a formal basis for understanding the modeling language.

5. Encourage the growth of the Object-Oriented tools market.

6. Support higher-level development concepts such as collaborations, frameworks, patterns and components.

7. Integrate best practices.

Goals of UMLGoals of UML

Page 7: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

Many free learning materials on the web– e.g. www.rational.com/uml

Standard of UMLwww.cs.wcupa.edu/~zjiang/01-09-67.pdf

Some recommended texts on UML:– The Unified Modeling Language User Guide, [G.

Booch, J. Rumbaugh, I. Jacobson, 2000]– UML Explained, [Kendall Scott, 2001]– Applying UML and Patterns 2nd Ed., [Craig Larman,

2002]– UML Distilled 2nd Ed., [Martin Fowler with K. Scott,

2000]– UML and C++, [R. Lee & W. Tepfenhart, 2001]

Page 8: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

UML OverviewUML OverviewUML is a language for visualizing,

specifying, constructing and documenting the artifacts of a software system

The artifacts might include requirements, architecture, design, source code, project plans, tests, prototypes, releases

Six diagram types will be introduced: class, relationship, constraint, activity, sequence, statechart

Page 9: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

Objects and ClassesObjects and Classes What’s object

– Identity– State– Behavior

Sequence Diagram Statechart Diagram

Messages and methods What’s class

– Objects and Classes– Nature of a class– Class Attributes– Operation (Method)– Interfaces– Interfaces and Implementation– Corresponding C++ code

Page 10: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

Objects and ClassesObjects and Classes

Fundamentals of Object-Oriented Programming

Page 11: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

ObjectsObjectsConceptually, there are many ways to think

of an object– something that can be seen or touched– a thing to which some action is directed– something that performs an action

The structure and behaviour of similar objects are defined in their common class

Objects have thee properties: identity , state, and behaviour

Page 12: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

Object Property 1: Object Property 1: IdentityIdentityIdentity is that property of an object which

distinguishes it from all other objectsMost programming languages use variable

names to refer to objectsKeep in mind, however, that an object may

not have a name; Similarly, an object might have multiple names (aliases)– For this reason, there is a subtle distinction

made between the concepts of "name" and "identity"

Page 13: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

Object Property 2: StateObject Property 2: StateThe state of an object encompasses all of the

(usually static) properties of the object plus the current (usually dynamic) values of each of these properties

Page 14: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

RequirementsCapture

Production

Implementation

DetailedDesign

ConceptDesign

Decommission

[review]

[review]

[review]

[review]

[Life Update]

[rework]

[rework]

[rework][iterate]

[life end]

Page 15: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

Object Property 3: Object Property 3: BehaviourBehaviourBehaviour is how an object acts and reacts,

in terms of its state changes and message passing

The state of an object represents the cumulative results of its behaviour

In object-oriented programming, a behaviour is invoked by sending a message to an object

If the receiver object does not have a method for that message, an error is reported

Page 16: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

Sequence DiagramsSequence DiagramsA sequence diagram is an interaction

diagram that focuses on the time ordering of messages

A vertical dashed line is used to represent the lifetime of an object (it’s the object’s lifeline)

A focus of control is a tall, thin rectangle that shows the period of time during which an object is performing an action

Page 17: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

: ATC

checkWeather

: Radar

requestLdgClearance(r)

checkRunwayClear(r)clear

clearance

: Pilot

Page 18: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

SequenceSequence

DevelopmentEngineer

Process ProgrammeManager

QualityEngineer

Customer

requirementsreview

report

monitor[monthly]

planprogramme

allocateaward

contract negotiate

discuss

contribute

allocate

capturerequirements

allocate

planquality

claim

review

Page 19: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

Statechart DiagramStatechart Diagram

Captures dynamic behavior (event-oriented)Purpose

– Model object lifecycleModel object lifecycle– Model reactive objects (user interfaces, devices, etc.)Model reactive objects (user interfaces, devices, etc.)

Page 20: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang
Page 21: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

Example ObjectsExample ObjectsThere are many physical objects we can

examine right in this room– each person is an object – any chair is not an object– each light bulb is an object– Any book is not an object– this room itself is an object (full or not)

Page 22: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

See if the followings are objects or not:– Desk– Light– Person– Log– The Earth– Clock– Machine– Computer– Saving account

Answer: N, Y, Y, N, Y, Y, Y, Y, Y

Page 23: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

Messages and MethodsMessages and MethodsA object executes a method when it is sent

a messageFor example, we can ask/tell a dog to sit by

sending him the message "sit"In object-oriented programming, objects are

sent messages asking/telling them to perform behaviours -- the object invokes the method corresponding to the message in order to execute the desired behaviour

Page 24: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

Objects versus ClassesObjects versus ClassesHow would we describe the state,

behaviour, and identity for each of these objects

We have looked at objects and we have seen that objects can be "classified" into classes

As programmers, we work with both classes and objects from those classes

For example, we might write a stack class and create multiple stack objects for use in our program

Page 25: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

Classes and ObjectsClasses and ObjectsAn object is called an "instance" of a classThe terms instance and object are

interchangeableCreating an object from a class is often

called instantiationFor example, there are many person objects

in this room -- each person is an instance of the person class

Page 26: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

The Nature of a ClassThe Nature of a ClassA class describes the common structure

(attributes/state) and behaviour of its instancesFor example,

– 3.14, 2.71, and 5.5 can be classified as Floats

– the following shapes can be classified as Circles

Page 27: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

In a 2D drawing package, circles have a radius, a line thickness, a line colour, and a fill colour

Each individual circle (instance) drawn by the user has its own value for each attribute

The programmer writes a Circle class and the program instantiates a Circle object every time the user draws a Circle

a snowman made from 9 Circle instances

Page 28: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

An attribute is a named property of a class that describes the range of values that instances of the property may hold.(Booch,1999)

An attribute has a type that defines the type of its instances.

Only the object itself should be able to change the value of its attributes.

The values of the attributes define the state of the object

Class AttributesClass Attributes

Page 29: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

Operation (Methods)Operation (Methods) An operation is the implementation of a

service that can be requested from any object of the class to affect behavior (Booch, 1999)

An operation can be:– Question (does not change the value of the

object)– Command (may change the value of the

object)

Page 30: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

InterfacesInterfacesAn interface is a collection of operations

that are used to specify a service of a class or a component (Booch, 1999)

An interface is a contract of related services and a set of conditions that must be true for the contract to be faithfully executed

Interfaces formalize polymorphism, they allow us to define polymorphism in a declarative way unrelated to implementation

Page 31: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

Interface and ImplementationInterface and ImplementationThe class interface is its external (public)

view The class implementation is its internal

(private) viewIt is convenient to think of the interface as

describing "what the objects of this class can do" and the implementation as "how the objects of this class do it"

The implementation of a class consists of all the "behind the scenes" operations defined in the interface of the class

Page 32: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

Objectsomething

action

state1 state2action

attribute1 attribute2

operation/method

attributes: {attribute1, attribute2}

operations/methods

value

structure

ReviewReview

Page 33: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

Light

On OffTurn on/off

True FalseTurn_on

attributes: {True, False}

operations/methods:

value

structure

Turn_off

Turn_on/off ( )

Page 34: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

C++ codeC++ codeclass Light{public:

void turn_on(); // Modifiervoid turn_off(); // ModifierLight(); // Constructor~Light(); // Destructor

private:bool status;

};

Page 35: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

Class heading (class Light) & head file (light.h) Operation implementation (light.cc) & library

(light.o, light.lib) Usage (message/event, interface and its

implementation) & main (classroom.cc)

int main(){Light s1[2]; //Class obj1, obj2;while (1){ Event event; event.receive_message(); //receive task

for (int i = 0 ; i < 2; i ++){ //for all the objs// concerned, interface

if (event.needs_turn_on()) s[i].turn_on (); //interface implement

} //end of one iteration} //end of whilereturn 0;}

Page 36: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

Class DiagramClass DiagramIntroductionClass IconRelationshipsConstraints

Page 37: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

IntroductionIntroductionThe class diagram is fundamental to object-

oriented programmingUML’s class diagrams capture the attributes

and operations of each class as well the relationships that exist between classes

Page 38: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

Class IconClass Icon Class Icon Hiding Details Visibility Notation Attribute Specification Operation Specification

Page 39: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

UML Class IconUML Class Icon The UML class icon is

a rectangle with three compartments:– class name– class attributes– class operations

Attributes are specified in the following form:– object:class name

Circle

radius: float

center_x: int

center_y: int

area()

display()

Page 40: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

Hiding DetailHiding Detail

Circle

area()

display()

Circle

radius: float

center_x: int

center_y: int

Circle

You can optionally leave out the attributes, operations, or both in a class icon:

Page 41: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

UML Member Visibility UML Member Visibility NotationNotation

UML has three visibility prefixes for members:+ for public, # for protected, and – for private

–e.g.Circle

-radius: float

#area()

+display()

Page 42: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

Full UML Attribute Full UML Attribute SpecificationSpecification

The full form of a UML attribute is as follows:[visibility] name [multiplicity] [: type] [= initial value] [{property}]

– The property choices are changeable, addOnly, and frozen

Student Info

-id: string

-hasGraduated: bool = false

Page 43: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

Full UML Operation Full UML Operation SpecificationSpecification

The full form of a UML operation is as follows:[visibility] name [(parameter-list)] [:return-type] [{property}]

– The property choices are sequential, concurrent, guarded, and isQuery

The full form of a UML parameter is:[direction] name : type [= default-value]

– The direction choices are in, out, and inout

Page 44: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

UML Class RelationshipsUML Class RelationshipsA class relationship is a connection between

two (or more) classesThe three most important class relationships

are generalizations, associations, and aggregations

UML provides a graphical representation for each of the relationships using a different line type for each relationship

Page 45: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

Class RelationshipsClass RelationshipsGeneralizationAssociationAssociation ClassQualified AssociationTernary AssociationAggregation

Page 46: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

GeneralizationGeneralization A generalization is a relationship between a general

thing (superclass) and a more specific kind of that thing (subclass)

In the UML, generalization requires that objects of the subclass may be used anywhere an object of the superclass appears

Person

Student

Page 47: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

GeneralizationGeneralizationClass Person{public:

Person( long ); // Constructor~Person(); // Destructor

void show_id();private:

long ssn; // social security number};

Class Student : public Person{public:

Student( long, long );~Student();void show_info();

private:long sn; // student number

};

Page 48: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

GeneralizationGeneralizationPerson::Person (long number){ ssn=number;}

Student::Student (long snumer, long ssnumber):Person(ssnumber){ sn=snumber;}

void Student:: show_info (){ Person::show_id(); cout << “; student number is “ << sn <<endl;}

void Person:: show_id (){ cout << “Social security number is “ << ssn <<endl;}

Page 49: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

GeneralizationGeneralizationint main (){Person a(900010034);a.show_id();

Student b(123456789, 654321);b.show_info();

}

Page 50: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

GeneralizationGeneralizationClass Person{public:

Person( long ); // Constructor~Person(); // Destructor

void show_id();protected:

long ssn; // social security number};

Class Student : public Person{public:

Student( long, long );~Student();void show_info();

private:long sn; // student number

};

Page 51: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

GeneralizationGeneralization

void Student:: show_info (){ cout << “Social security number is “ << ssn <<endl; cout << “; student number is “ << sn <<endl;}

Page 52: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

GeneralizationGeneralizationClass Person{public:

Person( long ); // Constructor~Person(); // Destructor

virtual void show_info()=0;protected:

long ssn; // social security number};

Class Student : public Person{public:

Student( long, long );~Student();void show_info();

private:long sn; // student number

};

Page 53: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

GeneralizationGeneralizationvoid Student:: show_info (){ cout << “Social security number is “ << ssn <<endl; cout << “; student number is “ << sn <<endl;}

Page 54: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

AssociationAssociationAn association is a structural relationship that

specifies that objects of one thing are connected to objects of another

Faculty Student

Page 55: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

AssociationAssociationClass Student{

// other declarations as beforeprivate:

Faculty * teacher;friend Faculty::add_new_student( Student *);

friend Faculty::remove_from_list ( Student *);public:

Faculty * get_teacher () {return teacher;}};

Class Faculty : public Student //it’s not generalization{

// other declarations as beforeprivate:

Student ** students;public:

void add_new_student ( Student *);void remove_from_list( Student *);

Student ** get_students () {return students;}};

Page 56: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

AssociationAssociationvoid Faculty:: add_new_student ( Student * s){ s->teacher = this; // add s to students;}

void Faculty:: remove_from_list (Student * s){ s->teacher = 0; // remove s from students;}

Page 57: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

AssociationAssociationAssociations can be adorned with a name.

Faculty StudentTeaching

Page 58: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

AssociationAssociationAssociations can be adorned with the roles.

teacherFaculty Studentlearner

Page 59: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

AssociationAssociationAssociations can be adorned with the multiplicity.

4..*Faculty Student

1

Page 60: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

Association Class (relation Association Class (relation attributes)attributes)

Each object of association class is one instance of relationship (link) in an association.

1..*Faculty Student4..*

Course

Page 61: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

Qualified AssociationQualified AssociationQualified association relates two classes and

a qualifier. The qualifier is a special attribute that

reduces the effective multiplicity of an association.

Directory Filefile name

Page 62: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

Ternary AssociationTernary Association A ternary association is a structural relationship that

specifies that objects of one thing are connected to objects of other two’s.

Developer

LanguageProject

Page 63: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

AggregationAggregationAn aggregation is an association that

represents whole/part relationshipThe “whole” end of the association

relationship is adorned with an open diamond shape (e.g. X is part of Y)

e.g. door:Door is part of car:Car

X Y

Page 64: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

Generalization, Aggregation, and Generalization, Aggregation, and AssociationAssociation

See if the following pairs of classes have generalization, aggregation or association Faculty & student (as) Hospital & doctor (as) Door & Car (ag --<>) Member & Organization (ag --<>) People & student (ge <|-- ) Circle & point (ge --|>) Department & Faculty (as) Employee & Faculty (ge <|--) Item & Printer (ge <|--) Account & Checking account (ge <|-- )

Page 65: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

ConstraintsConstraintsConstraints on ObjectConstraints on Relations (Ordering)General Constraints (Dependency)

Page 66: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

Constraints on ObjectConstraints on ObjectThe constraints restricts the values that objects can be.Example: No employee’s salary can exceed the salary of the employee’s boss.

Employee

Salary

Employer

Salary

{salary <=boss.salary}

Page 67: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

Constraints on Relations Constraints on Relations (Ordering)(Ordering)

{Order} indicates that the elements of the “many” end of an association have an explicit order that must be preserved.

Paper Author{ordered}

11..*

Page 68: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

General Constraints General Constraints (Dependency)(Dependency)

A dependency is a using relationship that states that a change in specification of one thing may affect another thing that uses it (but not necessarily the reverse)

Page 69: UML and Patterns Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: zjiang

General Constraints General Constraints (Dependency)(Dependency)

Aerodrome

checkRVR( r: Runway ) Runway

Dependencies are often used when one class uses another class as an argument of the operation

Dependencies are also often used to express general constraints.

Person Committeemember-of

chair-of

{subset}