{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

10-gooddesignflexiblesoftwa

10-gooddesignflexiblesoftwa - Good Design = Flexible...

Info icon This preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
Good Design == Flexible Software (Part 2) Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/5448 — Lecture 10 — 09/24/2009 © University of Colorado, 2009 1
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Lecture Goals • Review material from Chapter 5, part 2, of the OO A&D textbook • Good Design == Flexible Software • How to achieve flexible software • The Importance of Iteration • The Great Ease-Of-Change Challenge • Cohesive Classes • Discuss the Chapter 5 Example: Rick’s Guitars, Revisited • Emphasize the OO concepts and techniques encountered in Chapter 5 2
Image of page 2
Review: Our Three (Most Recent) OO Principles • Code to an Interface • If you have a choice between coding to an interface or an abstract base class as opposed to an implementation or subclass, choose the former • Let polymorphism be your friend • Encapsulate What Varies • Find the locations of your software likely to change and wrap them inside a class; hide the details of what can change behind the public interface of the class • Only One Reason To Change • Each class should have only one reason that can cause it to change • (the thing it encapsulates) 3
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Rick’s Current Application 4 getBuilder(): Builder getModel(): String getType(): Type getBackWood(): Wood getTopWood(): Wood matches(InstrumentSpec): boolean model: String InstrumentSpec Builder Type Wood builder topWood backWood type getStyle(): Style matches(InstrumentSpec): boolean MandolinSpec Style style getNumStrings(): int matches(InstrumentSpec): boolean numStrings: int GuitarSpec Guitar Mandolin getSerialNumber(): String getPrice(): double setPrice(double) getSpec(): InstrumentSpec serialNumber: String price: double Instrument spec addInstrument(String, double, InstrumentSpec) get(Sring): Instrument search(GuitarSpec): Guitar [*] search(MandolinSpec): Mandolin [*] Inventory inventory *
Image of page 4
The Problems? • Inventory.addInstrument() has code specific to each instrument type • If we add a new Instrument subclass, we have to change this method. • The Inventory class has one search() method for each type of instrument • InstrumentSpec is an abstract class and we can’t instantiate it directly. • This means that we can’t search across instruments easily • Instrument subclasses don’t offer much functionality • Instrument does all the hard work. • Each time we add a new subclass to Instrument, we need to add a new subclass to InstrumentSpec 5
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
search() Upgrade • We start by looking at the problem of having multiple search() methods • one for each subclass of InstrumentSpec • This situation is simply not tenable… our system will never be flexible if we have to change Inventory’s public interface every time we want to add a new type of Instrument to our system! • Having a method for each subclass of InstrumentSpec feels like we are coding to implementations, violating the “code to an interface” principle • But, we had to do this because InstrumentSpec is abstract and we made it abstract since we wanted it to define a base class (or interface) for all of its possible subclasses • Recall that we created the GuitarSpec class in the first place to address the problem that Guitar was initially being asked to play two roles 6
Image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern