Top Banner
HFOOAD Chapter 1 A Simple Application
60

HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

Dec 17, 2015

Download

Documents

Amelia Carter
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

HFOOAD Chapter 1

A Simple Application

Page 2: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

We build software to solve problems. People have problems.

Therefore, we build software for people.

Phần mềm tốt không chỉ giải được các bài toán hiện tại, mà nó có thể được bảo trì và sửa đổi để đáp ứng với những thay đổi tất

yếu mà khách hàng sẽ muốn có.

2

Page 3: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

Rick’s Guitars

‣ Maintain a guitar inventory

‣ Locate guitars for customers

3

Page 4: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

What your predecessor built

Does this make any sense to you?

4

Page 5: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

A simplified view

5

Page 6: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

But there’s a problem…

6

Rick’s GuitarsBack Room

Not in stock.

I’m looking for afender Strat.

Page 7: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

But there’s a problem…

fender ≠ Fender

7

Rick’s GuitarsBack Room

Not in stock.

I’m looking for afender Strat.

Gary Pollice
Same picture as before.
Page 8: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

The Guitar classpublic class Guitar {

private String serialNumber, builder, model, type, backWood, topWood; private double price;

public Guitar(String serialNumber, double price, String builder, String model, String type, String backWood, String topWood) { this.serialNumber = serialNumber; this.price = price; this.builder = builder; this.model = model; this.type = type; this.backWood = backWood; this.topWood = topWood; } public String getSerialNumber() {return serialNumber;} public double getPrice() {return price;} public void setPrice(float newPrice) { this.price = newPrice;} public String getBuilder() {return builder;} public String getModel() {return model;} public String getType() {return type;} public String getBackWood() {return backWood;} public String getTopWood() {return topWood;}} 8

Page 9: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

public class Inventory {

private List guitars;

public Inventory() { guitars = new LinkedList(); }

public void addGuitar(String serialNumber, double price, String builder, String model, String type, String backWood, String topWood) { Guitar guitar = new Guitar(serialNumber, price, builder, model, type, backWood, topWood); guitars.add(guitar); }

public Guitar getGuitar(String serialNumber) { for (Iterator i = guitars.iterator(); i.hasNext(); ) { Guitar guitar = (Guitar)i.next(); if (guitar.getSerialNumber().equals(serialNumber)) { return guitar; } } return null; }

The Inventory class

9

Page 10: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

The Inventory class continued public Guitar search(Guitar searchGuitar) { for (Iterator i = guitars.iterator(); i.hasNext(); ) { Guitar guitar = (Guitar)i.next(); String builder = searchGuitar.getBuilder().toLowerCase(); if ((builder != null) && (!builder.equals("")) && (!builder.equals(guitar.getBuilder().toLowerCase()))) continue; String model = searchGuitar.getModel().toLowerCase(); if ((model != null) && (!model.equals("")) && (!model.equals(guitar.getModel().toLowerCase()))) continue; String type = searchGuitar.getType().toLowerCase(); if ((type != null) && (!searchGuitar.equals("")) && (!type.equals(guitar.getType().toLowerCase()))) continue; String backWood = searchGuitar.getBackWood().toLowerCase(); if ((backWood != null) && (!backWood.equals("")) && (!backWood.equals(guitar.getBackWood().toLowerCase()))) continue; String topWood = searchGuitar.getTopWood().toLowerCase(); if ((topWood != null) && (!topWood.equals("")) && (!topWood.equals(guitar.getTopWood().toLowerCase()))) continue; return guitar; } return null; }} 10

Page 11: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

What can you do?

1

2

3

4

11

Page 12: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

What can you do?

case insensitive khi so sánh maker

Lúc nào cũng dùng case insensitive khi so sánh string

Dùng constant cho các maker khác nhau

Nói chuyện với Rick để hỏi chi tiết về bài toán

1

2

3

4

12

Page 13: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

Three steps to great software

1. Đảm bảo rằng phần mềm làm đúng những gì khách hàng muốn

2. Áp dụng tốt các nguyên lí hướng đối tượng

3. Cố gắng đạt được một thiết kế bảo trì được, tái sử dụng được

13

Page 14: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

Step 1: Talk to Rick

What questions will you ask Rick?

14

Page 15: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

Questions for Rick

1

2

3

4

15

Page 16: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

Questions for Rick

Do you only sell guitars?

How will you update the inventory?

How should a search for a guitar really work?

Do you need reports about inventory and sales?

1

2

3

4

16

Page 17: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

Rick says

Khách hàng không phải lúc nào cũng biết tất cả

các đặc điểm của cây đàn họ muốn.

Thường có nhiều hơn 01 cây đàn khớp với

nhu cầu khách hàng.

Khách hàng thường tìm một cây đàn trong một

khoảng giá cụ thểTôi có cần báo cáo kho hàng

và các chức năng khác, nhưng vấn đề số 1 của tôi là

tìm được đúng cây ghi-ta cho khách hàng.

17

Page 18: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

To do list:1. If there are guitars in

stock that fit the customer’s needs, always find them.

