49
Lecture 8: Objects/Classes Class Diagram MIS 210 Information Systems I

Sylnovie Merchant, Ph.D. MIS 210 Fall 2004 Lecture 8: Objects/Classes Class Diagram MIS 210 Information Systems I

Embed Size (px)

Citation preview

Page 1: Sylnovie Merchant, Ph.D. MIS 210 Fall 2004 Lecture 8: Objects/Classes Class Diagram MIS 210 Information Systems I

Lecture 8:Objects/ClassesClass Diagram

MIS 210Information Systems I

Page 2: Sylnovie Merchant, Ph.D. MIS 210 Fall 2004 Lecture 8: Objects/Classes Class Diagram MIS 210 Information Systems I

• The ERD was the first systems analysis tool to focus on data and how it is Linked and Organized.

• It was from this that objects then evolved.• An Entity is a thing the users need to know (i.e, record)

something about.• An Entity Type is a group or class of entities that are all

the same kind of thing.• An Occurrence of an Entity Type is a specific individual

thing of the kind the Entity Type describes.

The Entity Relationship Diagram

Page 3: Sylnovie Merchant, Ph.D. MIS 210 Fall 2004 Lecture 8: Objects/Classes Class Diagram MIS 210 Information Systems I

The Entity Relationship Diagram

• Attributes are the things we need to know about an Entity.

• An Association is the interaction of one or more Entities and is represented by a verb.

• These interactions among the Entities (i.e., these associations) show us the paths we need to follow through the database to access the data.

Page 4: Sylnovie Merchant, Ph.D. MIS 210 Fall 2004 Lecture 8: Objects/Classes Class Diagram MIS 210 Information Systems I

Objects

• It is “Something in the user’s world that has a separate and distinct existence, and is of interest in that they need to keep data about it.”

• Some private data and a set of operations that can access that data– These operations exclusively access the data it carries -

no other code can touch it.

• Contains both data (what it knows) and behavior (what it can do)

• An Object is an entity with behavior.

Page 5: Sylnovie Merchant, Ph.D. MIS 210 Fall 2004 Lecture 8: Objects/Classes Class Diagram MIS 210 Information Systems I

THINGS (Objects), etc.• Things…

– Car, Train, Elephant, Sale, Invoice, Division, Account, etc.– Physical or Conceptual

• Attributes…– Make, Model, Year, – Color, Weight, – Serial Number, License Number

• Relationships…– e.g., to a Person object, i.e., the Owner.

• Behaviors…– Create– Change Color– Change Owner– Destroy Itself (Suicide?)

Page 6: Sylnovie Merchant, Ph.D. MIS 210 Fall 2004 Lecture 8: Objects/Classes Class Diagram MIS 210 Information Systems I

Sylnovie Merchant, Ph.D. MIS 210 Fall 2004

Object

• Since an object carries only– The data we need for the job at hand, and– The behavior (program code) for the job at hand

• This makes it an abstraction of the real-world object.• Attributes: what an object knows (about itself)• Methods: what an object can do• Identity: each instance is unique, even if very similar

Page 7: Sylnovie Merchant, Ph.D. MIS 210 Fall 2004 Lecture 8: Objects/Classes Class Diagram MIS 210 Information Systems I

• Each object instance carries a set of attribute values

• These represent the data items that a user might need to know about the object at any time

• Data types and sizes are defined for the class apply to every instance.

• The domain of an attribute is its set of all permissible values.

Attributes:

Page 8: Sylnovie Merchant, Ph.D. MIS 210 Fall 2004 Lecture 8: Objects/Classes Class Diagram MIS 210 Information Systems I

Sylnovie Merchant, Ph.D. MIS 210 Fall 2004

Methods and Messages

• A method is what an object can do; a specific implementation of an operation by a particular object– how it does an operation

• An operation (behavior) is an action that an object performs or is subject to – something the object can perform (walk; run)

• A message is an operation that one object performs upon another– An operation (behavior) is initiated by sending a

messages

