Unformatted text preview:

Originality is Overrated OO Design Principles Kenneth M Anderson University of Colorado Boulder CSCI 4448 6448 Lecture 13 10 09 2007 University of Colorado 2007 Tuesday October 9 2007 1 Lecture Goals Review material from Chapter 8 of the OO A D textbook Object Oriented Design Principles Open Closed Principle Don t Repeat Yourself Single Responsiblity Principle Liskov Substitution Principle Aggregation and Composition Revisited Discuss the examples in Chapter 8 Emphasize the OO concepts and techniques encountered in Chapter 8 Tuesday October 9 2007 2 Originality is Overrated Corollary Only Steal from the Best various sources OO A D has been performed in various forms and in various contexts for nearly 40 years Over that time designers have developed a set of principles that ease the task of creating OO designs If you apply these principles in your own code you will be stealing from the best that the OO A D community has to offer The same is true of Design Patterns Tuesday October 9 2007 3 OO Principles What We ve Seen So Far We ve seen the following principles in action over the past eight lectures Classes are about behavior Emphasize the behavior of classes over the data of classes Don t subclass for instance for data related reasons Encapsulate what varies Provides info hiding limits impact of change increases cohesion One reason to change Limits impact of change increases cohesion Code to an Interface Promotes flexible AND extensible code Code applies across broad set of classes subclasses can be added in a straightforward fashion including at run time Tuesday October 9 2007 4 New Principles Open Closed Principle OCP Classes should be open for extension and closed for modification Don t Repeat Yourself DRY Avoid duplicate code by abstracting out things that are common and placing those things in a single location Single Responsibility Principle SRP Every object in your system should have a single responsibility and all the object s services should be focused on carrying it out Liskov Substitution Principle LSP Subtypes must be substitutable for their base types Tuesday October 9 2007 5 Open Closed Principle Classes should be open for extension and closed for modification Basic Idea Prevent or heavily discourage changes to the behavior of existing classes especially classes that exist near the root of an inheritance hierarchy If a change is required create a subclass and allow it to extend override the original behavior This means you must carefully design what methods are made public and protected in these classes private methods cannot be extended Why is this important Limits impact on code that follows Code to an Interface principle If you change the behavior of an existing class a lot of client code may need to be updated Tuesday October 9 2007 6 Example We ve seen one example of the Open Closed Principle in action InstrumentSpec matches being extended by GuitarSpec and MandolinSpec InstrumentSpec model String getBuilder Builder getModel String getType Type getBackWood Wood getTopWood Wood matches InstrumentSpec boolean GuitarSpec numStrings int getNumStrings int matches InstrumentSpec boolean Tuesday October 9 2007 MandolinSpec getStyle Style matches InstrumentSpec boolean 7 Don t Repeat Yourself Avoid duplicate code by abstracting out things that are common and placing those things in a single location Basic Idea Duplication is Bad At all levels of software engineering Analysis Design Code and Test We want to avoid duplication in our requirements use cases feature lists etc We want to avoid duplication of responsibilities in our code We want to avoid duplication of test coverage in our tests Why Incremental errors that can creep into a system when one copy is changed but the others are not Isolation of Change Requests We want to go to ONE place when responding to a change request Tuesday October 9 2007 8 Example I Duplication of Code Closing the Door in Chapter 2 BarkRecognizer recognize bark string door DogDoor open boolean open close isOpen boolean door Remote pressButton We had the responsibility for closing the door automatically in our dog door example originally livingDogDoorSimulator in the RemoteControl Class When we added a BarkRecognizer Class to the system it opened the door automatically but failed to close the door We could have placed a copy of the code to automatically close the door in BarkRecognizer but that would have violated the DRY principle Instead we moved the responsibility to the shared Door class Tuesday October 9 2007 9 Example II DRY is really about ONE requirement in ONE place We want each responsibility of the system to live in a single sensible place New Requirements for the Dog Door System Beware of Duplicates The dog door should alert the owner when something inside the house gets too close to the dog door The dog door will open only during certain hours of the day The dog door will be integrated into the house s alarm system to make sure it doesn t activate when the dog door is open The dog door should make a noise if the door cannot open because of a blockage outside The dog door will track how many times the dog uses the door When the door closes the house alarm will re arm if it was active before the door opened Tuesday October 9 2007 10 Example III New Requirements for the Dog Door System Beware of Duplicates The dog door should alert the owner when something inside the house gets too close to the dog door The dog door will open only during certain hours of the day The dog door will be integrated into the house s alarm system to make sure it doesn t activate when the dog door is open The dog door should make a noise if the door cannot open because of a blockage outside The dog door will track how many times the dog uses the door When the door closes the house alarm will re arm if it was active before the door opened Tuesday October 9 2007 11 Example III New Requirements for the Dog Door System The dog door should alert the owner when something is too close to the dog door The dog door will open only during certain hours of the day The dog door will be integrated into the house s alarm system The dog door will track how many times the dog uses the door Duplicates Removed Tuesday October 9 2007 12 Example IV Ruby on Rails makes use of DRY as a core part of its design focused configuration files no duplication of information for each request often single controller single model update single view But prior to Ruby on Rails 1 2 there was


View Full Document

CU-Boulder CSCI 6448 - OO Design Principles

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 OO Design Principles
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 OO Design Principles 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 OO Design Principles 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?