The words you are searching are inside this book. To get more targeted content, please make full-text search by clicking here.

Object-Oriented Design Engineering design A systematic, intelligent process in which designers generate, evaluate and specify designs for devices, systems, or

Discover the best professional documents and content resources in AnyFlip Document Base.
Search
Published by , 2017-02-05 02:20:02

Object-Oriented Design - Georgia Tech

Object-Oriented Design Engineering design A systematic, intelligent process in which designers generate, evaluate and specify designs for devices, systems, or

Object-Orien

Christophe
chris.simpkin

Chris Simpkins (Georgia Tech) CS 2340 Objec

nted Design

er Simpkins
[email protected]

cts and Design CS 2340 1 / 16

Object-Oriented Design

Engineering design

A systematic, intelligent proces
evaluate and specify designs fo
processes whose form(s) and f
objectives and users’ needs wh
constraints. – Dym and Little, q
Software Engineering Design

Object-oriented design:

identifying problem domain obje
classes (classes),
factoring classes so they have t
(responsibilities),

typically means defining class

defining relationships between

Chris Simpkins (Georgia Tech) CS 2340 Objec

ss in which designers generate,
or devices, systems, or
function(s) acheive clients’
hile satisfying a specified set of
quoted in Carlos Otero,

ects and representing them with

the right granularity

s and interface hierarchies
the classes (collaborations).

cts and Design CS 2340 2 / 16

Object-Oriented Design Prin

Single Responsibility Principle
Open Closed Principle
Liskov Substitution Principle
Interface Segregation Principle
Dependency Inversion Principle

Chris Simpkins (Georgia Tech) CS 2340 Objec

nciples

e
e

cts and Design CS 2340 3 / 16

Single Responsibility Princip

A class should have only one re
A responsiblity is an axis of cha
Classes with multiple responsib
responsibilities, making change
due to the risk of breaking the o
the class.

Chris Simpkins (Georgia Tech) CS 2340 Objec

ple (SRP)

eason to change.
ange.
blities will couple the
es to one responsilbity more difficult
other responsibilities handled by

cts and Design CS 2340 4 / 16

SRP Example - Too Many R

public class GreetingFrame extends J
private JLabel greetingLabel;

public GreetingFrame() {
...
JButton button = new JButton
button.addActionListener(thi
...

}
public void actionPerformed(Acti

Greeter greeter = new Greete
String greeting = greeter.gr
greetingLabel.setText(greeti
}
}

If we add other buttons or menu
modify the actionPerformed
event source.
If we chnage the behavior of th
actoinPerformed method.

Chris Simpkins (Georgia Tech) CS 2340 Objec

Responsibilities

JFrame implements ActionListener {

n("Greet!");
is);

ionEvent e) {
er("bob");
reet();
ing);

u items to the GUI, we have to
d method to handle an additional

he a button, we have to modify the

cts and Design CS 2340 5 / 16

SRP Example - Single-Resp

private class GreetButtonListener im
private JLabel greetingLabel;

public GreetButtonListener(JLabel
this.greetingLabel = greetingLab

}
public void actionPerformed(Action

...
}
}
public class GreetingFrame extends J
...
public GreetingFrame() {

...
button.addActionListener(new Gre
...
}
}

Additions to the UI require chan
Changes to greet button behav
GreetButtonListener.

Chris Simpkins (Georgia Tech) CS 2340 Objec

ponsibility Classes

mplements ActionListener {
greetingLabel) {

bel;
nEvent e) {

JFrame {

eetButtonListener(greetingLabel));

nges only to GreetingFrame.
vior require changes only to

cts and Design CS 2340 6 / 16

Open–Closed Principle (OC

Software Entities (classes, mod
open for extension, but closed f

Open for extension means the m
new behavior.
Closed for modification means
touched in order to add the exte
Object-oriented polymorphism make
new code that works with old code w
code.

Chris Simpkins (Georgia Tech) CS 2340 Objec

CP)

dules, functions) should be
for modification.

module should be extendable with

the module shouldn’t need to be
ension.
es this possible, namely, to write
without having to touch the old

cts and Design CS 2340 7 / 16

Open for Modification

public class Sql {
public Sql(String table, Column[]
public String create()
public String insert(Object[] fiel
public String selectAll()
public String findByKey(String key
public String select(Column column
public String select(Criteria crit
public String preparedInsert()
private String columnList(Column[]
private String valuesList(Object[]
private String selectWithCriteri
private String placeholderList(Col

}

This class violates SRP becuase the
e.g., updating an exising statement
kinds of statements. Extension requ
modification.

Chris Simpkins (Georgia Tech) CS 2340 Objec

columns)

lds)

yColumn, String keyValue)
n, String pattern)
teria)

] columns)
] fields, final Column[] columns)
ia(String criteria)
lumn[] columns)

ere are multiple axes of change,
type (like create) or adding new
uires opening the class for

cts and Design CS 2340 8 / 16

Open for Extension, Closed