Page 9: Sylnovie Merchant, Ph.D. MIS 210 Fall 2004 Lecture 8: Objects/Classes Class Diagram MIS 210 Information Systems I

• Defined: The condition or fact that something is itself and not something else.

• Two red apples. . .– Each is itself, even though identical to the other.

• Identity is inherent in an object.– An object has identity just because it is that

object.

Identity

Page 10: Sylnovie Merchant, Ph.D. MIS 210 Fall 2004 Lecture 8: Objects/Classes Class Diagram MIS 210 Information Systems I

• A child has identity from the moment of birth, or perhaps before.– It doesn’t get a name until after its birth.– So an identifier does not confer identity– But simply documents or recognizes identity that

already existed.– So do identifiers such as customer numbers, social

security numbers, etc.

• Remember, only objects have identity, attribute values don’t.

Identity

Page 11: Sylnovie Merchant, Ph.D. MIS 210 Fall 2004 Lecture 8: Objects/Classes Class Diagram MIS 210 Information Systems I

Sylnovie Merchant, Ph.D. MIS 210 Fall 2004

Encapsulation• Encapsulation means an object’s data (attributes)

is hidden within, and protected by, a shell of procedures (methods)

• Also known as information hiding• Methods allow for hiding of information by

concealing the way in which an object satisfies a request

• When we send a message, we do not know how or where it will be satisfied

• The interface is public; the internals are private • Leading to stability and portability

Page 12: Sylnovie Merchant, Ph.D. MIS 210 Fall 2004 Lecture 8: Objects/Classes Class Diagram MIS 210 Information Systems I

• Do you know how to drive a car?

• Do you know how everything works under the hood?– The correct answer is. . .

Encapsulation

Page 13: Sylnovie Merchant, Ph.D. MIS 210 Fall 2004 Lecture 8: Objects/Classes Class Diagram MIS 210 Information Systems I

• What does matter is consistency in the interface.

• Every car made these days has the standard pedal layout.

• Now we have standard user interfaces (or close) for all kinds of products: Touch phones, keyboards, cars, motorcycles, microwave ovens, thermostats, TV remote controls.

Encapsulation

Page 14: Sylnovie Merchant, Ph.D. MIS 210 Fall 2004 Lecture 8: Objects/Classes Class Diagram MIS 210 Information Systems I

• What all of these do, is HIDE THE DETAILS • The complexity is enclosed within the product• Leaving a small, easily-understood set of commands to

operate it.• We hide both data and program code within the object• Giving a level of encapsulation better than ever before,• Leading to stability and portability

Encapsulation

Page 15: Sylnovie Merchant, Ph.D. MIS 210 Fall 2004 Lecture 8: Objects/Classes Class Diagram MIS 210 Information Systems I

• Stability is improved since once a class is built, it will survive the years with very few changes.– It can be reused in new projects with few changes

• Portability is enhanced because only the narrow published interface needs to be learned by the new developers, i.e., the reusers.

Encapsulation

Page 16: Sylnovie Merchant, Ph.D. MIS 210 Fall 2004 Lecture 8: Objects/Classes Class Diagram MIS 210 Information Systems I

• All the attributes and their values should be hidden within the object and accessible only via the methods.

• There is a boundary between what is inside the object and what is outside it

• Crossed only by the interface.

Encapsulation

Page 17: Sylnovie Merchant, Ph.D. MIS 210 Fall 2004 Lecture 8: Objects/Classes Class Diagram MIS 210 Information Systems I

• Since we have encapsulated all the data and behavior of our system within the classes, we reduce the “ripple effect” of change.

• Even though the reduction may be small for each class, remember. . .– The number of possible interactions goes up

exponentially with the number of objects.– In a large system this makes a huge difference in

maintenance.

Encapsulation

Page 18: Sylnovie Merchant, Ph.D. MIS 210 Fall 2004 Lecture 8: Objects/Classes Class Diagram MIS 210 Information Systems I

Sylnovie Merchant, Ph.D. MIS 210 Fall 2004

Polymorphism

