Docsity

Object-Oriented Design: Abstraction and Encapsulation in Computer Science, Study notes of Computer Science

Partial preview of the text

Download Object-Oriented Design: Abstraction and Encapsulation in Computer Science and more Study notes Computer Science in PDF only on Docsity!

CMSC 132:

Object-Oriented Programming II

Object-Oriented Design I

Department of Computer Science

University of Maryland, College Park

Object-Oriented Design

Goals

Improve software design Reduce implementation effort Scalable to large software projects Try to take advantage of two techniques Abstraction Encapsulation

Encapsulation

Extension of abstraction

Always abstract data & function together Encapsulated entity

Abstract Data Type (ADT)

Examples

List ADT May be implemented as array, linked list, etc… Java collections library

Benefits of Encapsulation

Easier to make code modifications

Due to information hiding

Promotes code reuse

Interface to data structure clearly defined Easier to reuse code

Code reuse increases productivity

Object-Oriented View

Example problem description

Thermostat uses dial setting to control a heater tomaintain constant temperature in room Room Thermostat (dial) Heater getTemperature() heaterOn()

History of Object-Oriented Design Preceded by procedure-oriented view Earliest approach to programming Uses procedure abstraction Similar to actual machine instructions Focus on control flow, program scope Examples: Fortran, Cobol, Pascal, Basic

Example

Thermostat()1. Get room temperature

If (temperature < setting) turn heater on

Else turn heater off

Goto step 1

Factors in Success of OO Design

Growing demand

More experience with large software projects

Improvements in language design

Made OO programming easier

Improvements compiler technology

Support more language features efficiently

Improvements in hardware

Handled inefficiencies in OO programming Made performance less critical

Elements of Object-Oriented Design^ Objects Entities in program

Methods

Functions associated with objects

Classes

Groups of objects with similar properties

Inheritance

Relationship between classes

Object Example

Thermostat

State DesiredTemp CurrentTemp HeaterState Behavior SetDesiredTemp() TurnHeaterOn() TurnHeaterOff() Identity this

Object Example

Thermostat

State Property Value DesiredTemp integer

o CurrentTemp integer

o HeaterState boolean

ON

Object Behavior

Methods

Procedures associated with object

Specify behavior of objects Invocation

sending message to object

Example

myThermostat.setDesiredTemp(78) myThermostat.turnHeaterOn() myThermostat.turnHeaterOff()

Method Types

Accessor

Return state information

Mutator

Modify state information

Constructor

Create & initialize new object

Destructor

Remove object & free up resources

Reference Variables

Example

Identity

Equivalence

Whether two objects are equal

Name equivalence

Reference variables point to same object

Content equivalence

Objects from same class State in each object are identical