56
1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Object-Oriented Systems Development Development: Using the Unified Modeling Using the Unified Modeling Language Language

1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling

Embed Size (px)

Citation preview

Page 1: 1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling

1

Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill

Chapter 2:Object Basics

Object-Oriented Systems Object-Oriented Systems DevelopmentDevelopment: Using the Unified Modeling Using the Unified Modeling LanguageLanguage

Page 2: 1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling

2

Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill

Goals

•Define Objects and classes•Describe objects’ methods,

attributes and how objects respond to messages,

•Define Polymorphism, Inheritance, data abstraction, encapsulation, and protocol,

Page 3: 1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling

3

Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill

Goals (Con’t)•Describe objects

relationships,•Describe object

persistence,•Understand meta-classes.

Page 4: 1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling

4

Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill

What is an object?•The term object was first

formally utilized in the Simula language to simulate some aspect of reality.

•An object is an entity.– It knows things (has attributes)– It does things (provides

services or has methods)

Page 5: 1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling

5

Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill

It Knows things (attributes)I am an Employee. I know my name, social security number and my address.

Page 6: 1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling

6

Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill

Attributes (Con’t)

I am a Car.I know my color, manufacturer, cost, owner and model.

Page 7: 1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling

7

Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill

Attributes (Con’t)

I am a Fish. I know my date of arrival andexpiration.

Page 8: 1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling

8

Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill

It does things (methods)

I know how to computemy payroll.

Page 9: 1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling

9

Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill

Methods (Con’t)

I know how to stop.

Page 10: 1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling

10

Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill

Methods (Con’t)

I know how to cook myself.

Page 11: 1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling

11

Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill

What is an object? (Con’t)•Attributes or properties

describe object’s state (data) and methods define its behavior.

Page 12: 1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling

12

Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill

Object is whatever an application wants to talk

about.•For example, Parts and

assemblies might be objects of bill of material application.

•Stocks and bonds might be objects of financial investment applications.

Page 13: 1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling

13

Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill

Objects

• In an object-oriented system, everything is an object: numbers, arrays, records, fields, files, forms, an invoice, etc.

Page 14: 1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling

14

Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill

Objects (Con’t)

•An Object is anything, real or abstract, about which we store data and those methods that manipulate the data.

•Conceptually, each object is responsible for itself.

Page 15: 1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling

15

Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill

Objects (Con’t) A window object is

responsible for things like opening, sizing, and closing itself.

A chart object is responsible for things like maintaining its data and labels, and even for drawing itself.

Page 16: 1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling

16

Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill

Two Basic QuestionsWhen developing an O-O

application, two basic questions always arise.

• What objects does the application need?

• What functionality should those objects have?

Page 17: 1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling

17

Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill

Traditional Approach•The traditional approach to

software development tends toward writing a lot of code to do all the things that have to be done.

•You are the only active entity and the code is just basically a lot of building materials.

Page 18: 1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling

18

Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill

Object-Oriented Approach•OO approach is more like

creating a lot of helpers that take on an active role, a spirit, that form a community whose interactions become the application.

Page 19: 1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling

19

Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill

Object’s Attributes•Attributes represented by

data type. •They describe objects

states.• In the Car example the car’s

attributes are: •color, manufacturer, cost,

owner, model, etc.

Page 20: 1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling

20

Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill

Object’s Methods

• Methods define objects behavior and specify the way in which an Object’s data are manipulated.

• In the Car example the car’s methods are:

• drive it, lock it, tow it, carry passenger in it.

Page 21: 1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling

21

Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill

Objects are Grouped in Classes

•The role of a class is to define the attributes and methods (the state and behavior) of its instances.

•The class car, for example, defines the property color.

•Each individual car (object) will have a value for this property, such as "maroon," "yellow" or "white."

Page 22: 1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling

22

Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill

Employee Class

John object Jane object Mark object

Page 23: 1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling

23

Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill

A Class is an Object Template, or an Object

Factory.

Boeing Airplane Objects

(Boeing instances)

Page 24: 1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling

24

Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill

Class Hierarchy

•An object-oriented system organizes classes into subclass-super hierarchy.

• At the top of the hierarchy are the most general classes and at the bottom are the most specific

Page 25: 1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling

25

Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill

Class Hierarchy (Con’t)

Motor Vehicle

Truck CarBus

• A subclass inherits all of the properties and methods (procedures) defined in its superclass.

Page 26: 1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling

26

Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill

Inheritance (programming by extension )• Inheritance is a

relationship between classes where one class is the parent class of another (derived) class.

Page 27: 1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling

27

Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill

Inheritance (Con’t)

• Inheritance allows classes to share and reuse behaviors and attributes.

Page 28: 1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling

28

Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill

Inheritance (Con’t)

•The real advantage of inheritance is that we can build upon what we already have and,

•Reuse what we already have.

Page 29: 1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling

29

Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill

Inheritance (Con’t)

Ford

Vehicle

Car

Mustang Taurus Thunderbird

stop (myMustang)

I don’t know how to stop

I know how to stopstop method is reusable

Page 30: 1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling

30

Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill

Multiple Inheritance

•OO systems permit a class to inherit from more than one superclass.

•This kind of inheritance is referred to as multiple inheritance.

Page 31: 1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling

31

Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill

Multiple Inheritance (Con’t)• For example utility vehicle inherits

from Car and Truck classes.

MotorVehicle

