Download presentation
Presentation is loading. Please wait.
Published byJunior Mervyn Black Modified over 9 years ago
1
Object-Oriented Analysis & Design Chapter 1 NTPCUG Study Series
2
Welcome! 10 (+) week book study Tom Perkins –tomperki@gmail.comtomperki@gmail.com Books available at Nerdbooks.com
3
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
4
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
5
Rick’s Guitar Shop Application UML Class Diagrams Guitar serialNumber:string price: double builder:string type:string backWood:string topWood:string getSerialNumber():string getPrice():double setPrice():double getBuilder():double getModel():string getType():double getBackWood():string getTopWood():string Inventory Guitars: List addGuitar(string,double,string,string, string,string,string) getGuitar(string):Guitar search(Guitar):Guitar initializeInventory: printInventory()
6
Main Application Inv:Inventory g:Guitar whatErinLikes:Guitar initializeInventory(Inventory)
7
DEMO ricksGuitar_start
8
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????
9
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)
10
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)
11
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
12
Enums enum Wood { Indian_Rosewood, Brazilian_Rosewood, Mahogany, Maple, Cocobolo, Cedar, Adirondack, Alder, Sitka }
13
Enums – code GuitarEnums class InitalizeInventory method Note Intellisense -> fewer errors
14
Enums - Code To describe Erin’s dream guitar: Guitar whatErinLikes = new Guitar("", 0, Builder.Fender, "Stratocastor", GuitarType.Electric, Wood.Alder, Wood.Alder);
15
Step 1.b – Give customers choices Search should return a list of guitars instead of a single guitar LinkedList matchingGuitars = inv.search(whatErinLikes); Old: New: Guitar guitar = inv.search(whatErinLikes); See full code in ricksGuitars_choices
16
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
17
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
18
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
19
Solution: Break object into separate parts and keep those parts separate Encapsulation Create a separate GuitarSpec object
20
Guitar GuitarSpec Guitar serialNumber:string price: double builder:string model:string type:string backWood:string topWood:string spec:GuitarSpec getSerialNumber():string getPrice():double setPrice():double getBuilder():double getModel():string getType():double getBackWood():string getTopWood():string getSpec:GuitarSpec GuitarSpec builder:string model:string type:string backWood:string topWood:string getBuilder():double getModel():string getType():double getBackWood():string getTopWood():string
21
Encapsulation Code Changes Guitar whatErinLikes = new Guitar("", 0, Builder.Fender, "Stratocastor", GuitarType.Electric,Wood.Alder, Wood.Alder); LinkedList 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 matchingGuitars = inv.search(whatErinLikes);
22
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
23
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:
24
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
25
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
26
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 GuitarSpec 2.Encapsulate GuitarSpec in Guitar class constructor 3.Delegate comparison of search object to GuitarSpec class – no longer Inventory’s job 4.Update Tester class and test everything
27
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; }
28
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));
29
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?”
30
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; }
31
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
32
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
33
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.