1) The document discusses refactoring code to use a factory method for creating objects by encapsulating classes. It compares the performance of directly instantiating objects versus using a factory method.
2) An example is provided of refactoring code for creating Bird objects by moving the creation logic to a static factory method in the Animal class.
3) Performance tests were run instantiating 1,000, 10,000 and 100,000 Bird objects both ways. For smaller numbers, direct instantiation was faster, but for larger numbers the factory method had better performance.
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
Hw12 refactoring to factory method
1. UTAH STATE UNIVERSITY
COMPUTER SCIENCE
CS-7350
Encapsulate Classes with Factory, based on “Refactoring to Patterns” Chapter 6 by Kerievsky, J.
Jorge Edison Lascano
Spring 2012
02-24-2012
Encapsulate classes with factory, a brief performance analysis.According to Kerievsky, this
refactoring has benefits from the programmer point of view: “simplify the creation of kinds of
instances”, “reduces conceptual weight”, and “enforce the perform to an interface, not to an
implementation from GoF”; nevertheless, a performance in creating objects is not discussed. For
that reason and following his mechanics I created an example to create Bird(s), a subclass of
Animal: 1) I implemented public static createBird(…) creation method, then I moved it to
Animal, 2) I called it from main as AnimalRefactor.createBird(…), 3) I declared the
constructor protected (see code).To test performance, I instantiated 1.000, 10.000 and 100.000
objects to take 10 measure values and then I simply averaged them.Here the results in
milliseconds for Bird and BirdFactory, and Reason: 1.000 >0.0102, 0.0124, 6/5 ->Bird is
faster; 10.000 >0.0244, 0.0109, 2.2/1 Bird Factory is faster; 100.000 > 0.1029, 0.0559, 1.8/1
Bird Factory is faster. These measures cannot be considered conclusive, but just a
demonstration that a Factory is faster in this example under Netbeans7.1+ java1.7.0_01
CODE AND MEASURES
ORIGINAL CODE
package javaanimalfm;
/**
*
* @author elascano
*/
public class Animal {
private String name="";
private int birthDay=0;
private int birthMonth=0;
private int bithYear=0;
private int code=0;
public Animal(String n, int d, int m, int y, int c){
name=n;
birthDay=d;
birthMonth=m;
bithYear=y;
code=c;
}
}
class Bird extends Animal{
boolean fly=false;
int hatchFrequency=0;
int hatchTemperature=0;
boolean commercialEggs=false;
2. int furculaSize=0;
public Bird(String n, int d, int m, int y, int c, boolean f, int h, int t, boolean e, int s){
super(n,d,m,y,c);
fly=f;
hatchFrequency=h;
hatchTemperature=t;
commercialEggs=e;
furculaSize=s;
}
}
CODE REFACTORED TO FACTORY METHOD
package javaanimalfm;
/**
*
* @author elascano
*/
public class AnimalRefactor {
private String name="";
private int birthDay=0;
private int birthMonth=0;
private int bithYear=0;
private int code=0;
public AnimalRefactor(String n, int d, int m, int y, int c){
name=n;
birthDay=d;
birthMonth=m;
bithYear=y;
code=c;
}
protected AnimalRefactor(){
name="";
birthDay=0;
birthMonth=0;
bithYear=0;
code=0;
}
public static AnimalRefactor createBird(String n, int d, int m, int y, int c, boolean f, int h, int t,
boolean e, int s){
return new BirdRefactor(n,d,m,y,c,f,h,t,e,s);
}
}
class BirdRefactor extends AnimalRefactor{
boolean fly=false;
int hatchFrequency=0;
int hatchTemperature=0;
boolean commercialEggs=false;
int furculaSize=0;
protected BirdRefactor(String n, int d, int m, int y, int c, boolean f, int h, int t, boolean e, int s){
super(n,d,m,y,c);
fly=f;
hatchFrequency=h;
hatchTemperature=t;
commercialEggs=e;
furculaSize=s;
}
}
MAIN CLASS THAT CALLS THE ORIGINAL AND THE REFACTORED CODE
package javaanimalfm;
import java.util.ArrayList;
import java.util.List;
3. /**
*
* @author elascano
*/
public class JavaAnimalFMMain {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int count = 1000000;
long startTime;
long stopTime;
//LIST OF ANIMALS
List<Animal> list_of_animals = new ArrayList<>();
startTime = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
list_of_animals.add(new Animal("dog" + i, 5, 5, 2012, i));
}
stopTime = System.currentTimeMillis();
System.out.println("Avg Time by creating " + count + " " + Animal.class.getSimpleName() + "(s) = "
+ ((stopTime - startTime) / 1000f)
+ " msecs");
//LIST OF BIRDS CREATED DIRECTLY
List<Bird> list_of_birds = new ArrayList<>();
startTime = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
list_of_birds.add(new Bird("dog" + i, 5, 5, 2012, i, true, 5, 5, true, 5));
}
stopTime = System.currentTimeMillis();
System.out.println("Avg Time by creating " + count + " " + Bird.class.getSimpleName() + "(s) = "
+ ((stopTime - startTime) / 1000f)
+ " msecs");
//LIST OF BIRDSREFACTOR CREATED DIRECTLY
List<BirdRefactor> list_of_birdsR1 = new ArrayList<>();
startTime = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
list_of_birdsR1.add(new BirdRefactor("dog" + i, 5, 5, 2012, i, true, 5, 5, true, 5));
}
stopTime = System.currentTimeMillis();
System.out.println("Avg Time by creating" + count + " " + BirdRefactor.class.getSimpleName() + "(s)
REFACTOR BY ITSELF = "
+ ((stopTime - startTime) / 1000f)
+ " msecs");
//LIST OF BIRDSREFACTOR THROUGH INTERFACE FACTORY METHOD
List<BirdRefactor> list_of_birdsR3 = new ArrayList<>();
startTime = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
list_of_birdsR3.add((BirdRefactor) AnimalRefactor.createBird("dog" + i, 5, 5, 2012, i, true, 5, 5,
true, 5));
}
stopTime = System.currentTimeMillis();
System.out.println("Avg Time by creating" + count + " " + BirdRefactor.class.getSimpleName() + "(s)
THROUGH INTERFACE = "
+ ((stopTime - startTime) / 1000f)
+ " msecs");
}
}
MEASURES