Unformatted text preview:

Good Design == Flexible SoftwareKenneth M. AndersonUniversity of Colorado, BoulderCSCI 4448/6448 — Lecture 9 — 09/23/2008Thursday, September 25, 2008Lecture Goals• Review material from Chapter 5 Part 1 of the OO A&D textbook• Good Design == Flexible Software• The problem of “It seemed like a good idea at the time”• Discuss the Chapter 5 Example: Rick’s Guitars, Revisited• Emphasize the OO concepts and techniques encountered in Chapter 52Thursday, September 25, 2008Chapter 5 Overview• Main Points• Change in software development is inevitable• In order to handle change, you need flexible software• In particular, you need to design your system to be flexible for the most common types of change that it will encounter• Designing flexibility for infrequent change is counterproductive• Unfortunately, achieving flexible designs “the first time” is really hard• And, typically, only possible after acquiring experience with a domain• Without experience, small changes can turn into big problems!3Thursday, September 25, 2008Rick is Back• The software application that we produced for Rick back in Chapter 1 has been working great…• BUT… Rick would like to start carrying mandolins alongside guitars• Lets look at the original design and talk about how to add support for Mandolins4Thursday, September 25, 2008Original Design (circa End of Chapter 1)addGuitar(String, double, GuitarSpec)getGuitar(Sring): Guitarsearch(GuitarSpec): Guitar [*]InventorygetSerialNumber(): StringgetPrice(): doublesetPrice(double)getSpec(): GuitarSpecserialNumber: Stringprice: doubleGuitargetBuilder(): BuildergetModel(): StringgetType(): TypegetBackWood(): WoodgetTopWood(): WoodgetNumStrings(): intmodel: StringnumStrings: intGuitarSpecBuilder Type Woodinventory*specbuildertopWoodbackWoodtype5Thursday, September 25, 2008How to add a Mandolin?getSerialNumber(): StringgetPrice(): doublesetPrice(double)getSpec(): GuitarSpecserialNumber: Stringprice: doubleGuitargetSerialNumber(): StringgetPrice(): doublesetPrice(double)getSpec(): MandolinSpecserialNumber: Stringprice: doubleMandolinThese classes are very similar. What should we do?6Thursday, September 25, 2008Remove Duplication Via InheritanceWow!But why is the Instrument class name in italics?7Guitar MandolingetSerialNumber(): StringgetPrice(): doublesetPrice(double)getSpec(): InstrumentSpecserialNumber: Stringprice: doubleInstrumentAlso notice: getSpec() has moved to the superclass and its return type has changed. More on this later.Thursday, September 25, 2008Abstract Classes (I)• Instrument is an abstract class• UML indicates an abstract class by setting its class name in italics•Too subtle for my taste… you can also add a stereotype with the value «abstract» under a bold class name to indicate the same thing• Abstract classes are placeholders for actual implementation classes• You can’t instantiate an abstract class directly• Recall that we talked about abstract classes when we discussed the concept of “design by contract”• The abstract class defines what behavior its “category of objects” will provide and then subclasses implement that behavior• The abstract class can provide default behaviors (code reuse)8Thursday, September 25, 2008Abstract Classes (II)• In this instance, Instrument provides method bodies for all of its methods, but you still don’t want to instantiate it directly• However, there (eventually) may be differences in behavior between Guitars and Mandolins• Those behaviors will live in the respective subclasses•In the previous diagram, Instrument is known as a base class for Mandolin and Guitar… as the book says “they base their behavior off of it”, and then extend it as needed to make sense for them•Also, we make Instrument abstract because we don’t think of it as an entity that can be instantiated. In the real world, you never hold an “instrument” in your hand, you hold trumpets, trombones, flutes, triangles, etc.9Thursday, September 25, 2008Abstract Classes (III)public abstract class Instrument {12 private String serialNumber;3 private double price;4 5 public Instrument(String serialNumber, double price) {6 this.serialNumber = serialNumber;7 this.price = price;8 }9 10 public String getSerialNumber() {11 return serialNumber;12 }13 14 public double getPrice() {15 return price;16 }17 18 public void setPrice(double price) {19 this.price = price;20 }21 22 public abstract InstrumentSpec getSpec();23 24}2526Note use of abstract keyword in class definition and method definitionWhat’s an InstrumentSpec and where’s the method body?10Thursday, September 25, 2008Abstract Classes (IV)• The method• public abstract InstrumentSpec getSpec();• is an example of an abstract class defining behavior that MUST be implemented by its subclasses• If a subclass of Instrument does not provide a method body for the getSpec() method, then it has to be declared abstract as well• Don’t be alarmed by the requirement of returning an InstrumentSpec, through the use of substitutability, we can return an instance of InstrumentSpec OR any of its subclasses• This implies that GuitarSpec is going to become a subclass of InstrumentSpec (a class we need to create)• As we will see, however, we are going down the wrong design path11Thursday, September 25, 2008Updating Instrumentpublic abstract class Instrument {12 private String serialNumber;3 private double price;4 private InstrumentSpec spec;5 6 public Instrument(String serialNumber, double price, InstrumentSpec spec) {7 this.serialNumber = serialNumber;8 this.price = price;9 this.spec = spec;10 }11 12 public String getSerialNumber() {13 return serialNumber;14 }15 16 public double getPrice() {17 return price;18 }19 20 public void setPrice(double price) {21 this.price = price;22 }23 24 public InstrumentSpec getSpec() {25 return spec;26 }27 28}2930The book implements Instrument like this.What does this imply about the Guitar and Mandolin classes?12Thursday, September 25, 2008Guitar and Mandolin as Instrument Subclassespublic class Guitar extends Instrument {1 public Guitar(String serialNumber, double price, GuitarSpec spec) {2 super(serialNumber, price, spec);3 }4}56public class Mandolin extends Instrument {7 public Mandolin(String serialNumber, double price, MandolinSpec spec) {8 super(serialNumber, price, spec);9 }10}1112Guitar and Mandolin are “empty” classes; all they do is define


View Full Document

CU-Boulder CSCI 6448 - Good Design = Flexible Software

Documents in this Course
Struts

Struts

12 pages

Adapter

Adapter

23 pages

Prototype

Prototype

16 pages

Weka

Weka

15 pages

qooxdoo

qooxdoo

16 pages

Django

Django

12 pages

Overview

Overview

22 pages

XNA

XNA

5 pages

Load more
Download Good Design = Flexible Software
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Good Design = Flexible Software and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Good Design = Flexible Software 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?