Class Modeling Design Class diagram. Classes The term “class ” refers to a group of objects that...

Post on 14-Dec-2015

220 views 4 download

Tags:

transcript

Class Modeling

Design Class diagram

Classes• The term “class ” refers to a group of objects

that share a common attributes and common behaviour (operations).

• Objects are instances of a class.• A class is like a mold and an instance of a class

is like a molded object.• Example

Mary is an object instance, while Student is an object class.

Classes

• All objects that exists within a class inherit its attributes and the operations that are available to manipulate the attributes.

• A super class is a collection of classes • A subclass is a specialized instance of a class.

Classes

• A class has methods and attributes while object instances have behaviour and states

• Example: Bank account is a class which covers many different account types. John’s and Mary accounts are instances of the bank account class.

Bank Account-name-balance+debit(in amount)+credit(in amount)

Object1 : Bank Account

Name = John SmithBalance = 1,000.00

Object2 : Bank Account

Name = Robert JonesBalance = -1,000.00

Classes

• The two instances are in different states. John’s account is in the credit state (positive balance), while Robert’s account is in the withdrawn state (negative balance).

• The state of the objects can be changed by calling the credit or debit operations. Robert’s account can be changed to the credit state if a credit operation is invoked with a parameter greater than 1000.

Attributes

• Things in the real world have properties. An attribute is a property of a class.

• Attributes attached to classes and objects describe the class or object.

• An attribute is a data item where an object holds its own state information.

• Attributes have a name, value and data type e.g. integer, Boolean.

Example :

• A class automobile has an attribute colour. The domain of values for colour is (white, black, silver, gray, blue, red, yellow, green).

• A book can be described in terms of its author, ISBN, publisher, etc.

Operations• Each object can perform a set of functions in order to provide a

number of services in a software system.• A service is defined by one or more operations.• An operation is a function or a procedure which can access the

object’s data.• An operation consists of two parts

– name – argument(s)

• Thus each object must define its operation for each of its services. The collection of operations is the object’s interface. Other objects only need to know the interface of an object in order to invoke the operations provided by the object.

Operations

• An operation is sometimes called a method or a member function.

• Examples– withdraw(amount)– deposit (amount)– getbalance()

Encapsulation : Information Hiding

• It is only through such operations that other objects can access or manipulate the information stored in an object. The collection of operations provides an external interface to a class. The interface presents the outside view of the class without showing its internal structure or how the operations are implemented.

• It is only the producer, developer of that object that knows the details of the internal construction of that object. The users of an object are denied knowledge of the inner working of the object. Other objects only need to know the interface of an object in order to invoke the operations provided by the object.– Public interface– Protected interface

• Thus, objects are treated as black boxes. The implementation of objects are hidden from those that use them. This is a powerful concept called “Information Hiding”, better known as encapsulation principle.

Class diagram

• A class is depicted graphically in a class diagram.

• In UML, a class is represented by a rectangle with three compartments separated by horizontal lines. The class name appears in the top compartment, the list of attributes in the middle compartments and the list of operations in the bottom compartment of a box.

Shape

-origin-color

+move()+resize()

BankAccount

-account Name-customerName

+getbalance(): float+setbalance()

Examples

Naming Classes

• Classes are named as noun or a noun phrase. When using name phrases, eliminate the spaces and concatenate the words with their first letter in uppercase e.g. SavingAccount, BankAccount

Relationships

Relationships exist among objects which links the type of link that exists between objects. Through the link it is possible to discover the other objects that are related to it.

• Association• Generalization• Aggregation

Association• An association is a structural relationship that

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

• A plain association is between two peer classes which means that both classes are conceptually at the same level, no one more important than the other.

• Given an association connecting two classes, one can navigate( send messages) from an object of one class to an object of the other class and vice versa.

• It is possible that an object of the class can be linked to other objects of the same class.

Association

• If an association connects between two objects instead of classes, it is called a link. A link is an instance of an association.

BillGates:Person Microsoft:Company

Name of link

WorkFor

Links provide a convenient way to trace the relationship between objects.

Specify only those relationships that are necessary for the system

Person Company

Name of association

WorkFor

Association

• There are four adornments that apply to associations.– Name– Role– Multiplicity– Note

Reflexive Association and Roles

• A reflexive association is an association that relates one object of a class to another object of the same class. In other words, a class can be associated with itself

Aggregation :• Aggregation is a stronger form of association.• To model a “whole/part or part-of relationship

in which one class represents a larger thing ( the “whole”), which consists of smaller things (“ the parts”) is called aggregation. It represents a “has-a” relationship, meaning that an object of the whole has objects of the part.

• In UML, a link is placed between the whole and parts classes with a diamond head, attached to the whole class to indicate that this is an aggregation.

Company

Department

whole

part

aggregation

1

*****p

Aggregation :

* Part

Aggregation :

• Multiplicity can be specified at the end of the association for each of the part-of classes to indicate the quantity of the constituent parts.