2. Take into account typing mistakes by the customer or make it impossible to enter erroneous data.

18

Page 19: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

Iteration 1: Remove stringspublic class Guitar {

private String serialNumber, model; private double price; private Builder builder; private Type type; private Wood backWood, topWood;…

public enum Type {

ACOUSTIC, ELECTRIC;

public String toString() { switch(this) { case ACOUSTIC: return "acoustic"; case ELECTRIC: return "electric"; default: return "unspecified"; } }} 19

Page 20: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

A better enum implementation

public enum Type { ACOUSTIC("acoustic"), ELECTRIC("electric"), UNSPECIFIED("unspecified"); String value; private Type(String value) { this.value = value; } public String toString() { return value; }}

20

Page 21: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

The impact of our changes

What else might we have to do to our application?

1

2

3

4

21

Page 22: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

The impact of our changes

Change data entry forms and code

Update the inventory database

1

2

3

4

22

Page 23: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

Rick says

Khách hàng không phải lúc nào cũng biết tất cả

các đặc điểm của cây đàn họ muốn.

Thường có nhiều hơn 01 cây đàn khớp với

nhu cầu khách hàng.

Khách hàng thường tìm một cây đàn trong một

khoảng giá cụ thểTôi có cần báo cáo kho hàng

và các chức năng khác, nhưng vấn đề số 1 của tôi là

tìm được đúng cây ghi-ta cho khách hàng.

23

Page 24: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

To do list:

1. If there are guitars in stock that fit the customer’s needs, always find them.

2. Take into account typing mistakes by the customer or make it impossible to enter erroneous data.

3. Find ALL matching guitars.

24

Page 25: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

We can return an array of Guitars.

Why not a List of Guitars?

Does it really matter?

25

Page 26: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

Solution

public Iterator<Guitar> search(Guitar searchGuitar) { List<Guitar> matchingGuitars = new LinkedList<Guitar>(); for (Iterator<Guitar> i = guitars.iterator(); i.hasNext(); ) { Guitar guitar = i.next(); // Ignore serial number since that's unique // Ignore price since that's unique if (searchGuitar.getBuilder() != guitar.getBuilder()) continue; String model = searchGuitar.getModel().toLowerCase(); if ((model != null) && (!model.equals("")) && (!model.equals(guitar.getModel().toLowerCase()))) continue; if (searchGuitar.getType() != guitar.getType()) continue; if (searchGuitar.getBackWood() != guitar.getBackWood()) continue; if (searchGuitar.getTopWood() != guitar.getTopWood()) continue; matchingGuitars.add(guitar); } return matchingGuitars.iterator(); }}

26

Page 27: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

Something is still wrong

Tôi tưởng anh có bộ sưu tập ghi-ta Gibson lớn nhất bang. Tôi rất muốn một cái, nhưng cái hệ thống bán hàng đần độn của anh bảo rằng anh chẳng có cây Gibson nào.Tôi chỉ muốn một cây Gibson. Tôi chẳng quan tâm ghi-ta điện hay ghi-ta dây, gỗ gì cũng được. Vậy mà anh không có cây nào như vậy! Bán đàn kiểu quái gì vậy?

27

Page 28: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

Rick is not happy

Tôi tưởng các anh sửa rồi? Tôi có cả lô Gibson. Các anh nhận tiền công để làm gì?

28

Page 29: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

Why didn’t the Gibsons show up?

Maybe there reallywere none in stock.

What did the customer

enter?

29

Page 30: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

Something’s still wrong

I just entered “Gibson” and left everything else empty. That’s right, isn’t it?

30

Page 31: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

Three steps to great software

Does the software do what the customer wants?

What’s the problem?

1. Make sure the software does what the customer wants

2. Apply good object-oriented principles

3. Strive for a maintainable, reusable design

31

Page 32: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

Things that might be wrong

1

2

3

4

32

Page 33: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

Things that might be wrong

Not all properties are relevant

There is no wildcard value

There’s no way to select more than one value for a property…

1

2

3

4

33

Page 34: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

You know, when a customer searches for a guitar, they’re providing a set of characteristics they are looking for, not a specific guitar. Maybe that’s the problem.

34

Page 35: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

A GuitarSpec is not a Guitar

GuitarSpec Guitar

I am not a Guitar.

35

Page 36: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

We can use UML to show this

36

Page 37: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

public class GuitarSpec { private Builder builder; private String model; private Type type; private Wood backWood; private Wood topWood;

public GuitarSpec(Builder builder, String model, Type type, Wood backWood, Wood topWood) { this.builder = builder; this.model = model; this.type = type; this.backWood = backWood; this.topWood = topWood; }

public Builder getBuilder() { return builder; }…

Incremental changes

37

Page 38: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

Incremental changes (2)public class Guitar {

private String serialNumber; private double price; GuitarSpec spec;

public Guitar(String serialNumber, double price, Builder builder, String model, Type type, Wood backWood, Wood topWood) { this.serialNumber = serialNumber; this.price = price; this.spec = new GuitarSpec(builder, model, type, backWood, topWood); }

public GuitarSpec getSpec() { return spec; }}

38

Page 39: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

What’s in a name?

Is GuitarSpec a good name for the new class?

What other names might fit? Are the better or worse?

39

Page 40: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

Does this make sense?

40

Page 41: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

Things that might be wrong

Not all properties are relevant

There is no wildcard value

There’s no way to select more than one value for a property…

1

2

3

4

41

Did the changes we just made help?

Page 42: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

What principles did we apply?

Are there any more that come to mind?

1. Make sure the software does what the customer wants

2. Apply good object-oriented principles

3. Strive for a maintainable, reusable design

Three steps to great software

42

Page 43: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

A word on increments & iterations‣ Iterative development

‣ Repeating the steps in a development process over and over

‣ Shorter iterations are better

‣ Incremental development

‣ Add a bit at a time

‣ No big bang

43

Start

Goal

Page 44: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

A lot of customers are looking for 12-string guitars. Maybe we’d better add that to the guitars’ characteristics we record.

According to the contract, that’s not what you asked for.

44

Page 45: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

We can add the number of strings. It’s not a problem.

45

Page 46: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

What do you need to change?

1

2

3

4

46

Page 47: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

What do you need to change?

1

2

3

4

GuitarSpec

Inventory

47

Page 48: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

public class GuitarSpec { private Builder builder; private String model; private Type type; private int numStrings; private Wood backWood; private Wood topWood;

public GuitarSpec(Builder builder, String model, Type type, int numStrings, Wood backWood, Wood topWood) { this.builder = builder; this.model = model; this.type = type; this.numStrings = numStrings; this.backWood = backWood; this.topWood = topWood; }… public int getNumStrings() { return numStrings; }…} 48

Changes to GuitarSpec

Page 49: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

public Iterator<Guitar> search(GuitarSpec searchSpec) { List<Guitar> matchingGuitars = new LinkedList<Guitar>(); for (Iterator<Guitar> i = guitars.iterator(); i.hasNext(); ) { Guitar guitar = i.next(); GuitarSpec spec = guitar.getSpec(); if (searchSpec.getBuilder() != spec.getBuilder()) continue; String model = searchSpec.getModel().toLowerCase(); if ((model != null) && (!model.equals("")) && (!model.equals(spec.getModel().toLowerCase()))) continue; if (searchSpec.getType() != spec.getType()) continue; if (searchSpec.getBackWood() != spec.getBackWood()) continue; if (searchSpec.getTopWood() != spec.getTopWood()) continue; if (searchSpec.getNumStrings() != spec.getNumStrings()) continue; matchingGuitars.add(guitar); } return matchingGuitars.iterator(); }

49Changes to Inventory

Page 50: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

Cool. That’s just what I wanted. You’re the best. Next time you’ve got my business again.

50

Page 51: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

Three steps to great software

That wasn’t so hard!

1. Make sure the software does what the customer wants

2. Apply good object-oriented principles

3. Strive for a maintainable, reusable design

51

Page 52: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

Can we do better?

Why did we have to modify two files to make the change?

52

Page 53: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

Maybe we’ve allocated the responsibilities incorrectly. There’s a pattern called Information Expert that might be appropriate here.

O-O design is all about assigning responsibilities to the objects in our system.

53

Page 54: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

Information Expert

Assign responsibility to the class that has the essential information—the information expert.

Craig Larman, “Applying UML and Patterns”

54

Page 55: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

Think about this?

Who is the information expert?

What should we do?

What behavior is misplaced?

55

Page 56: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

Think about this?

Matching the guitar to the specification

GuitarSpec

Make GuitarSpec reasonable for determining

if it matches a guitar.

Who is the information expert?

What should we do?

What behavior is misplaced?

56

Page 57: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

The new GuitarSpec class

Gee, this looks sort of familiar!

public class GuitarSpec {… public boolean matches(GuitarSpec otherSpec) { if (builder != otherSpec.builder) return false; if ((model != null) && (!model.equals("")) &&(!model.toLowerCase().equals(otherSpec.model.toLowerCase()))) return false; if (type != otherSpec.type) return false; if (numStrings != otherSpec.numStrings) return false; if (backWood != otherSpec.backWood) return false; if (topWood != otherSpec.topWood) return false; return true; }…}

57

Page 58: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

The new Inventory class

Public class Inventory {… public List search(GuitarSpec searchSpec) { List matchingGuitars = new LinkedList(); for (Iterator i = guitars.iterator(); i.hasNext(); ) { Guitar guitar = (Guitar)i.next(); if (guitar.getSpec().matches(searchSpec)) matchingGuitars.add(guitar); } return matchingGuitars; }…}

58

Page 59: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

Congratulations!

‣ You’ve just performed your first refactoring

‣ You’ve made the customer happy

‣ You’ve applied solid O-O principles

‣ You have a maintainable application

‣ You’ve created great software, right?

Go out and celebrate at the coffee shop

59

Page 60: HFOOAD Chapter 1 A Simple Application. We build software to solve problems. People have problems. Therefore, we build software for people. Phần mềm tốt.

Do you think we might have broken something when we made those last changes?

How can you be sure?

60