Object-Oriented Design: Applying OOP Concepts to System Development - Prof. Nelson Padua-P, Study notes of Computer Science

The process of applying object-oriented design (ood) principles to system development. It covers the identification of classes and messages based on problem statement nouns and verbs, and provides examples of resulting classes and their interactions. The document also discusses the liskov substitution principle and the difference between composition and inheritance.

Typology: Study notes

Pre 2010

Uploaded on 02/13/2009

koofers-user-gre
koofers-user-gre 🇺🇸

5

(1)

9 documents

1 / 20

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CMSC 132:
Object-Oriented Programming II
Object-Oriented Design
Department of Computer Science
University of Maryland, College Park
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14

Partial preview of the text

Download Object-Oriented Design: Applying OOP Concepts to System Development - Prof. Nelson Padua-P and more Study notes Computer Science in PDF only on Docsity!

CMSC 132:

Object-Oriented Programming II

Object-Oriented Design

Department of Computer Science

University of Maryland, College Park

Applying Object-Oriented Design

  1. Look at objects participating in system Find nouns in problem statement (requirements & specifications) Noun may represent class needed in design Relationships (e.g., “has” or “belongs to”) may represent fields
  2. Look at interactions between objects Find verbs in problem statement Verb may represent message between objects
  3. Design classes accordingly Determine relationship between classes Find state & methods needed for each class
  1. Finding Classes

Thermostat uses dial setting to control a heater

to maintain constant temperature in room

Nouns

Thermostat Dial setting Heater Temperature Room

Finding Classes

Analyze each noun

Does noun represent class needed in design? Noun may be outside system Noun may describe state in class

Finding Classes

Decision not always clear

Possible to make everything its own class Approach taken in Smalltalk Overly complex 2+3 = 5 vs. NUM 2 .add(NUM 3 ) = NUM 5 Impact of design More classes  more abstraction, flexibility Fewer classes  less complexity, overhead Choice (somewhat) depends on personal preference

Singleton classes

A Singleton class is a class for which there will

only ever be one instance

Makes sense if the class is a subclass of

another class

For example, you might have a class Person, and a singleton subclass Elvis

Avoid making verbs/functions into classes

Examples class ListSorter, NameFinder Unless you might have multiple verb classes that all implement a common interface The Strategy design pattern

Finding Messages

Analyze each verb

Does verb represent interaction between objects?

For each interaction

Assign methods to classes to perform interaction

Analyzing Verbs

Uses

“Thermostat uses dial setting…”  Thermostat.setDesiredTemp(int degrees)

Control

“To control a heater…”  Heater.turnOn()  Heater.turnOff()

Maintain

“To maintain constant temperature in room”  Room.getTemperature()

Resulting Classes

Thermostat

State dialSetting Methods setDesiredTemp()

Heater

State heaterOn Methods turnOn(), turnOff()

Room

State temp Methods getTemperature()

Subtypes

If a class Y extends class X and implements

interface A

then Y is a subtype of both X and A

If Q is a subtype of P, then Q satisfies P’s

contract

Anyone who expects a P can be given a Q

This is known as the Liskov Substitution

Principle (named for Prof. Barbara Liskov)

Not always strictly followed, but an ideal to approach For example, some iterators don’t support remove

Which could be a subtype?

Class B {

/** Search for x in a,

* return location of first occurrence,

  • 1 if not found */

int search(int x, int a[]) { … } }

Class C {

/** * Search for x in a,

* return location of any occurrence,

  • 1 if not found */

int search(int x, int a[]) { … } }

is-a vs. has-a

Say we have two classes, Engine and Car

Two possible designs

A Car object has a reference to an Engine object has-a The Car class is a subtype of Engine is-a

Forms of Inheritance

Extension

Adds new functionality to subclass In Java → new method

Limitation

Restricts behavior of subclass In Java → override method, throw exception

Combination

Inherits features from multiple superclasses Also called multiple inheritance Not possible in Java In Java → implement interface instead

Multiple Inheritance Example

Combination

AlarmClockRadio has two parent classes State & behavior from both Radio & AlarmClock Superclasses