Docsity

Principles of Object-Oriented Design: SOLID and DRY, Study notes of Computer Science

Toggle sidebar
bg1
1/14/2009
1
OO Fundamentals
Question:
•What is the one thing in software design
and development that remains constant?
Where does it
come from?
What changes?
Principle 1: Encapsulate
Change
•Find what varies (or what will vary) and
encapsulate it.
Recall this question:
Which is better and why?
public class StudentBody1 {
private ArrayList students;
/* … */
public void addAll(ArrayList students) { /* … */ }
}
public class StudentBody2 {
private List students;
/* … */
public void addAll(List students) { /* … */ }
pf3
pf4
pf5

Partial preview of the text

Download Principles of Object-Oriented Design: SOLID and DRY and more Study notes Computer Science in PDF only on Docsity!

OO Fundamentals

Question:

• What is the one thing in software design

and development that remains constant?

Where does it

come from? What changes?

Principle 1: Encapsulate

Change

• Find what varies (or what will vary) and

encapsulate it.

Recall this question:

Which is better and why?

public class StudentBody1 {

private ArrayList students;

public void addAll(ArrayList students) { /* … */ }

public class StudentBody2 {

private List students;

public void addAll(List students) { /* … */ }

What does a List need to

support?

What does a client need to

know about List?

Principle 2: Code to an interface

rather than an implementation Cohesion

• A cohesive class does one thing and one

thing only

• The customer class keeps customer data.

It doesn’t try to compute a bill.

• The board class keeps track of the

locations of pieces, it doesn’t try to

determine legal moves.

Principle #3 Single responsibility

principle

• Every object in your system should have a

single responsibility, and all the object’s

services should be focused on carrying out

that responsibility.

Finding misplaced methods

• Does “The itself.” make

sense?

Fix the code

public Account getAcct (CID id, AcctNum act) { DataBase d = open(“bankdata”); List L=d.search (“Accounts”, id); Account a = L.member(act); return (a); }

public float getBal (CID id, AcctNum

act) {

Account a = getAcct (id, act);

if (a==null) {

return (-1.0);

} else {

List R = a.search(“*”);

return (R.sum());

public float withdraw (CID id, AcctNum act, float amount) { float balance = 0.0; Account a = getAcct(id, act); if (a==null) { return (-1.0); } else { List R = a.search(“*”); balance = R.sum(); } if (amount <= balance) { a.addTransaction (“withdraw”, amount); return (amount); } else { return 0.0; }}

Principle #5 Don’t Repeat

Yourself (DRY)

• Avoid duplicate code by abstracting out

things that are common and placing them

in a single location

Back to Checkers

• Suppose we want to allow players to play

either of two games

– Checkers as we know it

– 3D checkers

3D Checkers

Board Width, height: int Content: Object[][] addContent(int, int, object) removeContent (int, int) getObject(int, int)

Problems

• What about this code?

Board b = new 3DBoard();

Object x = getObject(4, 5);

Principle #6 Substitution

principle

• Subtypes must be substitutable for base

types

Better Solution

Board Width, height: int Content: Object[][] addContent(int, int, object) removeContent (int, int) getObject(int, int)