1 Class Diagrams: Advanced Concepts. 2 Overview Class diagrams are the most commonly used diagrams...

21
1 Class Diagrams: Class Diagrams: Advanced Concepts Advanced Concepts

Transcript of 1 Class Diagrams: Advanced Concepts. 2 Overview Class diagrams are the most commonly used diagrams...

1

Class Diagrams:Class Diagrams:Advanced ConceptsAdvanced Concepts

2

Overview

Class diagrams are the most commonly used Class diagrams are the most commonly used diagrams in UML.diagrams in UML.

Class diagrams are for visualizing, specifying and Class diagrams are for visualizing, specifying and documenting the system from a static perspective.documenting the system from a static perspective.

Class diagrams indicate which classes know about Class diagrams indicate which classes know about other classes and, if they do, what type of other classes and, if they do, what type of relationship exists.relationship exists.

Class diagrams will have different levels of detail Class diagrams will have different levels of detail (abstraction) depending on where we are in the (abstraction) depending on where we are in the software development process.software development process.

3

Overview

4

Static Operations and Attributes

ScopeScope Individual member data (attributes) may have Individual member data (attributes) may have

either either classclass scope or scope or instanceinstance scope. scope. Class scope - A single copy of an attribute is Class scope - A single copy of an attribute is

shared by all instances of a class. shared by all instances of a class. In UML you underline the attribute to indicate class In UML you underline the attribute to indicate class

scope: scope: productCount : intproductCount : int In C++ and Java members with class scope would In C++ and Java members with class scope would

be declared as static: static int productCount be declared as static: static int productCount Instance scope - Each instance of a class Instance scope - Each instance of a class

would have its own copy of the attribute. All would have its own copy of the attribute. All attributes have instance scope by default.attributes have instance scope by default.

5

Static Operations and Attributes

Static attributes have class scopeStatic features are underlined on a class diagramItalicized class name means abstract class.

6

Aggregate and Composition

Aggregation is …Aggregation is … A relationship between two classes.A relationship between two classes. A form of association.A form of association. Used to show a ‘has-a’ or a ‘whole-part’ Used to show a ‘has-a’ or a ‘whole-part’

relationship.relationship. Graphically represented as a solid line with an Graphically represented as a solid line with an

open diamond on the ‘whole’ end.open diamond on the ‘whole’ end. Aggregations and associations are Aggregations and associations are

implemented in exactly the same way. implemented in exactly the same way. The difference is entirely conceptual.The difference is entirely conceptual.

7

Aggregate and Composition

Composition is …Composition is … A relationship between two classes.A relationship between two classes. Used to show a ‘contains-a’ or a ‘whole-part’ Used to show a ‘contains-a’ or a ‘whole-part’

relationship.relationship. Graphically represented as a solid line with a solid Graphically represented as a solid line with a solid

diamond on the ‘whole’ end.diamond on the ‘whole’ end. Composition relationships and associations Composition relationships and associations

are are notnot implemented in exactly the same way. implemented in exactly the same way. If the ‘whole’ object is destroyed, the part If the ‘whole’ object is destroyed, the part

object will also be destroyed. object will also be destroyed.

8

Aggregate and Composition

Composition is …Composition is … A relationship between two classes.A relationship between two classes. Used to show a ‘contains-a’ or a ‘whole-part’ Used to show a ‘contains-a’ or a ‘whole-part’

relationship.relationship. Graphically represented as a solid line with a solid Graphically represented as a solid line with a solid

diamond on the ‘whole’ end.diamond on the ‘whole’ end. Composition relationships and associations Composition relationships and associations

are are notnot implemented in exactly the same way. implemented in exactly the same way. If the ‘whole’ object is destroyed, the part If the ‘whole’ object is destroyed, the part

object will also be destroyed. object will also be destroyed.

9

Aggregate and Composition

10

Aggregate and Composition

11

Common Modeling Technique

Modeling dependency, association, Modeling dependency, association, aggregation and composition aggregation and composition relationships.relationships. It is not always easy to determine which is the It is not always easy to determine which is the

best relationship to use. It all has to do with best relationship to use. It all has to do with your perspective of the problem domain.your perspective of the problem domain.

There is not always one best solution.There is not always one best solution. Personal experience is your most valuable Personal experience is your most valuable

modeling tool.modeling tool. CRC cards are helpful.CRC cards are helpful.

12

Common Modeling Technique

