Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 209 Software Development Refactoring.

Similar presentations


Presentation on theme: "Computer Science 209 Software Development Refactoring."— 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 > TrueStack Abstract Collection ArrayStack ArrayList LinkedStack LinkedList Similar methods: add, equals, toString What about the list variable?

5 Put ‘Em in AbstractStack > TrueStack Abstract Collection ArrayStack ArrayList LinkedStack LinkedList Abstract Stack If these methods were written well, there should be no changes to them! Similar methods: add, equals, toString What about the list variable?

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

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

8 Put ‘Em in AbstractStack > TrueStack Abstract Collection ArrayStack ArrayList LinkedStack LinkedList The constructors and the methods iterator, size, push, pop, and peek are still specialized in the stack implementations, so they remain where they were Abstract Stack

9 Put ‘Em in AbstractStack > TrueStack Abstract Collection ArrayStack ArrayList LinkedStack LinkedList Because AbstractStack now implements TrueStack, the iterator, size, push, pop, and peek methods are also declared abstract in that class Abstract Stack

10 AbstractStack - Responsibilities abstract public class AbstractStack extends AbstractCollection implements TrueStack { abstract public E peek(); abstract public E pop(); // Other abstract methods go here // Completed method implementations of add, equals, and // toString go here }

11 ArrayStack - Responsibilities public class ArrayStack extends AbstractStack { public ArrayStack(Collection 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


Download ppt "Computer Science 209 Software Development Refactoring."

Similar presentations


Ads by Google