Truck Car Bus

UtilityVehicle

Page 32: 1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling

32

Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill

Encapsulation and Information Hiding

Private Protocol

Public ProtocolMessages

Data

Permissible operations

• Information hiding is a principle of hiding internal data and procedures of an object.

Page 33: 1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling

33

Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill

Encapsulation and Information Hiding (Con’t)

•By providing an interface to each object in such a way as to reveal as little as possible about its inner workings.

•Encapsulation protects the data from corruption.

Page 34: 1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling

34

Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill

Protocol

•Protocol is an interface to the object.

•TV contains many complex components, but you do not need to know about them to use it.

Page 35: 1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling

35

Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill

Message

•Objects perform operations in response to messages.

•For example, you may communicate with your computer by sending it a message from hand-held controller.

Page 36: 1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling

36

Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill

A Case Study - A Payroll Program

• Consider a payroll program that processes employee records at a small manufacturing firm. This company has three types of employees:

• 1. Managers: Receive a regular salary.• 2. Office Workers: Receive an hourly wage

and are eligible for overtime after 40 hours.

• 3. Production Workers: Are paid according to a piece rate.

Page 37: 1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling

37

Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill

Structured ApproachFOR EVERY EMPLOYEE DO BEGIN

IF employee = manager THEN CALL computeManagerSalaryIF employee = office worker THEN CALL computeOfficeWorkerSalary

IF employee = production worker THEN CALL computeProductionWorkerSalary

END

Page 38: 1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling

38

Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill

What if we add two new types of employees?

•Temporary office workers ineligible for overtime,

• Junior production workers who receive an hourly wage plus a lower piece rate.

Page 39: 1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling

39

Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill

FOR EVERY EMPLOYEE DO BEGIN

IF employee = manager THEN CALL computeManagerSalary

IF employee = office worker THEN CALL computeOfficeWorker_salary

IF employee = production worker THEN CALL computeProductionWorker_salary

IF employee = temporary office worker THEN CALL

computeTemporaryOfficeWorkerSalaryIF employee = junior production worker THEN

CALL computeJuniorProductionWorkerSalary END

Page 40: 1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling

40

Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill

An Object-Oriented Approach• What objects does the application

need?– The goal of OO analysis is to

identify objects and classes that support the problem domain and system's requirements.

– Some general candidate classes are:

– Persons– Places– Things

Page 41: 1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling

41

Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill

What are some of the application’s classes?

•Employee•Manager •Office Workers •Production Workers

Page 42: 1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling

42

Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill

Class Hierarchy

• Identify class hierarchy• Identify commonality

among the classes•Draw the general-specific

class hierarchy.

Page 43: 1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling

43

Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill

Class Hierarchy (Con’t)

Employee

OfficeWorker Manager ProductionWorker

nameaddresssalarySS#

dataEntryComputePayrollprintReport

dataEntryComputePayrollprintReport

dataEntryComputePayrollprintReport

Page 44: 1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling

44

Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill

OO ApproachFOR EVERY EMPLOYEE DO BEGIN

employee computePayrollEND

Page 45: 1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling

45

Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill

If a new class of employee were addedEmployee

OfficeWorker Manager ProductionWorker

nameaddresssalarySS#

dataEntryComputePayrollprintReport

dataEntryComputePayrollprintReport

dataEntryComputePayrollprintReport

TemporaryOfficeWorker

ComputePayroll

JuniorProductionWorker

ComputePayroll

Page 46: 1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling

46

Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill

Polymorphism

•Polymorphism means that the same operation may behave differently on different classes.

•Example: computePayroll

Page 47: 1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling

47

Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill

Associations

•The concept of association represents relationships between objects and classes.

•For example a pilot can fly planes.

Page 48: 1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling

48

Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill

Associations (Con’t)

Pilot Planescan fly flown by

Page 49: 1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling

49

Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill

Clients and Servers

•A special form of association is a client-server relationship.

•This relationship can be viewed as one-way interaction: one object (client) requests the service of another object (server).

Page 50: 1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling

50

Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill

Clients and Servers (Con’t)

PrintServer ItemRequest for printing

Page 51: 1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling

51

Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill

Objects and Persistence

•Objects have a lifetime.• An object can persist

beyond application session boundaries, during which the object is stored in a file or a database, in some file or database form.

Page 52: 1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling

52

Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill

Meta-Classes

•Everything is an object. •How about a class? • Is a class an object? •Yes, a class is an object! So, if

it is an object, it must belong to a class.

• Indeed, class belongs to a class called a Meta-Class or a class' class.

Page 53: 1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling

53

Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill

Meta-Classes (Con’t)

•Meta-class used by the compiler. For example, the meta-classes handle messages to classes, such as constructors and "new."

Page 54: 1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling

54

Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill

Summary•Rather than treat data and

procedures separately, object-oriented programming packages them into "objects."

•O-O system provides you with the set of objects that closely reflects the underlying application

Page 55: 1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling

55

Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill

Summary (Con’t)Advantages of object-

oriented programming are:•The ability to reuse code, •develop more maintainable

systems in a shorter amount of time.

Page 56: 1 Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill Chapter 2: Object Basics Object-Oriented Systems Development Using the Unified Modeling

56

Object-Oriented Systems Development Bahrami © Irwin/ McGraw-Hill

Summary (Con’t)

•more resilient to change, and

•more reliable, since they are built from completely tested and debugged classes.