1 Software Maintenance and Evolution CSSE 575: Session 3, Part 3 Dealing with Generalization Steve Chenoweth Office Phone: (812) 877-8974 Cell: (937) 657-3885.

Slides:



Advertisements
Similar presentations
12-Dec-14 Refactoring IV. Previously discussed bad smells Duplicated code — and other forms of redundancy Long method — use short methods that delegate.
Advertisements

Reusable Classes.  Motivation: Write less code!
© 2010 Shawn A. Bohner Software Construction and Evolution - CSSE 375 Even more Bad Smells in Code Shawn & Steve Q1 Shawn & Steve Hint 
1 Software Maintenance and Evolution CSSE 575: Session 2, Part 2 Composing Methods Steve Chenoweth Office Phone: (812) Cell: (937)
Refactoring Overview  What is refactoring?  What are four good reasons to refactor?  When should you refactor?  What is a bad smell (relative to refactoring.
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
Software Construction and Evolution - CSSE 375 Bad Smells in Code Shawn Bohner & Steve Chenoweth.
ITEC200 – Week03 Inheritance and Class Hierarchies.
1 Software Maintenance and Evolution CSSE 575: Session 3, Part 4 Big Refactorings Steve Chenoweth Office Phone: (812) Cell: (937)
1 Software Maintenance and Evolution CSSE 575: Session 2, Part 3 Moving Features Between Objects Steve Chenoweth Office Phone: (812) Cell: (937)
1 Software Maintenance and Evolution CSSE 575: Session 1, Part 4 Even more Bad Smells in Code Steve Chenoweth Office Phone: (812) Cell: (937)
1 Software Maintenance and Evolution CSSE 575: Session 6, Part 4 Breaking Dependencies Steve Chenoweth Office Phone: (812) Cell: (937)
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.
CPSC150 Abstract Classes and Interfaces Chapter 10.
REFACTORING Improving the Design of Existing Code Atakan Şimşek e
CPSC150 Abstract Classes Chapter 10. CPSC150 Directory Example (note: your assignment does not have all of this) DirectoryEntry name phone public void.
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.
Abstraction, Inheritance, and Polymorphism in Java.
Inheritance using Java
JUnit The framework. Goal of the presentation showing the design and construction of JUnit, a piece of software with proven value.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
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.
Refactoring - A disciplined approach to rework for better design.
Refactoring Improving the structure of existing code Refactoring1.
1 Software Maintenance and Evolution CSSE 575: Session 3, Part 1 Simplifying Conditionals Steve Chenoweth Office Phone: (812) Cell: (937)
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.
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.
Software Engineering CS3003 Lecture 4 Code bad smells and refactoring.
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.
Session 18 Chapter 8: Understanding Inheritance. Recall Exercise 2 From Tuesday It’s very annoying to move a target from the pallet and drop it in the.
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.
1 Software Maintenance and Evolution CSSE 575: Session 2, Part 1 Refactoring Principles Steve Chenoweth Office Phone: (812) Cell: (937)
Software Construction and Evolution - CSSE 375 Making Method Calls Simpler Shawn and Steve Below – “Be the character!” The late acting teacher Lee Strasberg.
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.
Pertemuan 12 Refactoring Mata kuliah: T0144 – Advanced Topics in Software Engineering Tahun: 2010.
CET203 SOFTWARE DEVELOPMENT Session 2A Inheritance (programming in C#)
Software Construction and Evolution - CSSE 375 Simplifying Conditionals Shawn & Steve.
CSSE 375 Organizing Data – Part 1 Shawn and Steve Q1.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
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.
Software Construction Lab 05 Abstraction, Inheritance, and Polymorphism in Java.
Catalog of Refactoring (6) Making Method Calls Simpler.
A (Very) Simple Example Consolidate duplicate conditional fragments if (isSpecialDeal()) { total = price * 0.95; send (); } else { total = price * 0.98;
Principles and examples
Catalog of Refactoring
Steve Chenoweth Office Phone: (812) Cell: (937)
Catalog of Refactoring
Module Road Map Refactoring Why Refactoring? Examples
Steve Chenoweth Office Phone: (812) Cell: (937)
Steve Chenoweth Office Phone: (812) Cell: (937)
Extract Subclass, Extract Superclass and Extract Hierarchy
Software Construction and Evolution - CSSE 375 Composing Methods
Overview of Eclipse Lectures
Advanced Java Programming
Improving the structure of existing code
Refactoring Types Blake Duncan.
Refactoring.
Presentation transcript:

1 Software Maintenance and Evolution CSSE 575: Session 3, Part 3 Dealing with Generalization Steve Chenoweth Office Phone: (812) Cell: (937) hulman.edu From Generalizing code tends to make it more amenable to change!

2 Dealing with Generalization Generalization  Inheritance Some Bad Code Smells – Duplicate Code, Inappropriate Intimacy, Large Class, Lazy Class, Middle Man, Refused Bequest, Speculative Generality 1. Pull Up Field 2. Pull Up Method 3. Pull Up Constructor Body 4. Push Down Method 5. Push Down Field 6. Extract Subclass 7. Extract Superclass 8. Extract Interface 9. Collapse Hierarchy 10. Form Template Method 11. Replace Inheritance with Delegation 12. Replace Delegation with Inheritance 1. Pull Up Field 2. Pull Up Method 3. Pull Up Constructor Body 4. Push Down Method 5. Push Down Field 6. Extract Subclass 7. Extract Superclass 8. Extract Interface 9. Collapse Hierarchy 10. Form Template Method 11. Replace Inheritance with Delegation 12. Replace Delegation with Inheritance There’s a slide on all these, today!

3 Pull Up Field Situation: Two subclasses have the same field Solution: Move the field to the superclass Push Down Field for opposite situation… – If a field is used only by some subclasses, move to those subclasses

4 Pull Up Method Situation: You have methods with identical results on subclasses Solution: Move the them to the superclass Push Down Method for opposite situation… – If behavior on a superclass is relevant only for some of its subclasses, move it to those subclasses

5 Pull Up Constructor Body Situation: You have constructors on subclasses with mostly identical bodies Solution: Create a superclass constructor; call this from the subclass methods 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; }

6 Extract Subclass Situation: A class has features that are used only in some instances Solution: Create a subclass for that subset of features

7 Extract Superclass Situation: You have two classes with similar features Solution: Create a superclass and move the common features to the superclass

8 Extract Superclass: Mechanics Create a blank abstract superclass; make the original classes subclasses of this superclass. One by one, use Pull Up Field, Pull Up Method, and Pull Up Constructor Body to move common elements to the superclass. – It’s usually easier to move the fields first. – With subclass methods that have different signatures but the same purpose, rename them and then use Pull Up Method. – If you have methods with the same signature but different bodies, declare the common signature as an abstract method on the superclass. Compile and test after each pull. Examine the methods left on the subclasses. See if there are common parts, if there are you can use Extract Method followed by Pull Up Method on the common parts. If the overall flow is similar, you may be able to use Form Template Method. After pulling up all the common elements, check each client of the subclasses. If they use only the common interface you can change the required type to the superclass.

9 Exercise: Extract Superclass (1 of 6) 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; }

10 Exercise: Extract Superclass (2 of 6) 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; }

