Object Oriented Analysis and Design Class and Object Diagrams.

43
Object Oriented Analysis and Design Class and Object Diagrams

Transcript of Object Oriented Analysis and Design Class and Object Diagrams.

Page 1: Object Oriented Analysis and Design Class and Object Diagrams.

Object Oriented Analysis and Design

Class and Object Diagrams

Page 2: Object Oriented Analysis and Design Class and Object Diagrams.

Contents

2

Class Diagrams Object diagrams

Page 3: Object Oriented Analysis and Design Class and Object Diagrams.

Class Diagrams

3

Shows the relationships between classes We have several types of relationships

A class implements an interface A class inherits from another class A class is composed of another class A class is an aggregate of another class

Page 4: Object Oriented Analysis and Design Class and Object Diagrams.

Sample Class Diagram

4

implementscomposition

inheritance

aggregation

Page 5: Object Oriented Analysis and Design Class and Object Diagrams.

Why Use Class Diagrams?

5

When classes are defined as text You must read the text to find the associations You must remember all the associations You can then assimilate all the class relationships

With Diagrams The associations are shown as lines They are obvious and understood more quickly As a result, you understand the program more

quickly

Page 6: Object Oriented Analysis and Design Class and Object Diagrams.

Uses of Class Diagrams

6

As a notation for program design alternatives As a way to capture the design of a program As a way of explaining how a program works As a way for programmers to discuss a design As part of the documentation for a program

Page 7: Object Oriented Analysis and Design Class and Object Diagrams.

Modeling Classes

7

Select a class name Define the attributes Define the methods

Page 8: Object Oriented Analysis and Design Class and Object Diagrams.

Method Categories

8

Constructors – create objects Destructors – destroy objects Accessors – return all or part of object

state Modifiers – change all of part of object

state Iterators – visit objects in a collection of

objects

Page 9: Object Oriented Analysis and Design Class and Object Diagrams.

Defining Methods

9

Define Accessors to get and/or set private variables Methods necessary to manipulate the class Methods which are convenient to manipulate

the class Keep the number of methods to a

minimum offer convenience methods only when

warrented

Page 10: Object Oriented Analysis and Design Class and Object Diagrams.

Associations

10

An association indicates a structural relationship between two classes

Normally, this means that one class has-a instance of the other class

You can specify various amounts of information about an association

The most general association is represented as a line between two classes

Page 11: Object Oriented Analysis and Design Class and Object Diagrams.

Associations

11

This is a bidirectional association that indicates A car has an engine, And an engine has a car

Page 12: Object Oriented Analysis and Design Class and Object Diagrams.

Directed Associations

12

A directed association indicates that the association is only in one direction

We can use a directed association to indicate that a car has an engine, but an engine does not have a car

This is indicated by adding an arrow head

Page 13: Object Oriented Analysis and Design Class and Object Diagrams.

Roles

13

Each entity in an association can play a role You can label none, either, or both roles For example, an engine acts in the role of

the power source for a car

Page 14: Object Oriented Analysis and Design Class and Object Diagrams.

Multiplicity

14

Sometimes the relationship between two entities is not 1 to 1 For example, a car has 4 wheels This is indicated by placing numbers at the ends of the

associations

Page 15: Object Oriented Analysis and Design Class and Object Diagrams.

Multiplicity

15

In addition to a fixed number you can have 1 - single instance 1 .. * - one or more instances 0 .. 1 - zero or one instance * - zero or more instances

You can label both ends of the association with their multiplicity if desired

Page 16: Object Oriented Analysis and Design Class and Object Diagrams.

Multiplicity

16

When the multiplicity is greater than one, it indicates the use of a collection to hold the members

A collection of members can be ordered or unordered If ordered, this annotation is added

Page 17: Object Oriented Analysis and Design Class and Object Diagrams.

Labeling Associations

17

You can label an association to Provide additional information about the association Give it a name for convenience

Page 18: Object Oriented Analysis and Design Class and Object Diagrams.

Labeling Associations

18

An association name can have an optional arrow indicating the direction of the association

Person Carowns

Page 19: Object Oriented Analysis and Design Class and Object Diagrams.

Association Roles

19

The ends of an association can also be labeled with the role one or both participants plays in the association

Employee ProjectParticipates in

programmer

Role played by

employee

Page 20: Object Oriented Analysis and Design Class and Object Diagrams.

Qualified Associations

20

An association can be qualified to show how one object can access another

This usually involves specifying how one object can index or locate another

We use a rectangle on the association to indicate this

Customer

Order

-orderNum: integer-quantity:integer = 0

orderNum:integerplaces

Page 21: Object Oriented Analysis and Design Class and Object Diagrams.

Composition

21

Composition is a special type of association that indicates a part-of or has-a relationship

By using composition, we are specifying that one object is physically contained in the other

For example, the bank account and the customer are related by composition since the account has-a owner