Multiple class diagrams are required to Multiple class diagrams are required to model large systems.model large systems.

Each individual class diagram …Each individual class diagram … Shows a single aspect of the system.Shows a single aspect of the system. Contains only elements that are essential to Contains only elements that are essential to

understanding that aspect.understanding that aspect. Provide details consistent with its level of Provide details consistent with its level of

abstraction.abstraction. Uses meaningful class and member names.Uses meaningful class and member names.

Pointers to other classes are modeled as Pointers to other classes are modeled as associations.associations.

13

Common Modeling Technique

A well-defined class is loosely coupled (few entry A well-defined class is loosely coupled (few entry points) and highly cohesive (all members work points) and highly cohesive (all members work toward a common functionality).toward a common functionality).

Ask yourself “Am I trying to show Ask yourself “Am I trying to show what what the class the class does or does or howhow it does it”. That will tell you at it does it”. That will tell you at what level of abstraction to model the class.what level of abstraction to model the class.

In the In the requirements requirements andand specification specification phase you phase you are interested in “what”. In the are interested in “what”. In the design design phase phase you are interested in “how”.you are interested in “how”.

Don’t hesitate to attach Don’t hesitate to attach notesnotes to the class icons if to the class icons if further clarification is necessary.further clarification is necessary.

14

Interfaces and Abstract Interfaces and Abstract ClassesClasses

Abstract ClassAbstract Class A A abstract classabstract class cannot have any direct instances. cannot have any direct instances. Not all OO programming languages directly support Not all OO programming languages directly support

abstract classes. (In C++ abstract classes contain one abstract classes. (In C++ abstract classes contain one or more pure virtual functions).or more pure virtual functions).

int myFunction(int x) = 0;int myFunction(int x) = 0; Pure virtual functions have no function body.Pure virtual functions have no function body.

An abstract class is thought to be so general as to be An abstract class is thought to be so general as to be useless by itself.useless by itself.

Abstract classes only occur in the context of an Abstract classes only occur in the context of an inheritance hierarchy. inheritance hierarchy.

In UML you specify that a class is abstract by writing In UML you specify that a class is abstract by writing its name in italics. its name in italics.

15

Interfaces and Abstract Interfaces and Abstract ClassesClasses

An interface is a class that has no implementation.

All of its features are abstractCorresponds directly to interfaces in Java

16

Association ClassAssociation Class

An An association classassociation class…… is used to model an association as a class. is used to model an association as a class. is rendered by a dashed line from the is rendered by a dashed line from the

association to the class rectangle.association to the class rectangle. is essentially a class attached to an is essentially a class attached to an

association; the association itself is modeled association; the association itself is modeled as a class.as a class.

Each link in the association is an object of Each link in the association is an object of the association class. the association class. A A linklink is an instance of an is an instance of an associationassociation

a a linklink is to an is to an associationassociation as an as an objectobject is to a is to a classclass

17

Association ClassAssociation Class

Things to consider:Things to consider: You can’t attach the same class to more than You can’t attach the same class to more than

one association; an association class is the one association; an association class is the association.association.

The name of the association is usually omitted The name of the association is usually omitted since it is considered to be the same as that of since it is considered to be the same as that of the attached class.the attached class.

Distinguish between the use of an association Distinguish between the use of an association class as a modeling technique and the class as a modeling technique and the implementation of the association class. There implementation of the association class. There can be several ways to implement an can be several ways to implement an association class.association class.

18

Association ClassAssociation Class

19

Association ClassAssociation Class

20

Association ClassAssociation Class

21

VisibilityVisibility VisibilityVisibility

Class members (attributes and behaviors) may be Class members (attributes and behaviors) may be specified as public (+), private (-), or protected (#).specified as public (+), private (-), or protected (#).

In UML the single character visibility indicator is In UML the single character visibility indicator is placed to the left of the member.placed to the left of the member.

# myProtectedFunction( )# myProtectedFunction( ) - myPrivateFunction( )- myPrivateFunction( ) + myPublicFunction( )+ myPublicFunction( )

Restricting visibility is the same as restricting Restricting visibility is the same as restricting accessibility. By restring accessibility you are accessibility. By restring accessibility you are limiting the number of entry points into an object. limiting the number of entry points into an object. By restricting the number of entry points you are By restricting the number of entry points you are facilitating debugging and code re-use.facilitating debugging and code re-use.