Extract Subclass, Extract Superclass and Extract Hierarchy

Slides:



Advertisements
Similar presentations
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Advertisements

Object Oriented Programming
Reusable Classes.  Motivation: Write less code!
Inheritance Writing and using Classes effectively.
About Me – Frank Xu Education ▫ North Dakota State University  Ph.D. in Software Engineering ▫ Towson University  MS in Computer Science ▫ Southeast.
Department of computer science N. Harika. Inheritance Inheritance is a fundamental Object Oriented concept A class can be defined as a "subclass" of another.
Java boot camp1 Subclasses Concepts: The subclass and inheritance: subclass B of class A inherits fields and methods from A. A is a superclass of B. Keyword.
Unit 031 Interfaces What is an Interface? Interface Declaration Syntax Implementing Interfaces Using Interfaces as Types Interfaces and Inheritance Interfaces.
Unit 031 Interfaces What is an Interface? Interface Declaration Syntax Implementing Interfaces Using Interfaces as Types Interfaces and Inheritance Interfaces.
Inheritance and Polymorphism CS351 – Programming Paradigms.
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.
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
Java File Structure.  File class which is defined by java.io does not operate on streams  deals directly with files and the file system  File class.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
Chapter 6: Code Refactoring Omar Meqdadi SE 3860 Lecture 6 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
1 Biggest issue!!! You can’t do questions on this topic correctly unless you draw variables, draw objects when they are created, and draw frames for method.
Refactoring - A disciplined approach to rework for better design.
Chapter 7: Bad Code Smells Omar Meqdadi SE 3860 Lecture 7 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
C#/Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.
Chapter 6—Objects and Classes The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Objects and Classes C H A P T E R 6 To beautify.
1 Software Maintenance and Evolution CSSE 575: Session 3, Part 1 Simplifying Conditionals Steve Chenoweth Office Phone: (812) Cell: (937)
Refactoring Deciding what to make a superclass or interface is difficult. Some of these refactorings are helpful. Some research items include Inheritance.
Mohammed Al-Dhelaan CSci 253 Object Oriented Design Instructor: Brad Taylor 06/02/2009 Factory Method Pattern.
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.
Today’s Agenda  More refactoring patterns Software Testing and Maintenance 1.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
CS 61B Data Structures and Programming Methodology July 2, 2008 David Sun.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
CS100A, Fall 1998 Key Concepts 1 These notes contain short definitions of the basic entities that make up a Java program, along with a description of the.
1 Software Maintenance and Evolution CSSE 575: Session 3, Part 3 Dealing with Generalization Steve Chenoweth Office Phone: (812) Cell: (937)
Object Inheritance Lecturer: Kalamullah Ramli Electrical Engineering Dept. University of Indonesia Session-4.
C# Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the properties.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Software Construction and Evolution - CSSE 375 Simplifying Conditionals Shawn & Steve.
CS100A, Fall Lecture 5 1 CS100A, Fall 1997 Lecture, Tuesday, 16 September. This lecture continues the discussion of classes. The important new concept.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
Software Construction and Evolution - CSSE 375 Dealing with Generalization Steve and Shawn Left – In the 1990 movie “The Freshman,” Matthew Broderick,
Module 9. Dealing with Generalization Course: Refactoring.
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
2-Oct-16 Basic Object-Oriented Concepts. 2 Concept: An object has behaviors In old style programming, you had: data, which was completely passive functions,
Catalog of Refactoring
Object-Oriented Concepts
Catalog of Refactoring
Module Road Map Refactoring Why Refactoring? Examples
Lecture 12 Inheritance.
Object-oriented Programming in Java
Objects as a programming concept
Inheritance and Polymorphism
Lecture 15: More Inheritance
Refactoring Methods: Kevin Murphy.
CS2102: Lecture on Abstract Classes and Inheritance
Abstract Classes.
null, true, and false are also reserved.
Extending Classes.
Java Programming Language
Interfaces and Constructors
CSE 143 Lecture 27: Advanced List Implementation
Inheritance.
Unit 3 - The while Loop - Extending the Vic class - Examples
Software Design Lecture : 12.
OO Design with Inheritance
Recap Week 2 and 3.
Classes and Methods 15-Aug-19.
Presentation transcript:

Extract Subclass, Extract Superclass and Extract Hierarchy By: Ryan Meyer

Extract Subclass