The symbol for composition is an solid diamond at the composite end of the association line

Page 22: Object Oriented Analysis and Design Class and Object Diagrams.

Composition

22

Page 23: Object Oriented Analysis and Design Class and Object Diagrams.

Composition

23

Account

Customer

Branch

Composition indicates physical containment

Page 24: Object Oriented Analysis and Design Class and Object Diagrams.

Aggregation

24

Aggregation is like composition, but one object is not physically contained in the other

It is represented as an empty diamond

Page 25: Object Oriented Analysis and Design Class and Object Diagrams.

Aggregation

25

Account Customer

Branch

In aggregation, the objects in the aggregate are not physically contained, butare external to the aggregate and referenced by the aggregate

Page 26: Object Oriented Analysis and Design Class and Object Diagrams.

Inheritance

26

Inheritance is one of the key features of the object-oriented approach

This allows us to take a class and create a specialized subtype of the class

Specialization is usually done by adding attributes to the parent class

A specialized class is often called a derived class

The class from which it is derived is the parent class or base class

Page 27: Object Oriented Analysis and Design Class and Object Diagrams.

Inheritance

27

A derived class inherits The attributes of the base class The methods of the base class

A derived class adds Attributes to represent its special information Methods to handle the new attributes and

capabilities

Page 28: Object Oriented Analysis and Design Class and Object Diagrams.

Inheritance

28

We can create a special type of account – the savings account

This has everything an account has, plus more

An empty arrow head is used to indicate inheritance

Page 29: Object Oriented Analysis and Design Class and Object Diagrams.

Inheritance

29

Several classes can be derived from the same base class They each inherit the same attributes and methods of

the base class

Page 30: Object Oriented Analysis and Design Class and Object Diagrams.

Generalization

30

Generalization is the opposite of specialization

It allows you to take several classes, abstract what they have in common, Move this to a superclass

This has the advantage that the declarations for the common attributes and methods only need to be done once

This is one example of code reuse, although a minor one

Page 31: Object Oriented Analysis and Design Class and Object Diagrams.

Abstract Classes

31

During the process of class design we sometimes create a superclass that can never be instantiated

Such classes are called abstract classes Abstract classes exist purely to act as base

classes The names of abstract classes are italicized

Page 32: Object Oriented Analysis and Design Class and Object Diagrams.

Abstract Classes

32

Banks have chequing accounts and savings accounts They do not just have plain accounts Therefore, we can make the Account class abstract

Page 33: Object Oriented Analysis and Design Class and Object Diagrams.

Visibility

33

Visibility determines where the members of a class can be seen

Visibility is indicated by a symbol in front of the member name

+ Public Visible to all classes

# Protected

Visible to derived classes

~ Package Visible only with the package

- Private Visible only within the class

Page 34: Object Oriented Analysis and Design Class and Object Diagrams.

Attribute Type

34

Denotes the type of data that will be stored in this attribute

Analysis – How the user sees the data Design – represents the data type used in the

specific programming language Example

String Integer

Page 35: Object Oriented Analysis and Design Class and Object Diagrams.

Attribute Initial Value

35

An optional initial value can be assigned to an attribute Balance:float = 0.0

Page 36: Object Oriented Analysis and Design Class and Object Diagrams.

Derived Attribute

36

A slash “/” preceding the name of the attribute indicates that it is a derived attribute that comes from the parent class

Page 37: Object Oriented Analysis and Design Class and Object Diagrams.

Class Variables

37

A class variable is a single variable shared among all instances of the class

All members of the class can access the variable A class variable can have any degree of visibility Class variables are like globals, but are not in the

global name space You can also have class level methods which can

be invoked on the class, rather than an object Class-level objects have their names underlined

Page 38: Object Oriented Analysis and Design Class and Object Diagrams.

Object Diagrams

38

An object diagram shows several objects and their associations

This shows a snapshot of how objects are connected in an actual system

This is a static snapshot at a specific point in time Object diagrams are useful to explain how the

classes work together by showing an example of their use

The object symbols are represented with a name and class name

Page 39: Object Oriented Analysis and Design Class and Object Diagrams.

Object Diagrams

39

Page 40: Object Oriented Analysis and Design Class and Object Diagrams.

Using Object Diagrams

40

Object diagrams can be used for Sketching the relationship between objects to help

discover classes As examples of how classes can be combined To show the relationship of objects before and

after an operation

Page 41: Object Oriented Analysis and Design Class and Object Diagrams.

Object Notation

41

Object format is Object Name : Class Name : Class Name

Indicates an anonymous object

Attributes have actual values Address = 123 Anywhere St.

Operations are omitted

Page 42: Object Oriented Analysis and Design Class and Object Diagrams.

Additional Object Notation

42

The name of an object can be omitted if it is not needed

You can add the values of key variables if this is important to the example

Page 43: Object Oriented Analysis and Design Class and Object Diagrams.

Class/Object Diagram Differences

43