chapter 2 object orientation. process phase affected by this chapter requirements analysis design...

27
Chapter 2 Object Orientation

Upload: doreen-kennedy

Post on 27-Dec-2015

222 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Chapter 2 Object Orientation. Process Phase Affected by This Chapter Requirements Analysis Design Implementation ArchitectureFrameworkDetailed Design

Chapter 2Object Orientation

Page 2: Chapter 2 Object Orientation. Process Phase Affected by This Chapter Requirements Analysis Design Implementation ArchitectureFrameworkDetailed Design

Process Phase Affected by This ChapterRequirementsAnalysis

Design

Implementation

ArchitectureFramework Detailed Design

Key: = less so

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 3: Chapter 2 Object Orientation. Process Phase Affected by This Chapter Requirements Analysis Design Implementation ArchitectureFrameworkDetailed Design

Before Object Orientation

Real world concepts

Software DesignEntities

SkljkvjkvjfkavjafkksaovjsdvjfvkfjvkfjkSkljkvjkvjfkavjafkksaovjsdvjfvkfjvkfjkSkljkvjkvjfkavjafkksaovjsdvjfvkfjvkfjkSkljkvjkvjfkavjafkksaovjsdvjfvkfjvkfjk

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 4: Chapter 2 Object Orientation. Process Phase Affected by This Chapter Requirements Analysis Design Implementation ArchitectureFrameworkDetailed Design

How Do We Express Ourselves?

"Customers Montague and

Susan entered the Ajax bank

and were served by teller

Andy ..... "

AJAX BANK

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 5: Chapter 2 Object Orientation. Process Phase Affected by This Chapter Requirements Analysis Design Implementation ArchitectureFrameworkDetailed Design

How We Express Ourselves

"Customers Montague and Susan entered the Ajax bank and were served by teller Andy ..... "

CLASSES OBJECTS

Note that Java code convention reverses this capitalization.

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 6: Chapter 2 Object Orientation. Process Phase Affected by This Chapter Requirements Analysis Design Implementation ArchitectureFrameworkDetailed Design

Object Orientation

Real world concepts

Software design entities

SkljkvjkvjfkavjafkksaovjsdvjfvkfjvkfjkSkljkvjkvjfkavjafkksaovjsdvjfvkfjvkfjkSkljkvjkvjfkavjafkksaovjsdvjfvkfjvkfjkSkljkvjkvjfkavjafkksaovjsdvjfvkfjvkfjk

AccountgetDetails()

Transactionexecute()

CustomergetFirstName()

Direct correspondence

Graphics reproduced with permission from Corel.Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 7: Chapter 2 Object Orientation. Process Phase Affected by This Chapter Requirements Analysis Design Implementation ArchitectureFrameworkDetailed Design

Key Concept: Benefits of OO

Object orientation provides a direct mapping between concepts and code.

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 8: Chapter 2 Object Orientation. Process Phase Affected by This Chapter Requirements Analysis Design Implementation ArchitectureFrameworkDetailed Design

Class Introduction

AjaxCustomer

Real world Classin Design

(UML notation)

Classin Source code

(Java)

class AjaxCustomer{ . . . . }

PermissionToPayclass PermissionToPay{ . . . . }

Mentalconcept

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 9: Chapter 2 Object Orientation. Process Phase Affected by This Chapter Requirements Analysis Design Implementation ArchitectureFrameworkDetailed Design

aliceBrownBMW:Auto

The Members of a Class

Autopublic int vehicleID …

protected int mileage …private myPrivateVariable …static int numAutosMade …

Class model

Toyota

numAutosMade12390924850984

mileage33024

jaynesCar:Auto

…mileage83402

Objects of Auto

Subclasses have these members too

myToyota:Toyota

…mileage

2105

Static variable: One version only

Non-static variable: One version for every object

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 10: Chapter 2 Object Orientation. Process Phase Affected by This Chapter Requirements Analysis Design Implementation ArchitectureFrameworkDetailed Design

Attribute Types (Shlaer & Mellor)

Naming: o fixed for each object

o distinguishes individuals

Descriptive: o varies through life of object

Referential: o ties instance of one class to

instance(s) of another

o == aggregation

Auto

vehicleID

mileage

owner

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 11: Chapter 2 Object Orientation. Process Phase Affected by This Chapter Requirements Analysis Design Implementation ArchitectureFrameworkDetailed Design

Key Concept: Classes and Objects

A class expresses a concept such as “HondaCivic.” An object is an instance of a class such as “the Honda Civic with vehicle ID 89R783HJD894.”

Page 12: Chapter 2 Object Orientation. Process Phase Affected by This Chapter Requirements Analysis Design Implementation ArchitectureFrameworkDetailed Design

The Clients of a Classclass Customer{ . . . String getName() { … } int computeBalance() { … }. . .}

class AjaxWebsiteGenerator{ . . . void makeProfile( Customer c ) { … String name = c.getName() … } . . .}

class AjaxAssets{ . . . int computeAssets() { . . . Customer c = customers[ i ]; assets += c.computeBalance(); . . . }. . .}

Client of CustomerClient of Customer

Class Customer

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 13: Chapter 2 Object Orientation. Process Phase Affected by This Chapter Requirements Analysis Design Implementation ArchitectureFrameworkDetailed Design

Aspects of OO Useful for Application Development

Class Introduction (section 2.2.1)

o basic motive of Object Orientation

o identifying parts that corresponds to the real world

Instantiation (section 2.2.2)

o creating instances of encapsulated concepts

Inheritance (section 2.3.1)

o capturing the way concepts occur in hierarchy

Polymorphism (section 2.3.2)

o capturing use of single action word to represent different things, depending on context

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 14: Chapter 2 Object Orientation. Process Phase Affected by This Chapter Requirements Analysis Design Implementation ArchitectureFrameworkDetailed Design

