Refactoring Methods: Kevin Murphy.

Slides:



Advertisements
Similar presentations
Inheritance // A simple class hierarchy. // A class for two-dimensional objects. class TwoDShape { double width; double height; void showDim() { System.out.println("Width.
Advertisements

Software Engineering Implementation Lecture 3 ASPI8-4 Anders P. Ravn, Feb 2004.
CSCI 160 Midterm Review Rasanjalee DM.
Inheritance Inheritance Reserved word protected Reserved word super
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
REFACTORING Improving the Design of Existing Code Atakan Şimşek e
Unit 031 Interfaces What is an Interface? Interface Declaration Syntax Implementing Interfaces Using Interfaces as Types Interfaces and Inheritance Interfaces.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
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.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
Intro to OOP with Java, C. Thomas Wu
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
CSC 212 – Data Structures Lecture 12: Java Review.
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.
2000 Jordan Anastasiade. All rights reserved. 1 Class In this lesson you will be learning about: Class. Inheritance. Polymorphism. Nested and.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
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.
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.
Static Attributes and Inheritance  static attributes behave the same as non-static attributes in inheritance  public and protected static attributes.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
Objects and Classes Mostafa Abdallah
Lecture 06 Java and OOP Jaeki Song. Outlines Java and OOP –Class and Object – Inheritance – Polymorphism.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
Software Construction and Evolution - CSSE 375 Making Method Calls Simpler Shawn and Steve Below – “Be the character!” The late acting teacher Lee Strasberg.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
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.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Topics Inheritance introduction
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inherited Classes in Java CSCI 392 Ch 6 in O’Reilly Adapted from Dannelly.
1 Inheritance and Subclasses. 2 Inheritance Often we create very similar classes –Different types of triangles: equilateral, isosceles, etc. –Different.
CSSE 375 Organizing Data – Part 2 Shawn and Steve Continue the same quiz!
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
// Java2101.java This program tests the features of the class. public class Java2101 { public static void main (String args[]) { System.out.println("\nJAVA2101.JAVA\n");
CSSE 375 Organizing Data – Part 1 Shawn and Steve Q1.
REFACTORING CHANGE VALUE TO REFERENCE SUBSTITUTE ALGORITHM REPLACE CONDITIONAL WITH POLYMORHPISM.
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
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,
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.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Catalog of Refactoring (3) Organizing Data. Catalog Self Encapsulate Field Replace Data Value with Object Change Value to Reference Change Reference to.
Catalog of Refactoring (6) Making Method Calls Simpler.
Catalog of Refactoring
Module Road Map Refactoring Why Refactoring? Examples
Inheritance and Polymorphism
University of Central Florida COP 3330 Object Oriented Programming
Week 4 Object-Oriented Programming (1): Inheritance
Extract Subclass, Extract Superclass and Extract Hierarchy
Initializing Arrays char [] cArray3 = {'a', 'b', 'c'};
null, true, and false are also reserved.
MSIS 670 Object-Oriented Software Engineering
Inherited Classes in Java
OO Design with Inheritance
Recap Week 2 and 3.
Simple Classes in Java CSCI 392 Classes – Part 1.
JAVA CLASSES.
An Example of Inheritance
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
CIS 110: Introduction to computer programming
Chapter 5 Classes.
Presentation transcript:

Refactoring Methods: Kevin Murphy

Situation 1: class Employee {   private int _type;   static final int ENGINEER = 0;   static final int SALESMAN = 1;   static final int MANAGER = 2;   Employee (int type) {      _type = type;   }

Replace Constructor with Factory Method Create a factory method. Make its body a call to the current constructor. Replace all calls to the constructor with calls to the factory method. Declare the constructor private.

Replace Constructor with Factory Method static Employee create(int type) {      return new Employee(type); }  Employee eng = Employee.create(Employee.ENGINEER); class Employee...   private Employee (int type) {       _type = type;   }

Benefits Easier to use and follow in a program Can build multiple different subclasses out of the same method

Situation 2: class Order...   Customer getCustomer() {       return _customer;   }   void setCustomer (Customer arg) {       if (_customer != null) _customer.friendOrders().remove(this);       _customer = arg;       if (_customer != null) _customer.friendOrders().add(this);   }   private Customer _customer; class Customer...   void addOrder(Order arg) {       arg.setCustomer(this);   }   private Set _orders = new HashSet();   Set friendOrders() {       /** should only be used by Order */       return _orders;   }

Replace Subclass with Fields Use Replace Constructor with Factory Method (304) on the subclasses. Declare final fields for each constant method on the superclass. Declare a protected superclass constructor to initialize the fields. Add or modify subclass constructors to call the new superclass constructor. Implement each constant method in the superclass to return the field and remove the method from the subclasses.

Replace Subclass with Fields class Person... static Person createMale(){ return new Male(); } static Person createFemale() { return new Female(); } class Person... private final boolean _isMale; private fi class Person... protected Person (boolean isMale, char code) { _isMale = isMale; _code = code;nal char _code; class Male... Male() { super (true, 'M'); } class Female... Female() { super (false, 'F'); }

Benefits Numerous classes can be reduced into one class Subclasses that only have constant functions are superfluous

Situation 3: class Order...   Customer getCustomer() {       return _customer;   }   void setCustomer (Customer arg) {       if (_customer != null) _customer.friendOrders().remove(this);       _customer = arg;       if (_customer != null) _customer.friendOrders().add(this);   }   private Customer _customer; class Customer...   void addOrder(Order arg) {       arg.setCustomer(this);   }   private Set _orders = new HashSet();   Set friendOrders() {       /** should only be used by Order */       return _orders;   }

Bidirectional Association to Unidirectional Determine if the removal of the field in class 2 pointing at class 1 is possible Replace any calls to class 2 with calls to the field in class 1

Bidirectional Association to Unidirectional class Order... double getDiscountedPrice(Customer customer) { return getGrossPrice() * (1 - customer.getDiscount()); } class Customer... double getPriceFor(Order order) { Assert.isTrue(_orders.contains(order)); return order.getDiscountedPrice(this); }

Benefits Removes unnecessary connections Can save large amounts of data

Questions?