• Aggregations are not named• Keywords used to identify aggregations are

“consists of”, contains, is part of.

• A stronger form of aggregation is called composition, which implies whole class has exclusive ownership of the parts classes. This means parts may be created after a composite (whole) is created, but such parts will be explicitly removed before the destruction of the composite.

• In UML, a filled diamond indicates the composition relationship.

Composition

Person CompanyEmployee Employer

1..n 1

Example

WorkFor

Position

-title-starting date-salary

Division

Department

1..n

1..n

School

InstructorCourse

Department

Student

has

attends

Assigned to

teaches

0…1

1*..

* * *1* ..

1

1* ..1 1*..

ComparisonAssociation Aggregation Composition

Is a relationship where all object have their own lifecycle

Is a specialize form of Association where all object have their own lifecycle

It is a strong type of Aggregation. Child object dose not have their lifecycle.

There is no owner There is ownership and child object can not belongs to another parent object

Sole ownership, parent object deletes all child object will also be deleted

Multiple students can associate with single teacher and single student can associate with multiple teachers but there is no ownership between the objects and both have their own lifecycle.

A single teacher can not belongs to multiple departments, but if we delete the department teacher object will not destroy

Multiple choice questions: Single questions can have multiple options and option can not belong to multiple questions. If we delete questions options will automatically delete

Inheritance

• The attributes and operations common to a group of subclasses are attached to a super-class and inherited by its sub-classes.

• Each subclass may also include new features on its own.

Properties of inheritance

• Generalization– The purpose of this property is to distribute the

commonalities from the superclass among a group of similar sub classes. The subclass ( derived class) inherits all the super class's ( base class) operation and attributes.

Bank Account

-account number-password-balance +getBalance() : float+setBalance()

CheckingAccount

+checkClearing()

SavingsAccount

-interest+addinterestToBalance()

Generalization

• BankAccount (superclass) has an attribute account_number and an operation getBalance(), the CheckingAccount (subclass) will also have the same attribute, account_number and the operation getBalance() as it is a subclass of BankAccount.

• It is unnecessary and inappropriate to show the superclass attributes and operations in the subclasses

Specialization• Specialization allows subclasses to extend the functionalities

of superclass. A subclass can introduce new operations and attributes of its own.

• Example• SavingsAccount inherits attributes account_number,

password and balance from BankAccount and extends the functionalities of BankAccount with an additional attribute, interest and an additional operation, addInterestToBalance. A SavingsAccount has the attribute interest that BankAccount does not because not all bank accounts earn interest.

Example - specialization• A subclass Y inherits all of the attributes and

operations associate with its super-class X. This means that all data structures and algorithms originally designed and implemented for X are immediately available for Y- no further work need be done. Reuse has been accomplished directly.

• Any change to the data or operations contained within a super-class is immediately inherited by all subclasses that have inherited from the super-class. Therefore a change in the super class is immediately propagated through a system.

Inheritance

• Also, at each level of the class hierarchy, new attributes and operations may be added to those classes that have been inherited from higher levels in the hierarchy.

X1

CHAR1 CHAR2CHAR3

X1

X2

CHAR1 CHAR2 CHAR3 CHAR4 CHAR5

X3

CHAR1 CHAR2 CHAR3 CHAR4 CHAR5 CHAR6

X4

CHAR1 CHAR2 CHAR3 CHAR4 CHAR5 CHAR7

CHAR 4 + CHAR 5

CHAR 7CHAR 6

Library class hierarchyCatalogue numberAcquisition dateCostTypeStatusNumber of copies

Library item

Acquire ()Catalogue ()Dispose ()Issue ()Return ()

AuthorEditionPublication dateISBN

Book

YearIssue

MagazineDirectorDate of releaseDistributor

Film

VersionPlatform

Computerprogram

TitlePublisher

Published item

TitleMedium

Recorded item

User class hierarchyNameAddressPhoneRegistration #

Library user

Register ()De-register ()

Affiliation

Reader

Items on loanMax. loans

Borrower

DepartmentDepartment phone

Staff

Major subjectHome address

Student

Multiple inheritance

• Rather than inheriting the attributes and services from a single parent class, a system which supports multiple inheritance allows object classes to inherit from several super-classes

• Can lead to semantic conflicts where attributes/services with the same name in different super-classes have different semantics

• Makes class hierarchy reorganisation more complex

Multiple inheritance

# Tapes

Talking book

AuthorEditionPublication dateISBN

Book

SpeakerDurationRecording date

Voice recording

Abstract Classes

• An abstract class is used to specify the required behaviour ( operations) of a class providing their actual implementation. An operation without the implementation (body) is called an abstract operation. A class with one or more abstract operations is an abstract class.

• An abstract class can act as a repository of shared operation signatures for its subclasses and so those methods must be implemented by subclasses according to the signatures.

Shape-origin-color

+move()+resize()+draw()

Rectangle+draw()

Circle+draw()

Polygon+draw()