Catalog of Refactoring

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

Reusable Classes.  Motivation: Write less code!
About Me – Frank Xu Education ▫ North Dakota State University  Ph.D. in Software Engineering ▫ Towson University  MS in Computer Science ▫ Southeast.
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
Introduction to Refactoring Excerpted from ‘What is Refactoring?’ by William C. Wake and Refactoring: Improving the Design of Existing Code by Martin Fowler.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
OOP Languages: Java vs C++
Abstraction, Inheritance, and Polymorphism in Java.
Inheritance using Java
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
2000 Jordan Anastasiade. All rights reserved. 1 Class In this lesson you will be learning about: Class. Inheritance. Polymorphism. Nested and.
Chapter 6: Code Refactoring Omar Meqdadi SE 3860 Lecture 6 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Refactoring - A disciplined approach to rework for better design.
Refactoring Improving the structure of existing code Refactoring1.
Chapter 7: Bad Code Smells Omar Meqdadi SE 3860 Lecture 7 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
Refactoring1 Improving the structure of existing code.
Refactoring Deciding what to make a superclass or interface is difficult. Some of these refactorings are helpful. Some research items include Inheritance.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Programming in Java CSCI-2220 Object Oriented Programming.
1 CSC/ECE 517 Fall 2010 Lec. 3 Overview of Eclipse Lectures Lecture 2 “Lecture 0” Lecture 3 1.Overview 2.Installing and Running 3.Building and Running.
Today’s Agenda  More refactoring patterns Software Testing and Maintenance 1.
Refactoring 2. Admin Blackboard Quiz Acknowledgements Material in this presentation was drawn from Martin Fowler, Refactoring: Improving the Design of.
REFACTORINGREFACTORING. Realities Code evolves substantially during development Requirements changes 1%-4% per month on a project Current methodologies.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
Object Oriented Programming
1 Software Maintenance and Evolution CSSE 575: Session 3, Part 3 Dealing with Generalization Steve Chenoweth Office Phone: (812) Cell: (937)
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
CSSE 375 Organizing Data – Part 2 Shawn and Steve Continue the same quiz!
Refactoring1 Improving the structure of existing code.
AD Lecture #1 Object Oriented Programming Three Main Principles 1 Inheritance Encapsulation Polymorphism.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
Inheritance and Polymorphism
Software Construction and Evolution - CSSE 375 Dealing with Generalization Steve and Shawn Left – In the 1990 movie “The Freshman,” Matthew Broderick,
Refactoring. DCS – SWC 2 Refactoring ”A change made to the internal structure of software to make it easier to understand and cheaper to modify without.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Module 9. Dealing with Generalization Course: Refactoring.
Object Oriented Programming in Java Habib Rostami Lecture 7.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Software Construction Lab 05 Abstraction, Inheritance, and Polymorphism in Java.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Catalog of Refactoring (6) Making Method Calls Simpler.
Principles and examples
Sections 3.4 Formal Specification
Catalog of Refactoring
Module Road Map Refactoring Why Refactoring? Examples
Interfaces.
Inheritance and Polymorphism
Refactoring Methods: Kevin Murphy.
Extract Subclass, Extract Superclass and Extract Hierarchy
CSC 113 Tutorial QUIZ I.
Overview of Eclipse Lectures
MSIS 670 Object-Oriented Software Engineering
Polymorphism and access control
Week 6 Object-Oriented Programming (2): Polymorphism
Improving the structure of existing code
OO Design with Inheritance
Delegation vs inheritance
Overview of C++ Polymorphism
Chapter 14 Abstract Classes and Interfaces
CIS 199 Final Review.
Refactoring.
Presentation transcript:

Catalog of Refactoring (7) Dealing with Generalization

Catalog Pull Up Field Pull Up Method Push Down Method Push Down Field Pull Up Constructor Body Replace Constructor with Factory Method Form Template Method Extract Subclass Extract Superclass Extract Interface Collapse Hierarchy Replace Inheritance with Delegation Replace Delegation with Inheritance

Pull Up Field Two subclasses have the same field. Move the field to the superclass. Each refactoring method name is written in blue.

Pull Up Method You have methods with identical results on subclasses. Move them to the superclass.

Mechanism Inspect the methods to ensure they are identical. If the methods look like they do the same thing but are not identical, use algorithm substitution on one of them to make them identical. If the methods have different signatures, change the signatures to the one you want to use in the superclass Create a new method in the superclass, copy the body of one of the methods to it, adjust, and compile If the method calls another method that is present on both subclasses but not the superclass, declare an abstract method on the superclass Delete one subclass method and testing until only the superclass method remains

Pull Up Constructor Body You have constructors on subclasses with mostly identical bodies. Create a superclass constructor; call this from the subclass methods.

Mechanics Define a superclass constructor. Move the common code at the beginning from the subclass to the superclass constructor. Call the superclass constructor as the first step in the subclass constructor If there is any common code later, use Extract Method to factor out common code and use Pull Up Method to pull it up Compile and test in every step

Example class Employee... protected String _name; protected String _id; class Manager extends Employee... public Manager (String name, String id, int grade) { _name = name; _id = id; _grade = grade; } private int _grade;