Requirements For e-Mail Creation Example

1. Summary:

Produces e-mail text for various types of customers.

2. Detailed requirements

2.1 The application displays choices to the console, as shown in figure xx.

2.2 For customers delinquent more than 90 days, the e-mail message generated is the statement shown in figure xx.

Page 1 of 4

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 15: Chapter 2 Object Orientation. Process Phase Affected by This Chapter Requirements Analysis Design Implementation ArchitectureFrameworkDetailed Design

Typical Interaction for e-mail Creation Example

Page 2 of 4

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 16: Chapter 2 Object Orientation. Process Phase Affected by This Chapter Requirements Analysis Design Implementation ArchitectureFrameworkDetailed Design

2.3 All non-delinquent customers receive a tailored e-mail messages as follows.

2.3.1 Mountain customers:

This month we have a special on West Face tents. Only $700.

... lots more output specialized to mountaineering customers ...

2.3.2 Regular customers:

All items are marked down 20% for this month only.

... lots more output for regular customers ...

Requirements For e-Mail Creation Example

Page 3 of 4

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 17: Chapter 2 Object Orientation. Process Phase Affected by This Chapter Requirements Analysis Design Implementation ArchitectureFrameworkDetailed Design

Requirements For e-Mail Creation Example

2.4 The e-mail is to be displayed on the console.

3. Future enhancements

We anticipate that the text is likely to change frequently, and that new kinds of customers are likely to be specified, each with its own new set of requirements.

Page 4 of 4

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 18: Chapter 2 Object Orientation. Process Phase Affected by This Chapter Requirements Analysis Design Implementation ArchitectureFrameworkDetailed Design

Disadvantages of Branching

Code for each case not cohesive(“cohesive”: forms a comprehensible unity)

o All types of customers coded together in single class

Expensive to … … add new functionality

o bloat switch or if - then code

… remove functionality o hunt for all parts that must be removed

… change functionality o hunt for all parts that must be changed

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 19: Chapter 2 Object Orientation. Process Phase Affected by This Chapter Requirements Analysis Design Implementation ArchitectureFrameworkDetailed Design

Aspects of the Customer Design Needing Improvement

We need to visualize the designo Code not an effective way to understand design

The design’s maintainability still has flaws o As the application grows, specialized class(es)

will be required to interact with the user

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 20: Chapter 2 Object Orientation. Process Phase Affected by This Chapter Requirements Analysis Design Implementation ArchitectureFrameworkDetailed Design

Key Concept: Polymorphism

- the use of several versions of a method, one in each derived class. This enables objectOfBaseClass.theMethod() to be interpreted variously at runtime, depending on what derived class objectOfBaseClass belongs to.

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 21: Chapter 2 Object Orientation. Process Phase Affected by This Chapter Requirements Analysis Design Implementation ArchitectureFrameworkDetailed Design

What’s Needed to Specify Functionality

Name of the functionExample: add

Argument types(if any) Example: o First parameter: integero Second parameter: float

Return type Example: double, reference type, void

Exceptions (if any) Example: IOException

More(?)o Are parameters inputs and/or outputs?o Describe what the function does (natural language)

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 22: Chapter 2 Object Orientation. Process Phase Affected by This Chapter Requirements Analysis Design Implementation ArchitectureFrameworkDetailed Design

The Need For Interfaces: Simplify …

class Draw

{ …

int setColor( String ) { … }

Pen getStandardPen() { … }

int getLogoStyle() { … }

void setColor( int ) { … }

void drawLogo( int, int ) { … }

void speedUpPen( int ) { … }

}

Page 23: Chapter 2 Object Orientation. Process Phase Affected by This Chapter Requirements Analysis Design Implementation ArchitectureFrameworkDetailed Design

Interface Example: a Draw Class Functions dealing with the pen used

o Pen getStandardPen()o void speedUpPen( int )o . . .

Functions dealing with the colors available o void setColor( int )o int setColor( String )o . . .

Functions covering the drawing of the company’s logoo void drawLogo( int, int ) o int getLogoStyle()o . . .

. . .. . . .

}}

}Pen interface

Color interface

Logo interface

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 24: Chapter 2 Object Orientation. Process Phase Affected by This Chapter Requirements Analysis Design Implementation ArchitectureFrameworkDetailed Design

Key Concept: Interfaces

An interface is a set of function prototypes (each with name, parameter types, return type, exception type).

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 25: Chapter 2 Object Orientation. Process Phase Affected by This Chapter Requirements Analysis Design Implementation ArchitectureFrameworkDetailed Design

Issues to be Addressed How do we visualize a set of classes?

How can classes relate to each other?

How should classes relate to each other?

How can we describe functionality occurring among

several classes?

How do we describe the manner in which objects

respond to events occurring on them?

Are there patterns of class usage that recur?

o So we can existing design parts

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 26: Chapter 2 Object Orientation. Process Phase Affected by This Chapter Requirements Analysis Design Implementation ArchitectureFrameworkDetailed Design

Summary of This Chapter

A Class represents a concept

o Example: House

An Object is an instance of a class

o Example: 23 Main Street, Springfield

Classes can relate in several ways: Mainly …

o A Client of a class refers to that class in one of its methods

o Inheritance: “kind of” relationship

o Aggregation: “has a” relationship, explained in chapter xx

Polymorphism means “action depends on context”

o Executing anObject.aMethod() actually executes the version

of aMethod() in the subclass that anObject belongs toAdapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 27: Chapter 2 Object Orientation. Process Phase Affected by This Chapter Requirements Analysis Design Implementation ArchitectureFrameworkDetailed Design

Typical Interaction for Auto Description Exercise

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.