• Polymorphism: the same message can elicit a different response depending on the receiver

Page 19: Sylnovie Merchant, Ph.D. MIS 210 Fall 2004 Lecture 8: Objects/Classes Class Diagram MIS 210 Information Systems I

A class is a group of objects with: similar properties (attributes), common behavior (operations), common associations to other objects, and common semantics (i.e., meaning.)

Classes

Page 20: Sylnovie Merchant, Ph.D. MIS 210 Fall 2004 Lecture 8: Objects/Classes Class Diagram MIS 210 Information Systems I

• A class is a set of objects that share a common structure (attributes) and a common behavior (methods)

• We group objects together on the basis of:– Shared Attributes– Shared behavior

• Class of:– All Employees– All Products– All Transactions

• An individual object is then a INSTANCE

Classes

Page 21: Sylnovie Merchant, Ph.D. MIS 210 Fall 2004 Lecture 8: Objects/Classes Class Diagram MIS 210 Information Systems I

Classes perform such things as:– Creating Instances– Holding the program code– Holding the attribute definitions

• Data type and size, etc.– Point of contact for associations.

Classes

Page 22: Sylnovie Merchant, Ph.D. MIS 210 Fall 2004 Lecture 8: Objects/Classes Class Diagram MIS 210 Information Systems I

Many people use “object” to refer to a class.

Strictly, as used in UML, an “object” is an

Instance.

Classes or Instances?

Page 23: Sylnovie Merchant, Ph.D. MIS 210 Fall 2004 Lecture 8: Objects/Classes Class Diagram MIS 210 Information Systems I

Responsibilities and Collaborations

• Responsibilities– Knowing– Doing

• Collaboration– Objects working together to service a request

Page 24: Sylnovie Merchant, Ph.D. MIS 210 Fall 2004 Lecture 8: Objects/Classes Class Diagram MIS 210 Information Systems I

Class Symbol

• Name– should be unambiguous– should be a noun

• Attributes (what the class/object knows about itself)attribute name : data type = default value {property string}

• Methodsmethod name (parameter-list) : return-type

where parameter-list and return-type are specified as attributes

Visibility: + public - private # protected

Page 25: Sylnovie Merchant, Ph.D. MIS 210 Fall 2004 Lecture 8: Objects/Classes Class Diagram MIS 210 Information Systems I

Class Components

• Name

• Attributes

• Services

CustomerCustIdNumFirstNameLastName

etc...

AccessAccountDebitAccount

GetAccountBaletc...

What it knows about itself

What it can do

Page 26: Sylnovie Merchant, Ph.D. MIS 210 Fall 2004 Lecture 8: Objects/Classes Class Diagram MIS 210 Information Systems I

Class Diagrams

• Finding the classes is the core activity of OOSD.

• The class diagram describes the classes in the system and the static relationships among them

• Similar to ERD -- with methods added• Will be directly implemented in an OOPL• Most important model

Page 27: Sylnovie Merchant, Ph.D. MIS 210 Fall 2004 Lecture 8: Objects/Classes Class Diagram MIS 210 Information Systems I

• Some instances of a class may be grouped together on the basis of features not shared by the rest of the class– Attributes– Behavior – Associations

• Key verb is:

““ISAKINDA”ISAKINDA”““ISAKINDA”ISAKINDA”

Subclasses

Page 28: Sylnovie Merchant, Ph.D. MIS 210 Fall 2004 Lecture 8: Objects/Classes Class Diagram MIS 210 Information Systems I

• Yes, of course!• But Why?

• Because a Customer is a person, and people have names.

• Does a Customer have a Wage Rate?

• No, only Employee-type people have one of those!

Subclasses

In our company, does a Customer have a name?

Page 29: Sylnovie Merchant, Ph.D. MIS 210 Fall 2004 Lecture 8: Objects/Classes Class Diagram MIS 210 Information Systems I

Balance Owing Amount Overdue 30 days Amount Overdue 60 days Amount Overdue 90 days

Credit Rating Date of Last Payment

