Computer science 209
This presentation is the property of its rightful owner.
Sponsored Links
1 / 12

Computer Science 209 PowerPoint PPT Presentation


  • 54 Views
  • Uploaded on
  • Presentation posted in: General

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).

Download Presentation

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Computer science 209

Computer Science 209

Software Development

Refactoring


Design vs redesign

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


Redesign by refactoring

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


Refactoring the stack classes

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


Put em in abstractstack

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


Put em in abstractstack1

Put ‘Em in AbstractStack

<<Interface>>

TrueStack

Abstract

Collection

Abstract

Stack

LinkedStack

ArrayStack

LinkedList

ArrayList

AbstractStack is declared Abstract, so it will never be instantiated


Put em in abstractstack2

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


Put em in abstractstack3

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


Put em in abstractstack4

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


Abstractstack responsibilities

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)

}


Arraystack responsibilities

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

}


A well designed oo system

A Well-Designed OO System

  • Uses standard interfaces

  • Implements standard interfaces

  • Leverages existing code via inheritance

  • Refactors code to eliminate redundancies


  • Login