object oriented analysis and design class and object diagrams

Post on 14-Dec-2015

229 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Object Oriented Analysis and Design

Class and Object Diagrams

Contents

2

Class Diagrams 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

Sample Class Diagram

4

implementscomposition

inheritance

aggregation

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

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

Modeling Classes

7

Select a class name Define the attributes Define the methods

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

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

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

Associations

11

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

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

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

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

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

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

Labeling Associations

17

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

Labeling Associations

18

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

Person Carowns

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

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

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

Composition

22

Composition

23

Account

Customer

Branch

Composition indicates physical containment

Aggregation

24

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

It is represented as an empty diamond

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

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

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

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

Inheritance

29

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

the base class

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

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

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

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

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

Attribute Initial Value

35

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

Derived Attribute

36

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

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

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

Object Diagrams

39

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

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

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

Class/Object Diagram Differences

43

top related