Balance Owing Amount Overdue 30 days Amount Overdue 60 days Amount Overdue 90 days

Credit Rating Date of Last Payment

Name NameAddress AddressPhone Phone

Date of Birth Date of BirthSex Sex

Name NameAddress AddressPhone Phone

Date of Birth Date of BirthSex Sex

SIN Marital Status

Number of DependantsDate HiredWage Rate

SIN Marital Status

Number of DependantsDate HiredWage Rate

Overlapping Sets of AttributesCustomerAttributes

CustomerAttributes

Person Attributes

Person Attributes

EmployeeAttributes

EmployeeAttributes

Page 30: Sylnovie Merchant, Ph.D. MIS 210 Fall 2004 Lecture 8: Objects/Classes Class Diagram MIS 210 Information Systems I

Sylnovie Merchant, Ph.D. MIS 210 Fall 2004

Inheritance

• A (top-down) relationship among classes wherein one class shares the structure or behavior defined in one (single inheritance) or more (multiple inheritance) other classes.

• Based on a hierarchical (IS-A) relationship

• Inheritance is in the “eye of the beholder”

Page 31: Sylnovie Merchant, Ph.D. MIS 210 Fall 2004 Lecture 8: Objects/Classes Class Diagram MIS 210 Information Systems I

Single Inheritance

• Subclasses inherit from no more than one superclass

Student

Undergrad Graduate

ssn, name

Grad advisorYear (F,So,J, S)

Compute gpa Compute gpa

Page 32: Sylnovie Merchant, Ph.D. MIS 210 Fall 2004 Lecture 8: Objects/Classes Class Diagram MIS 210 Information Systems I

Multiple Inheritance

• Subclass inherits from more than one superclass

DVD Player

Audio Video

Page 33: Sylnovie Merchant, Ph.D. MIS 210 Fall 2004 Lecture 8: Objects/Classes Class Diagram MIS 210 Information Systems I

Repeated Inheritance

• Superclasses inherit from a higher level superclass

ElectronicEquipment

DVDPlayer

VideoAudio

Page 34: Sylnovie Merchant, Ph.D. MIS 210 Fall 2004 Lecture 8: Objects/Classes Class Diagram MIS 210 Information Systems I

Generalization

• The convention for defining subclasses

• Think substitution - anywhere you have the class you can freely use the sub class– For Example, Wherever You Use Customer

you can use “Corporate Customer”

Page 35: Sylnovie Merchant, Ph.D. MIS 210 Fall 2004 Lecture 8: Objects/Classes Class Diagram MIS 210 Information Systems I

Generalization/Specialization

Customer

NameAddressPhone

DiveCustomer

CertificateCertificate Date

BoatCustomer

Safety CourseCompletion Date

Page 36: Sylnovie Merchant, Ph.D. MIS 210 Fall 2004 Lecture 8: Objects/Classes Class Diagram MIS 210 Information Systems I

MultipleInheritance andGeneralization

CustomerNameAddressPhone

DiveCustomer

CertificateCertificate Date

BoatCustomerSafety Course

Completion Date

Dive&BoatCustomer

CertificateCertificate Date

Page 37: Sylnovie Merchant, Ph.D. MIS 210 Fall 2004 Lecture 8: Objects/Classes Class Diagram MIS 210 Information Systems I

Navigation

• Defines the direction of an association

• Line Options– Uni-Directional - “Order Comes From Customer”– Bi-Directional - Two Roles are Inverses of Each Other– No Arrows - Assumed “Unknown” or “Bi-Directional”

Page 38: Sylnovie Merchant, Ph.D. MIS 210 Fall 2004 Lecture 8: Objects/Classes Class Diagram MIS 210 Information Systems I

Role

• A Label for that direction of the association

• Each Association has a Role for Each Direction of the Association

• When no Role is Identified, the Role Takes the Name of the Target Class

Page 39: Sylnovie Merchant, Ph.D. MIS 210 Fall 2004 Lecture 8: Objects/Classes Class Diagram MIS 210 Information Systems I