public abstract class Sql {
public Sql(String table, Column[]
public abstract String generate();

}
public class CreateSql extends Sql {

public CreateSql(String table, Col
@Override public String generate()
}
public class SelectSql extends Sql {
public SelectSql(String table, Col
@Override public String generate()
}
...

These classes are open for extensio

Extend with new functionality b

Existing classes need not be to

Note that with good OO design you
classes instead of fewer large class

Chris Simpkins (Georgia Tech) CS 2340 Objec

d for Modification

columns)
;

{
lumn[] columns)
)

{
lumn[] columns)
)

on and closed for modification.

by adding a new subclass of Sql.

ouched.

often end up with many small
sses.

cts and Design CS 2340 9 / 16

Liskov Substitution Principle

Subtypes must be substitutable

A suprising counter-example:

public class Rectangle {
public void setWidth(double w) { .
public void setHeight(double h) {

}
public class Square extends Rectangl

public void setWidth(double w) {
super.setWidth(w);
super.setHeight(w);

}
public void setHeight(double h) {

super.setWidth(h);
super.setHeight(h);
}
}

We know from math class that a

The overridden setWidth and

enforce the class invariant of Sq

height. CS 2340 Objec

Chris Simpkins (Georgia Tech)

e (LSP)

e for their supertypes.

... }
... }

le {

a square “is a” rectangle.
d setHeight methods in Square
quare, namely, that width ==

cts and Design CS 2340 10 / 16

LSP Violation

Consider this client of Rectangle:

public void g(Rectangle r) {
r.setWidth(5);
r.setHeight(4);
assert(r.area() == 20);

}

Client assumed width and heigh
The Object-oriented is-a relationsh

Chris Simpkins (Georgia Tech) CS 2340 Objec

ht were independent.
hip is about behavior.

cts and Design CS 2340 11 / 16

Conforming to LSP: Design

Author of a class specifies the beha
preconditions and postconditions. S

Preconditions of overriden meth
than those of the superclass (e
the constraints of the superclas
Postconditins of overriden meth
than those of the superclass (e
superclas method and possibly

Require no more, promise no le

In the Rectangle-Square case the p
setWidth method:

assert((rectangle.w == w) && (rectan

tells us that a Square doesn’t satisf
relationship to Rectangle.

Chris Simpkins (Georgia Tech) CS 2340 Objec

n by Contract

avior of each method in terms of
Subclasses must follow two rules:
hods must be equal to or weaker
enforces or assumes no more than
ss method).
hods must be equal to or greater
enforces all of the constraints of the
y more).

ess.

postcondition of Rectangle’s

ngle.height == old.height))

fy the object-oriented is-a

cts and Design CS 2340 12 / 16

Dependency Inversion Princ

a. High-level modules should n
modules. Both should depend o
b. Abstractions should not depe
depend on abstractions.
A counter-example:

public void regulate(double minTemp,
for (;;) {
while (in(THERMOMETER) > minTemp
Thread.sleep(1000);
out(FURNACE, ENGAGE);

while (inTHERMOMETER) < maxTemp)
Thread.sleep(1000);

out(FURNACE, DISENGAGE);
}
}

The regulate method depend
furnace implementation.
The regulate algorithm is clutte
ChristShimeprkimns o(GmeoregitaeTercha) nd furnaCcSe23o40pOebjreca

ciple (DIP)1

not depend on low-level
on abstractions.
end on details. Details should

, double maxTemp) {
p)

)

ds on a particular thermometer and

ered with low-level details of the

catstiaondnDses.ign CS 2340 13 / 16

DIP Example

publ
fo

}
}

Thermostat depends on inter
Concrete heaters and thermom
Thermometer and Heater
Low-level detail is removed form
Can be extended with different
Thermometer and Heater wh

Chris Simpkins (Georgia Tech) CS 2340 Objec

lic void regulate(double minTemp,
double maxTemp){

or (;;) {
while (thermometer.read() > minTemp)
Thread.sleep(1000);
heater.engage();

while (thermometer.read() < maxTemp)
Thread.sleep(1000);

heater.disengage();

rfaces Thermometer and Heater
meters also depend on

m regulate method

concrete implementations of
hile maintaining OCP

cts and Design CS 2340 14 / 16

Interface Segregation Princ

Clients should not be forced to
use.

Break up fat interfaces into a set of
depends on the small interface it ne

Additional UI methods in UI req

transaction classes, even the o

methods. CS 2340 Objec

Chris Simpkins (Georgia Tech)

ciple (ISP)

depend on methods they don’t

smaller interfaces. Each client
eeds, and none of the others.

quire recompilation of all the
ones that don’t use the new

cts and Design CS 2340 15 / 16

ISP Example

Segregated UI interfaces:

Each transaction gets its own U

Adding transactions doesn’t req
transactions or UIs.

Chris Simpkins (Georgia Tech) CS 2340 Objec

UI interface.
quire touching or recompiling other

cts and Design CS 2340 16 / 16


Click to View FlipBook Version