Unformatted text preview:

Today’s AgendaIntroduce Null Object (1)Introduce Null Object (2)Introduce Null Object (3)Replace Data Value with Object (1)Replace Data Value with Object (2)Change Value to ReferenceReplace Magic Number with Sym. ConstantSeparate Query from ModifierParameterize MethodReplace Parameter with Explicit MethodsReplace Parameter with Method (1)Replace Parameter with Method (2)Replace Constructor with Factory Method (1)Replace Constructor with Factory Method (2)Encapsulate DowncastReplace Error Code with Exception (1)Replace Error Code with Exception (2)Replace Error Code with Exception (3)Pull Up Constructor BodyExtract Subclass (1)Extract Subclass (2)Extract Subclass (3)Form Template Method (1)Form Template Method (2)Form Template Method (3)Form Template Method (4)Form Template Method (5)Replace Inheritance with Delegation (1)Replace Inheritance with Delegation (2)Today’s AgendaMore refactoring patternsSoftware Testing and Maintenance 1Introduce Null Object (1)class Site … Customer getCustomer () { return _customer; }class Customer … public String getName () { … } public BillingPlan getPlan () { … } public PaymentHistory getHistory () { … }Customer customer = site.getCustomer ();BillingPlan plan;if (customer == null) plan = BillingPlan.basic ();else plan = customer.getPlan ();…String customerName;if (customer == null) customerName = “occupant”;else customerName = customer.getName (); …Software Testing and Maintenance 2Introduce Null Object (2)class NullCustomer extends Customer { public boolean isNull () { return true; } public String getName () { return “occupant”; } public BillingPlan getPlan () { return BillingPlan.basic (); }class Customer … static Customer newNull () { return new NullCustomer (); }class Site … Customer getCustomer () { return (_customer == null) ? Customer.newNull () : _customer; }Software Testing and Maintenance 3Introduce Null Object (3)Customer customer = site.getCustomer ();BillingPlan plan;plan = customer.getPlan ();…String customerName;customerName = customer.getName (); …Software Testing and Maintenance 4Replace Data Value with Object (1)class Order public Order (String customer) { _customer = customer; } public String getCustomer () { return _customer; } public void setCustomer (String customer) { _customer = customer; } private String _customer;Software Testing and Maintenance 5What if we need to add address or credit rating into a customer?Replace Data Value with Object (2)class Customer { public Customer (String name) { _name = name; } public String getName () { return _name; } private final String _name;}class Order … public order (String customer) { _customer = new Customer (customer); } public String getCustomer () { return _customer.getName (); } private Customer _customer; public void setCustomer (String customer) { _customer = new Customer (customer); }Software Testing and Maintenance 6Change Value to Referenceclass Order { public order (String customer) { _customer = Customer.create (customer); }}class Customer { private Customer (String name) { _name = name; } static void loadCustomer () { new Customer (“Bilston Gasworks”).store(); new Customer (“Jeff Bridge”).store(); … } private void store () { _instances.put(this.getName(), this); } public static Customer create (String name) { return (Customer) _instances.get(name); } private static Dictionary _instances = new Hashtable ();}Software Testing and Maintenance 7Replace Magic Number with Sym. Constantdouble potentialEnergy (double mass, double height) { return mass * 9.81 * height;}Software Testing and Maintenance 8double potentialEnergy (double mass, double height) { return mass * GRAVITATIONAL_CONSTANT * height;}Static final double GRAVITATIONAL_CONSTANT = 9.81;Separate Query from Modifierint getTotalOutstandingAndSetReadyForSummaries () { readyForSummaries = true; return outstanding; }Software Testing and Maintenance 9int getTotalOutstanding () { return outstanding; }void setReadyForSummaries () { readyForSummaries = true;}Parameterize Methodclass Employee { void tenPercentRaise () { salary *= 1.1; } void fivePercentRaise () { salary *= 1.05; }}Software Testing and Maintenance 10class Employee { void raise (double factor) { salary *= (1 + factor); }}Replace Parameter with Explicit Methodsvoid setValue (String name, int value) { if (name.equals(“height”)) { _height = value; return; } if (name.equals(“width”)) { _width = value; return; }}Software Testing and Maintenance 11void setHeight (int arg) { _height = arg;}Void setWidth (int arg) { _width = arg;}Replace Parameter with Method (1)public double getPrice () { int basePrice = _quantity * _itemPrice; int discountLevel; if (_quantity > 100) discountLevel = 2; else discountLevel = 1; double finalPrice = discountedPrice (basePrice, discountLevel); return finalPrice;}private double discountedPrice (int basePrice, int discountLevel) { if (discountLevel == 2) return basePrice * 0.1; else return basePrice * 0.05;}Software Testing and Maintenance 12Replace Parameter with Method (2)public double getPrice () { if (getDiscountLevel() == 2) return getBasePrice() * 0.1; else return getBasePrice() * 0.05;}private double getBasePrice () { return _quantity * _itemPrice;}private int getDiscountLevel () { if(_quantity > 100) return 2; else return 1;}Software Testing and Maintenance 13Replace Constructor with Factory Method (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; }}Software Testing and Maintenance 14class Employee { static Employee create (int type) { switch (type) { case ENGINEER: return new Engineer (); case SALESMAN: return new Salesman (); case MANAGER: return new Manager (); default: // throw an exception }}Replace Constructor with Factory Method (2)Software Testing and Maintenance 15static Employee create (String name) { try { return (Employee) Class.forName (name).newInstance(); } catch (Exception ex) { throw


View Full Document

UT Arlington CSE 4321 - Refactoring patterns

Download Refactoring patterns
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Refactoring patterns and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Refactoring patterns 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?