11 Exercise: Extract Superclass (3 of 6) 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();

12 Exercise: Extract Superclass (4 of 6) Create superclass with existing superclasses as subclasses abstract class Party {} class Employee extends Party... class Department extends Party... Pull Up Features to Superclass (start with fields) class Party... protected String _name; … public String getName() { return _name; }

13 Exercise: Extract Superclass (5 of 6) class Party... protected Party (String name) { _name = name; } private String _name; class Employee... public Employee (String name, String id, int annualCost) { super (name); _id = id; _annualCost = annualCost; } class Department... public Department (String name) { super (name); }

14 Exercise: Extract Superclass (6 of 6) class Department extends Party { public int getAnnualCost(){ Enumeration e = getStaff(); int result = 0; while (e.hasMoreElements()) { Employee each = (Employee) e.nextElement(); result += each.getAnnualCost(); } return result; } abstract public int getAnnualCost()

15 Exercise: Extract Superclass (7 of 6(!)) class Department extends Party { public int getAnnualCost(){ Enumeration e = getStaff(); int result = 0; while (e.hasMoreElements()) { Party each = (Party) e.nextElement(); result += each.getAnnualCost(); } return result; }

16 Extract Interface Situation: Several clients use the same subset of a class’s interface, or two classes have part of their interfaces in common Solution: Extract the subset into an interface

17 Collapse Hierarchy Situation: A superclass and subclass are not very different Solution: Merge them together

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

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

20 Replace Delegation with Inheritance Situation: You’re using delegation and are often writing many simple delegations for the entire interface Solution: Make the delegating class a subclass of the delegate

21 Where we have Been so far… Refactoring – Bad Code Smells – Composing Methods – Moving Features between Objects – Organizing Data – Simplifying Conditional Expressions – Making Method Calls Simpler – Dealing with Generalization These principles will serve you in constructing and evolving software systems

22 Assignment and Milestone Reminders Read Documentation Survey Paper, for next week – It’s under Resources, on the course web site Milestone 3: – What you did and how it worked, in summary – Processes associated with doing the refactoring – what did and what didn’t – How you generalized your project code to make it easier to add new features – And, include a reflection on some aspect of delivering this milestone, as described at the end of the journal format guide under Resources – Describe where you think you are now, on Shu Ha Ri This week’s journal, blog about Milestone 3, describing what you did, as you did it: – Include anticipated refactorings considered this week – What you tried and abandoned, etc. – How you planned what to do, and implemented it, as you did it