Example class Employee protected Employee (String name, String id) { _name = name; _id = id; } public Manager (String name, String id, int grade) { super (name, id); _grade = grade;

Another Example class Employee... boolean isPriviliged() {..} void assignCar() {..} class Manager... public Manager (String name, String id, int grade) { super (name, id); _grade = grade; if (isPriviliged()) assignCar(); //every subclass does this } boolean isPriviliged() { return _grade > 4;

Another Example class Employee... void initialize() { if (isPriviliged()) assignCar(); } class Manager... public Manager (String name, String id, int grade) { super (name, id); _grade = grade; initialize();

Push Down Method Behavior on a superclass is relevant only for some of its subclasses. Move it to those subclasses.

Mechanics Declare a method in all subclasses and copy the body into each subclass. Remove method from superclass. If it makes sense to access the method through a superclass variable, you don't intend to remove the method from any subclasses, and the superclass is abstract, you can declare the method as abstract, in the superclass Remove the method from each subclass that does not need it. Compile and test each step.

Push Down Field A field is used only by some subclasses. Move the field to those subclasses.

Extract Subclass A class has features that are used only in some instances. Create a subclass for that subset of features.

Mechanics Define a new subclass of the source class. Provide constructors for the new subclass Find all calls to constructors of the superclass. If they need the subclass, replace with a call to the new constructor. One by one use Push Down Method and Push Down Field to move features onto the subclass Look for any field that designates information now indicated by the hierarchy (usually a boolean or type code). Eliminate it by using Self Encapsulate Field and replacing the getter with polymorphic constant methods. All users of this field should be refactored with Replace Conditional with Polymorphism. Compile and test every step.

Example class JobItem ... public JobItem (int unitPrice, int quantity, boolean isLabor, Employee employee) { _unitPrice = unitPrice; _quantity = quantity; _isLabor = isLabor; _employee = employee; } public int getTotalPrice() { return getUnitPrice() * _quantity; public int getUnitPrice(){ return (_isLabor) ? _employee.getRate() : _unitPrice; public int getQuantity(){ return _quantity; public Employee getEmployee() { return _employee; private int _unitPrice; private int _quantity; private Employee _employee; private boolean _isLabor; class Employee... public Employee (int rate) { _rate = rate; public int getRate() { return _rate; private int _rate;

Example class JobItem... protected JobItem (int unitPrice, int quantity, boolean isLabor) { _unitPrice = unitPrice; _quantity = quantity; protected boolean isLabor() { return false; } public int getUnitPrice(){ return _unitPrice; class LaborItem extends JobItem public LaborItem (int quantity, Employee employee) { super (0, quantity, true); _employee = employee; return true; return _employee.getRate();

Extract Superclass You have two classes with similar features. Create a superclass and move the common features to the superclass.

Example class Employee... public Employee (String name, String id, int annualCost) { _name = name; _id = id; _annualCost = annualCost; } public int getAnnualCost() { return _annualCost; public String getId(){ return _id; public String getName() { return _name; private String _name; private int _annualCost; private String _id;

public class Department... public Department (String name) { _name = name; } public int getTotalAnnualCost(){ Enumeration e = getStaff(); int result = 0; while (e.hasMoreElements()) { Employee each = (Employee) e.nextElement(); result += each.getAnnualCost(); return result; public int getHeadCount() { return _staff.size(); public Enumeration getStaff() { return _staff.elements(); public void addStaff(Employee arg) { _staff.addElement(arg); public String getName() { return _name; private String _name; private Vector _staff = new Vector();

Example class Party... abstract public int getAnnualCost() protected Party (String name) { _name = name; } private String _name; class Employee extends Party public Employee (String name, String id, int annualCost) { super (name); _id = id; _annualCost = annualCost; class Department extends Party public Department (String name) { public int getAnnualCost(){ Enumeration e = getStaff(); int result = 0; while (e.hasMoreElements()) { Party each = (Party) e.nextElement(); result += each.getAnnualCost(); return result;

Extract Interface Several clients use the same subset of a class's interface, or two classes have part of their interfaces in common.. Extract the subset into an interface.

Example double charge(Employee emp, int days) { int base = emp.getRate() * days; if (emp.hasSpecialSkill()) return base * 1.05; else return base; } interface Billable { public int getRate(); public boolean hasSpecialSkill(); class Employee implements Billable ... double charge(Billable emp, int days) {

Collapse Hierarchy A superclass and subclass are not very different. Merge them together.

Replace Inheritance with Delegation A subclass uses only part of a superclasses interface or does not want to inherit data. Create a field for the superclass, adjust methods to delegate to the superclass, and remove the subclassing.

Example class MyStack { class MyStack extends Vector { public void push(Object element) { insertElementAt(element,0); } public Object pop() { Object result = firstElement(); removeElementAt(0); return result; class MyStack { private Vector _vector = new Vector(); public boolean isEmpty() { return _vector.isEmpty(); } public void push(Object element) { _vector.insertElementAt(element,0); public Object pop() { Object result = _vector.firstElement(); _vector.removeElementAt(0); return result;