1 / 12

Computer Science 209

Computer Science 209. Software Development Refactoring. Design vs Redesign. Modern software is developed via rapid prototyping, also known as extreme programming (cf. Beck, Extreme Programming Explained Boston: Addison-Wesley, 2000).

hector
Télécharger la présentation

Computer Science 209

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computer Science 209 Software Development Refactoring

  2. Design vs Redesign • Modern software is developed via rapid prototyping, also known as extreme programming (cf. Beck, Extreme Programming Explained Boston: Addison-Wesley, 2000). • Set up a skeletal version without full functionality, try it out, then refine and add detail • Rare to have a compete design before implementation

  3. Redesign by Refactoring • Whenever you revisit code, try to improve its design by refactoring (cf. Fowler, Refactoring: Improving the Design of Existing Code, Boston: Addison-Wesley, 2000) • Examples • two or more redundant pieces of code can be placed in a single method definition • Similar data variables and methods in two or more different classes can be moved up to a common superclass

  4. Refactoring the Stack Classes <<Interface>> TrueStack Abstract Collection LinkedStack ArrayStack LinkedList ArrayList Similar data variables: list, modCount Similar methods: add, size, iterator, equals, toString, push

  5. Put ‘Em in AbstractStack <<Interface>> TrueStack Abstract Collection If these methods are written well, there should be no changes to them! Abstract Stack LinkedStack ArrayStack LinkedList ArrayList Similar data variables: list, modCount Similar methods: add, size, iterator, equals, toString, push

  6. Put ‘Em in AbstractStack <<Interface>> TrueStack Abstract Collection Abstract Stack LinkedStack ArrayStack LinkedList ArrayList AbstractStack is declared Abstract, so it will never be instantiated

  7. Put ‘Em in AbstractStack <<Interface>> TrueStack Abstract Collection Abstract Stack LinkedStack ArrayStack LinkedList ArrayList AbstractStack extends AbstractCollection and implements TrueStack The concrete stack classes just extend AbstractStack

  8. Put ‘Em in AbstractStack <<Interface>> TrueStack Abstract Collection Abstract Stack LinkedStack ArrayStack LinkedList ArrayList The variables list and modCount are declared protected in AbstractStack, so they will be visible there and in subclasses but not anywhere else

  9. Put ‘Em in AbstractStack <<Interface>> TrueStack Abstract Collection Abstract Stack LinkedStack ArrayStack LinkedList ArrayList The constructors and the methods pop and peek are still specialized in the stack implementations, so they remain where they were pop and peek are declared as abstract methods in AbstractStack

  10. AbstractStack - Responsibilities abstract public AbstractStack<E> extends AbstractCollection<E> implements TrueStack<E>{ protected List<E> list; protected int modCount; public AbstractStack(){ // Defers instantiation of modCount = 0; // list to subclasses } abstract public E peek(); abstract public E pop(); // All other required methods (complete implementations) }

  11. ArrayStack - Responsibilities public ArrayStack<E> extends AbstractStack<E>{ public ArrayStack(){ // Run the constructor of super(); // the superclass first list = new ArrayList<E>(); } public ArrayStack(Collection<E> col){ // Run my default this(); // constructor first this.addAll(col); } // Implementations of peek and pop go here }

  12. A Well-Designed OO System • Uses standard interfaces • Implements standard interfaces • Leverages existing code via inheritance • Refactors code to eliminate redundancies

More Related