• Draw a line joining two class boxes to show a relationship exists

• Write the verb above the line.• Add a solid arrowhead so that it makes a sentence

when you read it in the direction of the arrow:• “Student enrolls in course

Associations

Student

*Student No.NameAgeSex

CourseOffering

*Course No.*DateRoom No.Max Enrol

Enrolls In

Page 40: Sylnovie Merchant, Ph.D. MIS 210 Fall 2004 Lecture 8: Objects/Classes Class Diagram MIS 210 Information Systems I

Associations

CourseOffering

*Course No.*DateRoom No.Max Enrol

Student

*Student No.NameAgeSex

Instructor

*Employee No.NameAgeSexSalary

teaches

Enrolls In

Page 41: Sylnovie Merchant, Ph.D. MIS 210 Fall 2004 Lecture 8: Objects/Classes Class Diagram MIS 210 Information Systems I

Associations

CourseOffering

*Course No.*DateRoom No.Max Enrol

Student

*Student No.NameAgeSex

Instructor

*Employee No.NameAgeSexSalary

teaches

College

*NameAddressPhoneRating

Employs

Enrolls In

teaches

Page 42: Sylnovie Merchant, Ph.D. MIS 210 Fall 2004 Lecture 8: Objects/Classes Class Diagram MIS 210 Information Systems I

• We diagram this by adding a star (asterisk) below the relationship line whenever it should show “many.”

• Read this one as :“Instructor teaches many courses”• This is a “One-to-Many” Association, or 1:M

Multiplicity

CourseOffering

*Course No.*DateRoom No.Max Enrol

Instructor

*Employee No.NameAgeSexSalary

teaches

*

Page 43: Sylnovie Merchant, Ph.D. MIS 210 Fall 2004 Lecture 8: Objects/Classes Class Diagram MIS 210 Information Systems I

• And, since we wish many Customers to buy many Products, this one is a Many-to-Many association, or M:M

Multiplicity

Customer

*A/c No.NameAddressPhone No.Balance

Product

*Prod No.DescriptionUnit PriceQty in Stock

buys

**

Page 44: Sylnovie Merchant, Ph.D. MIS 210 Fall 2004 Lecture 8: Objects/Classes Class Diagram MIS 210 Information Systems I

Multiplicity

• How many objects may participate in a given relationship• One Customer can make “one to many” Orders

– Multiplicity Notation• * = one to many• 1 = one• 0 = zero• 0..* = zero to many• 1..* = one to many• 2,4 = two or four• 2..4 = two to four

Multiplicity

Page 45: Sylnovie Merchant, Ph.D. MIS 210 Fall 2004 Lecture 8: Objects/Classes Class Diagram MIS 210 Information Systems I

Aggregation

• Part-whole relationship

• A whole is equal to the sum of its parts

• Classes are grouped to form an aggregate class

Page 46: Sylnovie Merchant, Ph.D. MIS 210 Fall 2004 Lecture 8: Objects/Classes Class Diagram MIS 210 Information Systems I

• Aggregation is a relationship between two classes where the instances of one class are in some way components, members or contents of the instances of the other class.

• These are relationships that appear on the class diagram along with all the subclass hierarchies and the regular 1:M and M:M associations

Aggregation

Page 47: Sylnovie Merchant, Ph.D. MIS 210 Fall 2004 Lecture 8: Objects/Classes Class Diagram MIS 210 Information Systems I

Aggregation versus Association

Association: refers to the relationship between dissimilar classes of objects

Book

Customer

PageChapter

association

aggregation

Page 48: Sylnovie Merchant, Ph.D. MIS 210 Fall 2004 Lecture 8: Objects/Classes Class Diagram MIS 210 Information Systems I

Aggregation versus Generalization

Car

ToyotaFord

DoorWheels

Page 49: Sylnovie Merchant, Ph.D. MIS 210 Fall 2004 Lecture 8: Objects/Classes Class Diagram MIS 210 Information Systems I

Example Class Diagram