Upload
rafe-phelps
View
219
Download
1
Tags:
Embed Size (px)
Citation preview
Lecture 8:Objects/ClassesClass Diagram
MIS 210Information 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
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.
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.
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?)
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
• 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:
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
• 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
• 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
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
• Do you know how to drive a car?
• Do you know how everything works under the hood?– The correct answer is. . .
Encapsulation
• 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
• 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
• 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
• 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
• 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
Sylnovie Merchant, Ph.D. MIS 210 Fall 2004
Polymorphism
• Polymorphism: the same message can elicit a different response depending on the receiver
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
• 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
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
Many people use “object” to refer to a class.
Strictly, as used in UML, an “object” is an
Instance.
Classes or Instances?
Responsibilities and Collaborations
• Responsibilities– Knowing– Doing
• Collaboration– Objects working together to service a request
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
Class Components
• Name
• Attributes
• Services
CustomerCustIdNumFirstNameLastName
etc...
AccessAccountDebitAccount
GetAccountBaletc...
What it knows about itself
What it can do
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
• 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
• 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?
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
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”
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
Multiple Inheritance
• Subclass inherits from more than one superclass
DVD Player
Audio Video
Repeated Inheritance
• Superclasses inherit from a higher level superclass
ElectronicEquipment
DVDPlayer
VideoAudio
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”
Generalization/Specialization
Customer
NameAddressPhone
DiveCustomer
CertificateCertificate Date
BoatCustomer
Safety CourseCompletion Date
MultipleInheritance andGeneralization
CustomerNameAddressPhone
DiveCustomer
CertificateCertificate Date
BoatCustomerSafety Course
Completion Date
Dive&BoatCustomer
CertificateCertificate Date
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”
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
• 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
Associations
CourseOffering
*Course No.*DateRoom No.Max Enrol
Student
*Student No.NameAgeSex
Instructor
*Employee No.NameAgeSexSalary
teaches
Enrolls In
Associations
CourseOffering
*Course No.*DateRoom No.Max Enrol
Student
*Student No.NameAgeSex
Instructor
*Employee No.NameAgeSexSalary
teaches
College
*NameAddressPhoneRating
Employs
Enrolls In
teaches
• 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
*
• 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
**
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
Aggregation
• Part-whole relationship
• A whole is equal to the sum of its parts
• Classes are grouped to form an aggregate class
• 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
Aggregation versus Association
Association: refers to the relationship between dissimilar classes of objects
Book
Customer
PageChapter
association
aggregation
Aggregation versus Generalization
Car
ToyotaFord
DoorWheels
Example Class Diagram