44
OOAD Class Diagram

Slide 5 Class Diagram

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Slide 5 Class Diagram

OOADClass

Diagram

Page 2: Slide 5 Class Diagram

What is a Class Diagram?

• A Class Diagram is a diagram describing the structure of a system

• shows the system's

• classes

• Attributes

• Operations (or methods),

• Relationships among the classes.

Page 3: Slide 5 Class Diagram

What is a Class Diagram?

• A class diagram depicts classes and their interrelationships

• Used for describing structure and behavior in the use cases

• Provide a conceptual model of the system in terms of entities and their relationships

• Used for requirement capture, end-user interaction

• Detailed class diagrams are used for developers

Page 4: Slide 5 Class Diagram

Essential Elements of a UML Class Diagram

• Class• Attributes• Operations• Relationships

– Associations

– Generalization

– Realization

– Dependency

• Constraint Rules and Notes

Page 5: Slide 5 Class Diagram

Class

• Describes a set of objects having similar:– Attributes (status)

– Operations (behavior)

– Relationships with other classes

• Graphically, a class is rendered as a rectangle, usually including its name, attributes, and operations in separate, designated compartments.

Page 6: Slide 5 Class Diagram

Class Names

The name of the class is the only required tag in the graphical representation of a class. It always appears in the top-most compartment.

Page 7: Slide 5 Class Diagram

Class

Page 8: Slide 5 Class Diagram

Class Attributes and OperationsAn attribute is a named property of a class that describes the object being modeled.

In the class diagram, attributes appear in the second compartment just below the name-compartment.

Attributes can be:+ public# protected- private

Attributes are usually listed in the form: attributeName : Type

Operations describe the class behavior and appear in the third compartment.

Page 9: Slide 5 Class Diagram

Visibility and Access for attributes and operations of a class

Page 10: Slide 5 Class Diagram

Class

Account_Name- Customer_Name- Balance

+addFunds( )+withDraw( )+transfer( )

Name

Attributes

Operations

Page 11: Slide 5 Class Diagram

Class Attributes and OperationsClass Attributes: The attribute type is shown after the colon. Attributes map onto member variables (data members) in code.

Class Operations (Methods): The return type of a method is shown after the colon at the end of the method signature. The type of method parameters are shown after the colon following the parameter name. Operations map onto class methods in code

Page 12: Slide 5 Class Diagram

Operation (Method) Parameter Directionality

Parameter direction

Description

in states that p1 and p2 are passed to op1 by the caller.

They are both in parameters.

inout states that p3 is passed to op2 by the caller and is then possibly modified by op2 and is passed back out.

p3 is an inout parameter.

out states that p6 is not set by the caller but is modified by op3 and is passed back out.

p6 is an out parameter.

Page 13: Slide 5 Class Diagram

Association

• In UML, object interconnections (logical or physical), are modeled as relationships.

• An association between two classes indicates that objects at one end of an association “recognize” objects at the other end and may send messages to them.

• Example: “An Employee works for a Company”

Employee Company

Page 14: Slide 5 Class Diagram

Association Relationships

If two classes in a model need to communicate with each other, there must be link between them.

An association denotes that link.

InstructorStudent

Page 15: Slide 5 Class Diagram

Association Relationships

We can indicate the multiplicity of an association by adding multiplicity adornments to the line denoting the association.

The example indicates that a Student has one or more Instructors:

InstructorStudent1..*

Page 16: Slide 5 Class Diagram

Association Relationships

The example indicates that every Instructor has one or more Students:

InstructorStudent1..*

Page 17: Slide 5 Class Diagram

Association Relationships

We can also indicate the behavior of an object in an association (i.e., the role of an object) using rolenames.

InstructorStudent1..*1..*

learns fromteaches

Page 18: Slide 5 Class Diagram

Association Relationships

We can also name the association.

TeamStudentmembership

1..* 1..*

Page 19: Slide 5 Class Diagram

Association Relationships

We can specify dual associations.

TeamStudent

member of

1..*

president of1 1..*

1..*

Page 20: Slide 5 Class Diagram

Associations

• Multiplicity– the number of objects that participate in the association.– Indicates whether or not an association is mandatory.

Exactly one 1

Zero or more (unlimited) * (0..*)

One or more 1..*

Zero or one (optional association) 0..1

Specified range 2..4

Multiple, disjoint ranges 2, 4..6, 8

Multiplicity Indicators

Page 21: Slide 5 Class Diagram

Association Relationships

A class can have a self association.

LinkedListNode

next

previous

Page 22: Slide 5 Class Diagram

Aggregation• A special form of association that models a

whole-part relationship between an aggregate (the whole) and its parts.

• A directional association between objects.

• When an object ‘has-a’ another object, then you have got an aggregation between them.

