Download presentation
Presentation is loading. Please wait.
Published byDebra Dawson Modified over 9 years ago
1
Nothing Stays the Same Chapter 5 HeadFirst OOAD Tom Perkins - NTPCUG
2
Inventory addGuitar(string,double,GuitarSpec) getGuitar(string):Guitar search(GuitarSpec):Guitar[*] GuitarSpec Guitar serialNumber:string price:double getSerialNumber():string getPrice():double setPrice():double getSpec():GuitarSpec TypeBuilderWood model:string numStrings:int getBuilder():Builder getModel():String getType():Type getBackWood():Wood getTopWood():Wood getNumStrings():int matches(GuitarSpec):bool toString():string toString:string inventory * spec 1 builder 1 type 1 topWood 1 backWood 1
3
Let’s Sell Mandolins!
4
Design Decision Guitar and Mandolin have many shared properties Create an Instrument class to store common properties and methods – abstract Base class Need new concrete classes Mandolin and MandolinSpec to handle mandolins Mandolin and Guitar will become “inherited classes”, subclasses, derived classes
5
Inherited Classes A new class, retains all members and functionality of a previously defined class You can add new members and functionality to new class Derived class extends base class Override functionality of base class Public Class Truck End Class Public Class PickupTruck Inherits Truck End Class public class Truck{} public class PickupTruck : Truck {}
6
Abstract Classes Used to provide some invariant functionality in a class Cannot be instantiated – cannot create an object from an abstract class Some members can be overridden in inherited classes Public MustInherit MyAbstractClass End Class public abstract MyAbstractClass {}
7
Abstract Classes Placeholders for actual implementation classes The abstract class defines the behavior; the subclasses implement that behavior
8
Guitar serialNumber:string price:double getSerialNumber():string getPrice():double setPrice():double getSpec():GuitarSpec Mandolin getSpec():MandolinSpec Instrument serialNumber:string price:double getSerialNumber():string getPrice():double setPrice():double getSpec():GuitarSpec Abstract class Attributes common to Mandolin and Guitar moved to Instrument Can’t create an instance of Instrument Abstract class defines some basic behavior Subclasses implement behavior
9
GuitarSpec builder:Builder model:string type:Type backWood:Wood topWood:Wood numStrings:int getBuilder():Builder getModel():String getType():Type getBackWood():Wood getTopWood():Wood getNumStrings():int matches(GuitarSpec):bool MandolinSpec builder:Builder model:string type:Type style:Style backWood:Wood topWood:Wood numStrings:int getBuilder():Builder getModel():String getType():Type getStyle():Style getBackWood():Wood getTopWood():Wood getNumStrings():int matches(MandolinSpec):bool Style toString():string
10
Inventory addInstrument(string,double,GuitarSpec) get(string):Guitar search(GuitarSpec):Guitar[*] Search(MandolinSpec):Mandolin[*] Instrument serialNumber:string price:double getSerialNumber():string getPrice():double setPrice():double getSpec():GuitarSpec Mandolin Guitar Full Diagram, Part I
11
InstrumentSpec model:string getBuilder():string getModel():string getType():Type getBackWood():Wood getTopWood():Wood matches(InstrumentSpec):b oolean Mandolin getNumStrings():Int matches(GuitarSpec):bool Guitar Type Builder Wood toString():string toString:string builder 1 type 1 topWood 1 backWood 1 getStyle():Style matches(MandolynSpec):bool Style toString():string Full Diagram, Part II
12
RicksGuitarsAndMandolins
13
Rick goes Bonkers!
14
Has Rick’s application become your career? For each added instrument, we have to create: –Instrument class –Create a new subclass for Instrument Spec (BanjoSpec, FiddleSpec, etc) –Write a new search method for that instrument in the Inventory class … “What a revolting development this is!” (William Bendix in The Life of Riley)
15
Some OO Principles “Code to an Interface, not an implementation” Encapsulation –Eliminate duplicate code –Encapsulate what varies (less probablity of change) Cohesion –A class should do only one thing!
16
Toward one search Method … public ArrayList search(GuitarSpec searchSpec) { ArrayList matchingGuitars = new ArrayList(); foreach (Instrument g in inventory) { if (g.getSpec().matches(searchSpec)) matchingGuitars.Add(g); } return matchingGuitars; } public ArrayList search(MandolinSpec searchSpec) { ArrayList matchingMandolins = new ArrayList(); foreach (Instrument g in inventory) { if (g.getSpec().matches(searchSpec)) matchingMandolins.Add(g); } return matchingMandolins; } Try making InstrumentSpec a concrete class
17
Improved (single) Search method public ArrayList search(InstrumentSpec searchSpec) { ArrayList matchingInstruments = new ArrayList(); foreach (Instrument g in inventory) { if (g.getSpec().matches(searchSpec)) matchingInstruments.Add(g); } return matchingInstruments; } See RicksConcreteInstrumentSpec.sln Uses concrete InstrumentSpec, single search method
18
“What? A minor flaw in my design? Impossible!” It’s hard to change your own designs Code once, look twice –“Measure twice, saw once!” –Keep looking at your design when you run into problems Peer programming Design is iterative – not a single shot! Major problem:
19
Modified Design Individual InstrumentSpec Classes InstrumentSpec properties:HashTable builder: Builder model:string type:InstrumentType backWood:Wood topWood:Wood getProperty(string):Object getProperties():HashTable getBuilder():string getModel():string getType():Type getBackWood():Wood getTopWood():Wood matches(InstrumentSpec):boolean
20
Code Walkthru and Demo
21
Useful Links Good discussion of constructors in C#: http://www.yoda.arachsys.com/csharp/constructors.html http://www.yoda.arachsys.com/csharp/constructors.html Java to C# conversion –www.25hoursaday.com/CsharpVsJava.htmlwww.25hoursaday.com/CsharpVsJava.html
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.