1 cmsc 132: object-oriented programming ii software development iv department of computer science...

21
1 CMSC 132: Object-Oriented Programming II Software Development IV Department of Computer Science University of Maryland, College Park

Post on 21-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

1

CMSC 132: Object-Oriented Programming II

Software Development IV

Department of Computer Science

University of Maryland, College Park

2

Overview

Object-oriented design Goals

Techniques

Object-oriented view

Examples

3

Object-Oriented Design

GoalsImprove software design

Reduce implementation effort

Scalable to large software projects

Try to take advantage of two techniques

Abstraction

Encapsulation

4

Techniques – Abstraction

AbstractionProvide simple high-level model of

Physical entity

Activity

Helpful for managing complexity

Enables information hidingCan change implementation & representation

Will not affect other software components

5

Types of Abstraction

Procedural abstractionSpecify what actions should be performed

Hide algorithms

Data abstractionSpecify data objects for problem

Hide representation

6

Abstraction Example

Abstraction of a Student RosterData

List of student names

Actions

Create roster

Add student

Remove student

Print roster

STUDENT ROSTER

List of names

Create()

AddStudent()

RemoveStudent()

Print()

7

Techniques – Encapsulation

EncapsulationConfine information so it is only visible / accessible through an associated external interface

ApproachFor some entity X in program

Abstract data in X

Abstract actions on data in X

Collect data & actions on X in same location

Protects and hides X

8

Encapsulation

Extension of abstractionAlways abstract data & function together

Encapsulated entity Abstract Data Type (ADT)

ExamplesList ADT

May be implemented as array, linked list, etc…

Java collections library

9

Benefits of Encapsulation

Easier to make code modificationsDue to information hiding

Promotes code reuseInterface to data structure clearly defined

Easier to reuse code

Code reuse increases productivity

10

Object-Oriented Design

View software asA collection of entities (objects)

Functions associated with each object

Communication between objects

Exploits abstraction & encapsulation

Can rely on programming language support

11

Object-Oriented View

Example problem descriptionThermostat uses dial setting to control a heater to maintain constant temperature in room

Room

Thermostat(dial)

Heater

getTemperature() heaterOn()

12

History of Object-Oriented Design

Preceded by procedure-oriented viewEarliest approach to programming Uses procedure abstractionSimilar to actual machine instructionsFocus on control flow, program scopeExamples: Fortran, Cobol, Pascal, Basic

ExampleThermostat()

1. Get room temperature

2. If (temperature < setting) turn heater on

3. Else turn heater off

4. Goto step 1

13

OO Programming Languages

Development historySimula (Dahl & Nygaard, 1962)

Modeling discrete event simulation

Smalltalk (Kay, 1972)

General programming

C++ (Stroustrup, 1979)

Manage complexity in huge software projects

Java (Gosling, 1991)

Designed for embedded processors

14

Factors in Success of OO Design

Growing demandMore experience with large software projects

Improvements in language designMade OO programming easier

Improvements compiler technologySupport more language features efficiently

Improvements in hardwareHandled inefficiencies in OO programming

Made performance less critical

15

Elements of Object-Oriented Design

ObjectsEntities in program

MethodsFunctions associated with objects

ClassesGroups of objects with similar properties

InheritanceRelationship between classes

16

Objects

DefinitionEntity that has state, behavior, and identity

State (data)

Properties possessed by object

Current values of those properties

Behavior (methods)

How objects react to changes in state

How objects interact with each other

Identity (references)

Mechanism to distinguish between objects

17

Object Example

ThermostatState

DesiredTemp

CurrentTemp

HeaterState

Behavior

SetDesiredTemp()

TurnHeaterOn()

TurnHeaterOff()

Identity

this

18

Object Example

ThermostatState Property Value

DesiredTemp integer 78o

CurrentTemp integer 72o

HeaterState boolean ON

19

Object State

PropertiesStatic, unchanging

May view as types

ValuesDynamic, changes

Within bounds set by properties

20

Methods

DefinitionProcedures associated with object

Specify behavior of objects

Invocation sending message to object

ExamplemyThermostat.setDesiredTemp(78)

myThermostat.turnHeaterOn()

myThermostat.turnHeaterOff()

21

Method Types

AccessorReturn state information

MutatorModify state information

ConstructorCreate & initialize new object

DestructorRemove object & free up resources