Top Banner
Object-Oriented Analysis & Design Chapter 1 NTPCUG Study Series
33

Object-Oriented Analysis & Design Chapter 1 NTPCUG Study Series.

Dec 17, 2015

Download

Documents

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: Object-Oriented Analysis & Design Chapter 1 NTPCUG Study Series.

Object-Oriented Analysis & DesignChapter 1NTPCUG Study Series

Page 2: Object-Oriented Analysis & Design Chapter 1 NTPCUG Study Series.

Welcome!

• 10 (+) week book study

• Tom Perkins– [email protected]

m

• Books available at Nerdbooks.com

Page 3: Object-Oriented Analysis & Design Chapter 1 NTPCUG Study Series.

Session Objectives

• Awareness of functional OO app• Identify 3 steps to great software design• Use enums vs string comparisons• Use encapsulation to protect program

parts• Use delegation to achieve loose coupling• Be aware of what makes a program

functional, maintainable, reusable, and flexible

Page 4: Object-Oriented Analysis & Design Chapter 1 NTPCUG Study Series.

Rick’s Guitar Store App

• Developed by “Down and Dirty Coding, Inc”

• Inventory management system• Search tool – match a customer with

their dream instrument• Handout (C# and VB)• Available on class website

Page 5: Object-Oriented Analysis & Design Chapter 1 NTPCUG Study Series.

Rick’s Guitar Shop ApplicationUML Class Diagrams

Guitar

serialNumber:string price: double builder:string type:string backWood:string topWood:string

getSerialNumber():stringgetPrice():doublesetPrice():doublegetBuilder():doublegetModel():stringgetType():doublegetBackWood():stringgetTopWood():string

Inventory

Guitars: List

addGuitar(string,double,string,string,string,string,string)

getGuitar(string):Guitar search(Guitar):GuitarinitializeInventory:printInventory()

Page 6: Object-Oriented Analysis & Design Chapter 1 NTPCUG Study Series.

Main Application

Inv:Inventoryg:GuitarwhatErinLikes:Guitar

initializeInventory(Inventory)

Page 7: Object-Oriented Analysis & Design Chapter 1 NTPCUG Study Series.

DEMOricksGuitar_start

Page 8: Object-Oriented Analysis & Design Chapter 1 NTPCUG Study Series.

Nice app, but it has a few problems …

• Doesn’t work (functionality)• Doesn’t give client choices• Heavy use of string comparisons• Dependencies

– Inventory class must know internals of Guitar class

– Maybe needs some architecture re-do

• WHERE TO START????

Page 9: Object-Oriented Analysis & Design Chapter 1 NTPCUG Study Series.

3 Steps to Great Software

1. Solve the customer’s problem (do what it is supposed to do)

2. Apply OO principles (remove duplicate code, OO techniques, etc)

3. Strive for maintainable, reusable design (patterns, refactoring)

Page 10: Object-Oriented Analysis & Design Chapter 1 NTPCUG Study Series.

Step 1 – get it to do what it should be doing

• Problem: search doesn’t return a hit (never, ever)

• What’s the problem?• Possible solutions:

– Use lowercase comparisons– Use enums instead of strings– (some problem fix, some improved

design – using enums improves design and fixes problem)

Page 11: Object-Oriented Analysis & Design Chapter 1 NTPCUG Study Series.

Enums

• Enumerations are user-defined sets of integral constants that correspond to a set of friendly names.

• Using enumerations makes your code easier to read, easier to debug, and less prone to errors caused by typographical errors. – MS eLearning series – Windows-based

Applications with C# and VB, chapter 3, lesson 2

Page 12: Object-Oriented Analysis & Design Chapter 1 NTPCUG Study Series.

Enums

enum Wood { Indian_Rosewood,

Brazilian_Rosewood, Mahogany,

Maple, Cocobolo,

Cedar, Adirondack, Alder, Sitka

}

Page 13: Object-Oriented Analysis & Design Chapter 1 NTPCUG Study Series.

Enums – code GuitarEnums class

InitalizeInventory method

Note Intellisense -> fewer errors

Page 14: Object-Oriented Analysis & Design Chapter 1 NTPCUG Study Series.

Enums - CodeTo describe Erin’s dream guitar:

Guitar whatErinLikes = new Guitar("", 0, Builder.Fender, "Stratocastor", GuitarType.Electric, Wood.Alder, Wood.Alder);

Page 15: Object-Oriented Analysis & Design Chapter 1 NTPCUG Study Series.

Step 1.b – Give customers choices

Search should return a list of guitars instead of a single guitar

LinkedList<Guitar> matchingGuitars = inv.search(whatErinLikes);

Old:

New:

Guitar guitar = inv.search(whatErinLikes);

See full code in ricksGuitars_choices

Page 16: Object-Oriented Analysis & Design Chapter 1 NTPCUG Study Series.

Step 2 – Apply some OO Principles

• Encapsulation – Break your apps/objects into logical parts– Keep those parts separate– Apples in apple boxes, oranges in orange

boxes– Code Cohesion – group like things

together• Groups (blocks of code, objects) should have

a single purpose

Page 17: Object-Oriented Analysis & Design Chapter 1 NTPCUG Study Series.

Good Object Design

• Well-designed objects only one purpose• Name of object should tell what it does

– Jet object• takeOff(), land() methods OK• takeTicket() method not OK

• Object should represent only one concept– Duck object – should not represent quacker,

rubber duckie, and getting out of the way of a foul ball

• Unused values (null properties) are a dead giveaway

Page 18: Object-Oriented Analysis & Design Chapter 1 NTPCUG Study Series.

Encapsulation Candidate –

• Guitar object as used in Search Method

Guitar whatErinLikes = new Guitar("", 0, Builder.Fender,"Stratocastor", GuitarType.Electric, Wood.Alder, Wood.Alder); Unused variables

• Clients never enter a serial number or price• Only guitar’s general properties used

– Builder, topWood, etc

Page 19: Object-Oriented Analysis & Design Chapter 1 NTPCUG Study Series.

Solution:

• Break object into separate parts and keep those parts separate

• Encapsulation• Create a separate GuitarSpec object

Page 20: Object-Oriented Analysis & Design Chapter 1 NTPCUG Study Series.

Guitar GuitarSpecGuitar

serialNumber:string price: double builder:string model:string type:string backWood:string topWood:string spec:GuitarSpec

getSerialNumber():stringgetPrice():doublesetPrice():doublegetBuilder():doublegetModel():stringgetType():doublegetBackWood():stringgetTopWood():stringgetSpec:GuitarSpec

GuitarSpec

builder:string model:string type:string backWood:string topWood:string

getBuilder():doublegetModel():stringgetType():doublegetBackWood():stringgetTopWood():string

Page 21: Object-Oriented Analysis & Design Chapter 1 NTPCUG Study Series.

Encapsulation Code Changes

Guitar whatErinLikes = new Guitar("", 0, Builder.Fender, "Stratocastor", GuitarType.Electric,Wood.Alder, Wood.Alder);

LinkedList<Guitar> matchingGuitars = inv.search(whatErinLikes);

Search now using a GuitarSpec object:

Old way

New way

GuitarSpec whatErinLikes = new GuitarSpec( Builder.Fender, "Stratocastor", GuitarType.Electric,

Wood.Alder, Wood.Alder);

LinkedList<Guitar> matchingGuitars = inv.search(whatErinLikes);

Page 22: Object-Oriented Analysis & Design Chapter 1 NTPCUG Study Series.

Encapsulation Code Changes

•Guitar class contains a GuitarSpec object – composition•Design principle – Favor composition over inheritance

•Use “has-a” instead of “is-a”

class Guitar { private string serialNumber; private double price; private GuitarSpec spec;

public Guitar(string serialNumber, double price, Builder builder, string model, GuitarType type, Wood backWood, Wood topWood) { this.serialNumber = serialNumber; this.price = price; this.spec = new GuitarSpec(builder, model, type, backWood, topWood); …

private variables

constructor

Page 23: Object-Oriented Analysis & Design Chapter 1 NTPCUG Study Series.

Encapsulation Code Changes

foreach (Guitar g in guitars) { GuitarSpec guitarSpec = g.getSpec();

if (!searchSpec.getBuilder().Equals( guitarSpec.getBuilder()) )

continue; …

Inventory search routine now uses GuitarSpec for comparisons:

Page 24: Object-Oriented Analysis & Design Chapter 1 NTPCUG Study Series.

Additional benefit of Encapsulation

• Suppose Rick wants to start carrying both 6-string and 12-string guitars– Add a numStrings field to GuitarSpec

only– Won’t need to change Guitar class

• Encapsulation – change one part of your app without changing other parts

• What to encapsulate – Isolate the parts of your app that might vary from the parts that will remain the same

Page 25: Object-Oriented Analysis & Design Chapter 1 NTPCUG Study Series.

What we’ve done

• OO Principles applied:– Functionality – does what it is supposed to do– Encapsulation – separated parts that might

vary from parts that remain the same– Flexibility – made code easier to change,

change is inevitable

• Yet to come (tune in next week …)– Polymorphism and Inheritance

• See: ricksGuitars_encapsulation

Page 26: Object-Oriented Analysis & Design Chapter 1 NTPCUG Study Series.

Step 3 – Make code reusable and extendable

• Rick wants to offer both 6-string and 12-string guitars

• Plan:1. Add numStrings property and getNumStrings()

method to GuitarSpec2. Encapsulate GuitarSpec in Guitar class

constructor3. Delegate comparison of search object to

GuitarSpec class – no longer Inventory’s job4. Update Tester class and test everything

Page 27: Object-Oriented Analysis & Design Chapter 1 NTPCUG Study Series.

2. Encapsulate GuitarSpec parameter in Guitar

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

Old:

New: public Guitar(string serialNumber, double price, GuitarSpec spec) { this.serialNumber = serialNumber; this.price = price; this.spec = spec; }

Page 28: Object-Oriented Analysis & Design Chapter 1 NTPCUG Study Series.

New constructor with encapsulated GuitarSpec

Create Guitar objects for Inventory

static void InitializeInventory(Inventory inv) { inv.addGuitar("11277", 3999.95, new GuitarSpec(Builder.Collings, "CJ", GuitarType.Acoustic,6, Wood.Indian_Rosewood,

Wood.Sitka));

inv.addGuitar("V95693", 1499.95, new GuitarSpec(Builder.Fender, "Stratocastor",

GuitarType.Electric,6, Wood.Alder, Wood.Alder));

Page 29: Object-Oriented Analysis & Design Chapter 1 NTPCUG Study Series.

3. Delegation

• Delegation – let someone else do it• Inventory search method – now has

to know the insides of the GuitarSpec object to do its comparison

• Delegate this comparison to the GuitarSpec object – “Does this other GuitarSpec object

match you?”

Page 30: Object-Oriented Analysis & Design Chapter 1 NTPCUG Study Series.

Modified GuitarSpec object – match method

public Boolean matches(GuitarSpec otherSpec) { if (builder != otherSpec.builder) return false;

if ((model != null) && (!model.Equals("")) && (!model.Equals(otherSpec.model))) 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; }

Page 31: Object-Oriented Analysis & Design Chapter 1 NTPCUG Study Series.

Using the match method

foreach (Guitar g in guitars) { if (g.getSpec().matches(searchSpec))

matchingGuitars.AddLast(g);

} return matchingGuitars;

Search method in Inventory class:

See: ricksGuitars_final for code

Page 32: Object-Oriented Analysis & Design Chapter 1 NTPCUG Study Series.

Review (and that rhymes with Whew!)

• We’ve looked at – Enums– Encapsulation– Delegation– Code Reuse

• To write good software:– Do what the customer wants– Apply OO principles– Design and code for flexibility, maintainability,

and reuse

Page 33: Object-Oriented Analysis & Design Chapter 1 NTPCUG Study Series.

Assignment – Chapter 2

• Get the book!• Slow down! Make yourself comfortable!

(Take your pants off!)• Do the exercises• Read “No Dumb Questions”• Drink lots of water• Talk about what you’ve learned• Get involved with the process• Use what you’ve learned at least one time

this week