When to Extract Subclass Class JobItem { … private int unitPrice; private Employee employee; private boolean laborItem; public JobItem(…){ } public void setEmployee(…,Employee e){ employee = e; public int getUnitPrice(){ return unitPrice; public employee getEmployee(){ return employee; public boolean isLaborItem(){ return laborItem; public int getTotalPrice(){ if(laborItem){ totalPrice = ….; else{ return totalPrice; When to Extract Subclass Class has methods/ variables which are used only in certain cases Hints: Boolean flags Some fields are left null/0 Frequent if statements in get methods Does your class describe more than one thing?

Class JobItem { … private int unitPrice; private Employee employee; private boolean laborItem; public JobItem(…){ } public int getUnitPrice(){ return unitPrice; public employee getEmployee(){ return employee; public boolean isLaborItem(){ return laborItem; public int getTotalPrice(){ if(laborItem){ totalPrice = ….; else{ return totalPrice; Extract the rare case into it’s own class and extend it to the original class: (methods, private variables, etc.) Class LaborItem extends JobItem { private Employee employee; public LaborItem(…, Employee e){ super(…); employee = e; } public boolean isLaborItem(){ return true; Public int getTotalPrice(){ totalPrice = …; return totalPrice;

Unnecessary conditional statements Class JobItem { … private int unitPrice; private Employee employee; private boolean laborItem; public JobItem(…){ } public void setEmployee(…,Employee e){ employee = e; public int getUnitPrice(){ return unitPrice; public employee getEmployee(){ return employee; public boolean isLaborItem(){ return laborItem; public int getTotalPrice(){ if(laborItem){ totalPrice = ….; else{ return totalPrice; Remove: Any extracted lines Unnecessary conditional statements Class JobItem { … private int unitPrice; public JobItem(…){ } public int getUnitPrice(){ return unitPrice; public boolean isLaborItem(){ return false; public int getTotalPrice(){ totalPrice = ….; return totalPrice;

Result: Class JobItem { … private int unitPrice; public JobItem(…){ } public int getUnitPrice(){ return unitPrice; public boolean isLaborItem(){ return false; public int getTotalPrice(){ totalPrice = ….; return totalPrice; Class LaborItem extends JobItem { private Employee employee; public LaborItem(…, Employee e){ super(…); employee = e; } public boolean isLaborItem(){ return true; Public int getTotalPrice(){ totalPrice = …; return totalPrice;

Extract Superclass

When to Extract Superclass Class Department{ Class Employee { … private string Name; private string ID; private int Cost; private int salary; private List Personel; public Employee(…){ public Department(…){ } public int getTotalAnnualCost(){ public int getAnnualCost(){ totalCost = …; return salary*12; return totalCost; public string getID(){ public string getName(){ return ID; return Name; public int getHeadCount(){ totalPrice = ….; return totalPrice; Classes have common fields/methods Hints: Similar named variables Used in similar situations

Create new class out of repeated code: (Similar methods with different implementations make abstract) Class Department{ Class Employee { … private string Name; private string ID; private int Cost; private int salary; private List Personel; public Employee(…){ public Department(…){ } public int getTotalAnnualCost(){ public int getAnnualCost(){ totalCost = …; return salary*12; return totalCost; public string getID(){ public string getName(){ return ID; return Name; public int getHeadCount(){ totalPrice = ….; return totalPrice; Class Party{ … private string Name; public Party(…){ } public abstract int getAnnualCost(); public string getName(){ return Name;

Remove the extracted lines and extend the subclasses: Class Department{ … private string Name; private int Cost; private List Personel; public Department(…){ } public int getTotalAnnualCost(){ totalCost = …; return totalCost; public string getName(){ return Name; public int getHeadCount(){ totalPrice = ….; return totalPrice; Class Party{ … private string Name; public Party(…){ } public abstract int getAnnualCost(); public string getName(){ return Name; Class Department extends Party{ … private int Cost; private List Personel; public Department(…){ super(…); } public int getAnnualCost(){ totalCost = …; return totalCost; public int getHeadCount(){ totalPrice = ….; return totalPrice;

Remove the extracted lines and extend the subclasses: Class Employee { private string Name; private string ID; private int salary; public Employee(…){ … } public int getAnnualCost(){ return salary*12; public string getID(){ return ID; public string getName(){ return Name; Class Party{ … private string Name; public Party(…){ } public abstract int getAnnualCost(); public string getName(){ return Name; Class Employee extends Party{ private string ID; private int salary; public Employee(…){ super(…); … } public int getAnnualCost(){ return salary*12; public string getID(){ return ID;

Result: Class Party{ … private string Name; public Party(…){ } Class Department extends Party{ … private int Cost; private List Personel; public Department(…){ super(…); } public int getAnnualCost(){ totalCost = …; return totalCost; public int getHeadCount(){ totalPrice = ….; return totalPrice; Class Party{ … private string Name; public Party(…){ } public abstract int getAnnualCost(); public string getName(){ return Name; Class Employee extends Party{ private string ID; private int salary; public Employee(…){ super(…); … } public int getAnnualCost(){ return salary*12; public string getID(){ return ID;

Extract Hierarchy

When to use it: A Class is trying to do too much work Hints: Class Billingscheme{ Something thing; … public Something getSomething(){ if(…){ thing = …; } else if(…){ return thing; public int billBuisness(…){ return …; public int billResidence(…){ public int billDisabled(…){ A Class is trying to do too much work Hints: Lots of if else statements Helper methods for a variety of scenarios Multiple problems from both the Extract Subclass and Extract Superclass… What one thing does your class describe?

How to Extract Hierarchy Identify each variation Create a subclass around that variation May use a variety of refactoring: Extract Subclass Extract Superclass Extract Method Pull Down Push Down Etc. Class BillingScheme{ … public abstract Something getSomething(); } Class BuisnessBillingScheme extends Billing Scheme { Class ResidentBillingScheme extends Billing Scheme { Class DisabilityBillingScheme extends Billing Scheme {