• Direction between them specified which object contains the other object.

• It is also called ‘Has-a’ relationship.

Page 23: Slide 5 Class Diagram

Aggregation

Class2 is part of Class1. Many instances (denoted by the *) of Class2

can be associated with Class1. Objects of Class1 and Class2 have separate

lifetimes.

Page 24: Slide 5 Class Diagram

Aggregation

• Note: If you delete the parent object, even then the child object may exist. One object can contain the other, but there is no restriction that the composed object has to exist in order to have existence of child object.

Page 25: Slide 5 Class Diagram

Aggregation

Whole Part

Car Door2..*

Page 26: Slide 5 Class Diagram

Composition

• A strong form of aggregation• In a more specific manner, a restricted

aggregation is called composition.– The whole is the sole owner of its part.

• The part object may belong to only one whole

– The life time of the part is dependent upon the whole.

• The composite must manage the creation and destruction of its parts.

Page 27: Slide 5 Class Diagram

Composition

Objects of Class2 live and die with Class1.

Class2 cannot stand by itself.

Page 28: Slide 5 Class Diagram

Composition

• A class contains students. A student cannot exist without a class. There exists composition between class and students.

Page 29: Slide 5 Class Diagram

Difference between aggregation and composition

• Composition is more restrictive.

• When there is a composition between two objects, the composed object cannot exist without the other object.

• This restriction is not there in aggregation. Though one object can contain the other object, there is no condition that the composed object must exist.

• The existence of the composed object in aggregation is entirely optional.

Page 30: Slide 5 Class Diagram

Difference between aggregation and composition

• Example: A Library contains students and books. Relationship between library and student is aggregation.

• Relationship between library and book is composition.

– A student can exist without a library and therefore it is aggregation.

– A book cannot exist without a library and therefore its a composition.

Page 31: Slide 5 Class Diagram

Generalization Relationship

o A generalization connects a subclass to its superclass. It denotes an inheritance of attributes and behavior from the superclass to the subclass and indicates a specialization in the subclass of the more general superclass.

o “is kind of” relationship.

Subtype2Subtype1

Supertype

Page 32: Slide 5 Class Diagram

Generalization• A sub-class inherits from its super-class

– Attributes

– Operations

– Relationships

• A sub-class may

– Add attributes and operations

– Add relationships

– Refine (override) inherited operations

Person

Student

Page 33: Slide 5 Class Diagram

Generalization Relationships

UML permits a class to inherit from multiple superclasses, although some programming languages (e.g., Java) do not permit multiple inheritance.

Student

TeachingAssistant

Employee

Page 34: Slide 5 Class Diagram

Generalization Relationships

 Inheritance is indicated by a solid line with a closed, unfilled arrowhead pointing at the super class

Page 35: Slide 5 Class Diagram

Generalization Relationships

An example of inheritance using tree notation

Page 36: Slide 5 Class Diagram

Packages

UML provides an organizing element called a package.

Packages enable modelers to organize the model's classifiers into namespaces, which is sort of like folders in a filing system.

Dividing a system into multiple packages makes the system easier to understand, especially if each package represents a specific part of the system.

Page 37: Slide 5 Class Diagram

Drawing Packages

• There are two ways of drawing packages on diagrams.

1. If the modeler decides to show the package's members within the large rectangle, then all those members need to be placed within the rectangle.

Page 38: Slide 5 Class Diagram

Packages

Page 39: Slide 5 Class Diagram

Drawing Packages

2. If the modeler decides to show the package's members outside the large rectangle then all the members that will be shown on the diagram need to be placed outside the rectangle. To show what classifiers belong to the package, a line is drawn from each classifier to a circle that has a plus sign inside the circle attached to the package

Page 40: Slide 5 Class Diagram

Packages

Page 41: Slide 5 Class Diagram

Realization

• A realization relationship indicates that one class implements a behavior specified by another class (an interface or protocol).

• An interface can be realized by many classes.

• A class may realize many interfaces.

LinkedList<<interface>>

List ArrayList

Page 42: Slide 5 Class Diagram

Constraint Rules and Notes

• Constraints and notes annotate among other things associations, attributes, operations and classes.

• Constraints are semantic restrictions noted as Boolean expressions.

– UML offers many pre-defined constraints.

id: long { value > 0 }

CustomerOrder*1

{ total < $50 }may be canceled

Constraint Note

Page 43: Slide 5 Class Diagram

What is a Object Diagram?

• Object diagrams represent an instance of a class diagram.

• The purposes of object diagrams are similar to class diagrams.

• The difference is that a class diagram represents an abstract model consisting of classes and their relationships. But an object diagram represents an instance at a particular moment which is concrete in nature.

Page 44: Slide 5 Class Diagram

Class and Objects