Module 9. Dealing with Generalization Course: Refactoring.

Slides:



Advertisements
Similar presentations
© 2010 Shawn A. Bohner Software Construction and Evolution - CSSE 375 Even more Bad Smells in Code Shawn & Steve Q1 Shawn & Steve Hint 
Advertisements

Refactoring Overview  What is refactoring?  What are four good reasons to refactor?  When should you refactor?  What is a bad smell (relative to refactoring.
Inheritance Inheritance Reserved word protected Reserved word super
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
ITEC200 – Week03 Inheritance and Class Hierarchies.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
Slide 1 Systems Analysis & Design CS183 Spring Semester 2008 Dr. Jonathan Y. Clark Course Website:
Introduction to Refactoring Excerpted from ‘What is Refactoring?’ by William C. Wake and Refactoring: Improving the Design of Existing Code by Martin Fowler.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Object-oriented Programming Concepts
Building Java Programs Inner classes, generics, abstract classes reading: 9.6, 15.4,
Chapter 10 Classes Continued
13-Jul-15 Refactoring II. Books Design Patterns is the classic book by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides Basically a catalog.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Abstraction, Inheritance, and Polymorphism in Java.
Abstract classes and Interfaces. Abstract classes.
Inheritance using Java
Programming Languages and Paradigms Object-Oriented Programming.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Object Oriented Programming Key Features of OO Approach Data encapsulation –data and methods are contained in a single unit, object –promotes internal.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Inheritance Abstract Classes Check out Inheritance from SVN.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
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.
Refactoring Improving the structure of existing code Refactoring1.
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.
Lecture 12 March 16, The Scope of a Variable What if there are two variables with the same name? –A local or block-local variable can have the same.
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.
Refactoring. Refactoring Overview  What is refactoring?  What are four good reasons to refactor?  When should you refactor?  What is a bad smell (relative.
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.
Refactoring II Dealing with Polymorphism. Switch in Rental Switches on Movie! class Rental … public double getCharge() { double result = 0; switch (getMovie().getPriceCode()){
Module 3. Smells Between Classes Course: Refactoring.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
Object Oriented Programming
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Interfaces About Interfaces Interfaces and abstract classes provide more structured way to separate interface from implementation
1 Software Maintenance and Evolution CSSE 575: Session 3, Part 3 Dealing with Generalization Steve Chenoweth Office Phone: (812) Cell: (937)
Refactoring. Mathematics: Factor ● fac·tor – One of two or more quantities that divides a given quantity without a remainder, e.g., 2 and 3 are factors.
CSSE 375 Organizing Data – Part 2 Shawn and Steve Continue the same quiz!
Refactoring1 Improving the structure of existing code.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Subclassing, pt. 2 Method overriding, virtual methods, abstract classes/methods COMP 401, Fall 2014 Lecture 9 9/16/2014.
Banaras Hindu University. A Course on Software Reuse by Design Patterns and Frameworks.
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.
Software Construction Lab 05 Abstraction, Inheritance, and Polymorphism in Java.
1 Inheritance One of the goals of object oriented programming is code reuse. Inheritance is one mechanism for accomplishing code reuse. It allows us to.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Principles and examples
Catalog of Refactoring
Catalog of Refactoring
Inheritance and Polymorphism
Extract Subclass, Extract Superclass and Extract Hierarchy
Overview of Eclipse Lectures
MSIS 670 Object-Oriented Software Engineering
Week 6 Object-Oriented Programming (2): Polymorphism
Advanced Java Programming
Improving the structure of existing code
Delegation vs inheritance
Chapter 14 Abstract Classes and Interfaces
Refactoring.
Presentation transcript:

Module 9. Dealing with Generalization Course: Refactoring

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

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

Pull Up Field Two subclasses have the same field. Move the field to the superclass.

Pull Up Field: Mechanism 1. Inspect all uses of the candidate fields. 2. Rename the fields. 3. Compile and test. 4. Create a new field in the superclass. 5. Delete the subclass fields. 6. Compile and test. 7. Self Encapsulate Field.

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

Pull Up Method: Mechanism 1. Inspect the methods. 2. Change the signatures. 3. Create a new method. 4. Delete one subclass method. 5. Compile and test. 6. Keep deleting subclass methods and testing. 7. Change a required type to the superclass.

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

Pull Up Constructor Body class Manager extends Employee... public Manager (String name, String id, int grade) { _name = name; _id = id; _grade = grade; } public Manager (String name, String id, int grade) { super (name, id); _grade = grade; }

Pull Up Constructor Body: Mechanism 1. Define a superclass constructor. 2. Move the common code. 3. Call the superclass constructor. 4. Compile and test.

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

Push Down Method: Mechanism 1. Declare a method in all subclasses. 2. Remove method from superclass. 3. Compile and test. 4. Remove if does not need it. 5. Compile and test.

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

Push Down Field: Mechanism 1. Declare the field in all subclasses. 2. Remove the field from the superclass. 3. Compile and test. 4. Remove the field from all subclasses that don't need it. 5. Compile and test.

Exercise Pull Up field, method Extract Interface

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

Extract Subclass Before:  a class has behavior used for some instances of the class and not for others After:  delegate vs inheritance  can't change the class-based behavior

Extract Subclass: Mechanism 1. Define a new subclass. 2. Provide constructors. 3. Replace Constructor with Factory Method 4. Find all calls to constructors [and replace]. 5. Move features onto the subclass. 6. Self Encapsulate Field 7. Replace Conditional with Polymorphism 8. Compile and test after each push down.

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

Extract Superclass: Mechanism 1. Create an abstract superclass. 2. Move common elements to the superclass. 3. Compile and test after each pull. 4. See if there are common parts. 5. Use only the common interface.

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.

Extract Interface Before:  use of a subset of a class's responsibilities  a class works with any class that can handle certain requests After:  make the subset of responsibilities a thing in its own right  clear in the use of the system Hints:  distinct roles in different situations  describe the outbound interface of a class

Extract Interface: Mechanism 1. Create an empty interface. 2. Declare the common operations. 3. Declare the relevant class(es). 4. Adjust client type declarations.

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

Collapse Hierarchy: Mechanism 1. Choose class. 2. Move all the behavior and data. 3. Compile and test with each move. 4. Adjust references. 5. Remove the empty class. 6. Compile and test.

Form Template Method You have two methods in subclasses that perform similar steps in the same order, yet the steps are different. Get the steps into methods with the same signature, so that the original methods become the same. Then you can pull them up.

Form Template Method

Form Template Method: Mechanism 1. Decompose the methods. 2. Pull Up Method. 3. Rename Method. 4. Compile and test after each signature change. 5. Pull Up Method. 6. Compile and test. 7. Remove the other methods 8. Compile and test.

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. Replace Inheritance with Delegation

Before:  the superclass operations aren't really true of the subclass  load of data that is not appropriate for the subclass  protected superclass methods  one thing when your intention is something else After:  only partial use of the delegated class  choose an interface part  extra delegating methods Replace Inheritance with Delegation

Replace Inheritance with Delegation: Mechanism 1. Create a field. 2. Use the delegate field. 3. Compile and test after changing each method. 4. Remove the subclass and replace the delegate. 5. For each superclass add a simple delegating method. 6. Compile and test.

You're using delegation and are often writing many simple delegations for the entire interface. Make the delegating class a subclass of the delegate. Replace Delegation with Inheritance

Before:  use all the methods of the delegate  sick of writing all those simple delegating methods Hints:  Don’t use you aren't using all the methods the delegate is shared by more than one object Replace Delegation with Inheritance

Replace Delegation with Inheritance: Mechanism 1. Make the delegating object a subclass of the delegate. 2. Compile. 3. Set the delegate field to be the object itself. 4. Remove the simple delegation methods. 5. Compile and test. 6. Replace all other delegations. 7. Remove the delegate field.

Exercise Form Template method Real Project

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