SlideShare una empresa de Scribd logo
1 de 4
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;
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;
/**
 *
 * @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
1.000 BIRDS    1.000 BIRDS BY FACTORY
      1         0.0130   0.0120
      2         0.0100   0.0120
      3         0.0120   0.0170
      4         0.0090   0.0120
      5         0.0100   0.0120
      6         0.0100   0.0080
      7         0.0090   0.0090
      8         0.0100   0.0100
      9         0.0100   0.0200
     10         0.0090   0.0120 REASON
AVERAGE        0.0102    0.0124 1.215686 Birds better

          10.000
          BIRDS          10.000 BIRDS BY FACTORY
      1         0.0190   0.0110
      2         0.0200   0.0100
      3         0.0200   0.0100
      4         0.0210   0.0100
      5         0.0320   0.0130
      6         0.0500   0.0110
      7         0.0200   0.0100
      8         0.0200   0.0130
      9         0.0190   0.0100
     10         0.0230   0.0110 REASON
AVERAGE        0.0244    0.0109 2.238532 Birds Factory better

          100.000
          BIRDS          100.000 BIRDS BY FACTORY
      1         0.0860   0.0450
      2         0.0830   0.0730
      3         0.0870   0.0430
      4         0.0970   0.0490
      5         0.1040   0.0470
      6         0.1020   0.0510
      7         0.1480   0.0570
      8         0.0870   0.0750
      9         0.0850   0.0760
     10         0.1500   0.0430 REASON
AVERAGE        0.1029    0.0559 1.840787 Birds Factory better

Más contenido relacionado

Similar a Hw12 refactoring to factory method

Transaction is a monad
Transaction is a  monadTransaction is a  monad
Transaction is a monadJarek Ratajski
 
Abstract Base Class (C++ Program)Create an abstract base class cal.pdf
Abstract Base Class (C++ Program)Create an abstract base class cal.pdfAbstract Base Class (C++ Program)Create an abstract base class cal.pdf
Abstract Base Class (C++ Program)Create an abstract base class cal.pdfcalderoncasto9163
 
Improving the java type system
Improving the java type systemImproving the java type system
Improving the java type systemJoão Loff
 
The purpose of this C++ programming project is to allow the student .pdf
The purpose of this C++ programming project is to allow the student .pdfThe purpose of this C++ programming project is to allow the student .pdf
The purpose of this C++ programming project is to allow the student .pdfRahul04August
 
The uniform interface is 42
The uniform interface is 42The uniform interface is 42
The uniform interface is 42Yevhen Bobrov
 
Assignment7.pdf
Assignment7.pdfAssignment7.pdf
Assignment7.pdfdash41
 
How to test complex SaaS applications - The family july 2014
How to test complex SaaS applications - The family july 2014How to test complex SaaS applications - The family july 2014
How to test complex SaaS applications - The family july 2014Guillaume POTIER
 
Deep dumpster diving 2010
Deep dumpster diving 2010Deep dumpster diving 2010
Deep dumpster diving 2010RonnBlack
 
The Ring programming language version 1.6 book - Part 55 of 189
The Ring programming language version 1.6 book - Part 55 of 189The Ring programming language version 1.6 book - Part 55 of 189
The Ring programming language version 1.6 book - Part 55 of 189Mahmoud Samir Fayed
 
JUnit Kung Fu: Getting More Out of Your Unit Tests
JUnit Kung Fu: Getting More Out of Your Unit TestsJUnit Kung Fu: Getting More Out of Your Unit Tests
JUnit Kung Fu: Getting More Out of Your Unit TestsJohn Ferguson Smart Limited
 
The Ring programming language version 1.5.2 book - Part 52 of 181
The Ring programming language version 1.5.2 book - Part 52 of 181The Ring programming language version 1.5.2 book - Part 52 of 181
The Ring programming language version 1.5.2 book - Part 52 of 181Mahmoud Samir Fayed
 
C346_PA3_W12srccommonBaseThread.javaC346_PA3_W12srccommonB.docx
C346_PA3_W12srccommonBaseThread.javaC346_PA3_W12srccommonB.docxC346_PA3_W12srccommonBaseThread.javaC346_PA3_W12srccommonB.docx
C346_PA3_W12srccommonBaseThread.javaC346_PA3_W12srccommonB.docxhumphrieskalyn
 
Rich and Snappy Apps (No Scaling Required)
Rich and Snappy Apps (No Scaling Required)Rich and Snappy Apps (No Scaling Required)
Rich and Snappy Apps (No Scaling Required)Thomas Fuchs
 
The Ring programming language version 1.9 book - Part 53 of 210
The Ring programming language version 1.9 book - Part 53 of 210The Ring programming language version 1.9 book - Part 53 of 210
The Ring programming language version 1.9 book - Part 53 of 210Mahmoud Samir Fayed
 
The Ring programming language version 1.7 book - Part 16 of 196
The Ring programming language version 1.7 book - Part 16 of 196The Ring programming language version 1.7 book - Part 16 of 196
The Ring programming language version 1.7 book - Part 16 of 196Mahmoud Samir Fayed
 
An Introduction to Property Based Testing
An Introduction to Property Based TestingAn Introduction to Property Based Testing
An Introduction to Property Based TestingC4Media
 
Un monde où 1 ms vaut 100 M€ - Devoxx France 2015
Un monde où 1 ms vaut 100 M€ - Devoxx France 2015Un monde où 1 ms vaut 100 M€ - Devoxx France 2015
Un monde où 1 ms vaut 100 M€ - Devoxx France 2015ThierryAbalea
 

Similar a Hw12 refactoring to factory method (20)

Transaction is a monad
Transaction is a  monadTransaction is a  monad
Transaction is a monad
 
Abstract Base Class (C++ Program)Create an abstract base class cal.pdf
Abstract Base Class (C++ Program)Create an abstract base class cal.pdfAbstract Base Class (C++ Program)Create an abstract base class cal.pdf
Abstract Base Class (C++ Program)Create an abstract base class cal.pdf
 
Improving the java type system
Improving the java type systemImproving the java type system
Improving the java type system
 
The purpose of this C++ programming project is to allow the student .pdf
The purpose of this C++ programming project is to allow the student .pdfThe purpose of this C++ programming project is to allow the student .pdf
The purpose of this C++ programming project is to allow the student .pdf
 
The uniform interface is 42
The uniform interface is 42The uniform interface is 42
The uniform interface is 42
 
Assignment7.pdf
Assignment7.pdfAssignment7.pdf
Assignment7.pdf
 
JUnit Pioneer
JUnit PioneerJUnit Pioneer
JUnit Pioneer
 
How to test complex SaaS applications - The family july 2014
How to test complex SaaS applications - The family july 2014How to test complex SaaS applications - The family july 2014
How to test complex SaaS applications - The family july 2014
 
Deep dumpster diving 2010
Deep dumpster diving 2010Deep dumpster diving 2010
Deep dumpster diving 2010
 
The Ring programming language version 1.6 book - Part 55 of 189
The Ring programming language version 1.6 book - Part 55 of 189The Ring programming language version 1.6 book - Part 55 of 189
The Ring programming language version 1.6 book - Part 55 of 189
 
JUnit Kung Fu: Getting More Out of Your Unit Tests
JUnit Kung Fu: Getting More Out of Your Unit TestsJUnit Kung Fu: Getting More Out of Your Unit Tests
JUnit Kung Fu: Getting More Out of Your Unit Tests
 
The Ring programming language version 1.5.2 book - Part 52 of 181
The Ring programming language version 1.5.2 book - Part 52 of 181The Ring programming language version 1.5.2 book - Part 52 of 181
The Ring programming language version 1.5.2 book - Part 52 of 181
 
C346_PA3_W12srccommonBaseThread.javaC346_PA3_W12srccommonB.docx
C346_PA3_W12srccommonBaseThread.javaC346_PA3_W12srccommonB.docxC346_PA3_W12srccommonBaseThread.javaC346_PA3_W12srccommonB.docx
C346_PA3_W12srccommonBaseThread.javaC346_PA3_W12srccommonB.docx
 
Rich and Snappy Apps (No Scaling Required)
Rich and Snappy Apps (No Scaling Required)Rich and Snappy Apps (No Scaling Required)
Rich and Snappy Apps (No Scaling Required)
 
The Ring programming language version 1.9 book - Part 53 of 210
The Ring programming language version 1.9 book - Part 53 of 210The Ring programming language version 1.9 book - Part 53 of 210
The Ring programming language version 1.9 book - Part 53 of 210
 
The Ring programming language version 1.7 book - Part 16 of 196
The Ring programming language version 1.7 book - Part 16 of 196The Ring programming language version 1.7 book - Part 16 of 196
The Ring programming language version 1.7 book - Part 16 of 196
 
An Introduction to Property Based Testing
An Introduction to Property Based TestingAn Introduction to Property Based Testing
An Introduction to Property Based Testing
 
Leveraging Symfony2 Forms
Leveraging Symfony2 FormsLeveraging Symfony2 Forms
Leveraging Symfony2 Forms
 
Un monde où 1 ms vaut 100 M€ - Devoxx France 2015
Un monde où 1 ms vaut 100 M€ - Devoxx France 2015Un monde où 1 ms vaut 100 M€ - Devoxx France 2015
Un monde où 1 ms vaut 100 M€ - Devoxx France 2015
 
Good code
Good codeGood code
Good code
 

Más de Edison Lascano

eXtreme Programming May Be Embedded inside Scrum
eXtreme Programming May Be Embedded inside ScrumeXtreme Programming May Be Embedded inside Scrum
eXtreme Programming May Be Embedded inside ScrumEdison Lascano
 
An Infectious Disease Surveillance Simulation (IDSS) in the Cloud
An Infectious Disease Surveillance Simulation (IDSS) in the CloudAn Infectious Disease Surveillance Simulation (IDSS) in the Cloud
An Infectious Disease Surveillance Simulation (IDSS) in the CloudEdison Lascano
 
UML for Aspect Oriented Design
UML for Aspect Oriented DesignUML for Aspect Oriented Design
UML for Aspect Oriented DesignEdison Lascano
 
Hw8 GoF state, strategy, tempate method, visitor
Hw8 GoF state, strategy, tempate method, visitorHw8 GoF state, strategy, tempate method, visitor
Hw8 GoF state, strategy, tempate method, visitorEdison Lascano
 
Hw7 mediator memento observer
Hw7 mediator memento observerHw7 mediator memento observer
Hw7 mediator memento observerEdison Lascano
 
Hw6 interpreter iterator GoF
Hw6 interpreter iterator GoFHw6 interpreter iterator GoF
Hw6 interpreter iterator GoFEdison Lascano
 
Hw5 proxy, chain of responsability, command
Hw5 proxy, chain of responsability, commandHw5 proxy, chain of responsability, command
Hw5 proxy, chain of responsability, commandEdison Lascano
 
Hw4 composite decorator facade flyweight
Hw4 composite decorator facade flyweightHw4 composite decorator facade flyweight
Hw4 composite decorator facade flyweightEdison Lascano
 
Hw11 refactoringcreation
Hw11 refactoringcreationHw11 refactoringcreation
Hw11 refactoringcreationEdison Lascano
 
GoF Patterns: Prototype, Singleton, Adapter, Bridge
GoF Patterns: Prototype, Singleton, Adapter, BridgeGoF Patterns: Prototype, Singleton, Adapter, Bridge
GoF Patterns: Prototype, Singleton, Adapter, BridgeEdison Lascano
 
Abstract Factory and Builder patterns
Abstract Factory and Builder patternsAbstract Factory and Builder patterns
Abstract Factory and Builder patternsEdison Lascano
 
GoF design patterns chapters 1 and 2
GoF design patterns chapters 1 and 2GoF design patterns chapters 1 and 2
GoF design patterns chapters 1 and 2Edison Lascano
 

Más de Edison Lascano (13)

eXtreme Programming May Be Embedded inside Scrum
eXtreme Programming May Be Embedded inside ScrumeXtreme Programming May Be Embedded inside Scrum
eXtreme Programming May Be Embedded inside Scrum
 
An Infectious Disease Surveillance Simulation (IDSS) in the Cloud
An Infectious Disease Surveillance Simulation (IDSS) in the CloudAn Infectious Disease Surveillance Simulation (IDSS) in the Cloud
An Infectious Disease Surveillance Simulation (IDSS) in the Cloud
 
UML for Aspect Oriented Design
UML for Aspect Oriented DesignUML for Aspect Oriented Design
UML for Aspect Oriented Design
 
Soap and Rest
Soap and RestSoap and Rest
Soap and Rest
 
Hw8 GoF state, strategy, tempate method, visitor
Hw8 GoF state, strategy, tempate method, visitorHw8 GoF state, strategy, tempate method, visitor
Hw8 GoF state, strategy, tempate method, visitor
 
Hw7 mediator memento observer
Hw7 mediator memento observerHw7 mediator memento observer
Hw7 mediator memento observer
 
Hw6 interpreter iterator GoF
Hw6 interpreter iterator GoFHw6 interpreter iterator GoF
Hw6 interpreter iterator GoF
 
Hw5 proxy, chain of responsability, command
Hw5 proxy, chain of responsability, commandHw5 proxy, chain of responsability, command
Hw5 proxy, chain of responsability, command
 
Hw4 composite decorator facade flyweight
Hw4 composite decorator facade flyweightHw4 composite decorator facade flyweight
Hw4 composite decorator facade flyweight
 
Hw11 refactoringcreation
Hw11 refactoringcreationHw11 refactoringcreation
Hw11 refactoringcreation
 
GoF Patterns: Prototype, Singleton, Adapter, Bridge
GoF Patterns: Prototype, Singleton, Adapter, BridgeGoF Patterns: Prototype, Singleton, Adapter, Bridge
GoF Patterns: Prototype, Singleton, Adapter, Bridge
 
Abstract Factory and Builder patterns
Abstract Factory and Builder patternsAbstract Factory and Builder patterns
Abstract Factory and Builder patterns
 
GoF design patterns chapters 1 and 2
GoF design patterns chapters 1 and 2GoF design patterns chapters 1 and 2
GoF design patterns chapters 1 and 2
 

Último

Sector 104, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 104, Noida Call girls :8448380779 Model Escorts | 100% verifiedSector 104, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 104, Noida Call girls :8448380779 Model Escorts | 100% verifiedDelhi Call girls
 
8377087607, Door Step Call Girls In Kalkaji (Locanto) 24/7 Available
8377087607, Door Step Call Girls In Kalkaji (Locanto) 24/7 Available8377087607, Door Step Call Girls In Kalkaji (Locanto) 24/7 Available
8377087607, Door Step Call Girls In Kalkaji (Locanto) 24/7 Availabledollysharma2066
 
Just Call Vip call girls diu Escorts ☎️9352988975 Two shot with one girl (diu )
Just Call Vip call girls diu Escorts ☎️9352988975 Two shot with one girl (diu )Just Call Vip call girls diu Escorts ☎️9352988975 Two shot with one girl (diu )
Just Call Vip call girls diu Escorts ☎️9352988975 Two shot with one girl (diu )gajnagarg
 
➥🔝 7737669865 🔝▻ jhansi Call-girls in Women Seeking Men 🔝jhansi🔝 Escorts S...
➥🔝 7737669865 🔝▻ jhansi Call-girls in Women Seeking Men  🔝jhansi🔝   Escorts S...➥🔝 7737669865 🔝▻ jhansi Call-girls in Women Seeking Men  🔝jhansi🔝   Escorts S...
➥🔝 7737669865 🔝▻ jhansi Call-girls in Women Seeking Men 🔝jhansi🔝 Escorts S...amitlee9823
 
VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...
VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...
VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...SUHANI PANDEY
 
Anupama Kundoo Cost Effective detailed ppt with plans and elevations with det...
Anupama Kundoo Cost Effective detailed ppt with plans and elevations with det...Anupama Kundoo Cost Effective detailed ppt with plans and elevations with det...
Anupama Kundoo Cost Effective detailed ppt with plans and elevations with det...sriharipichandi
 
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)amitlee9823
 
UI:UX Design and Empowerment Strategies for Underprivileged Transgender Indiv...
UI:UX Design and Empowerment Strategies for Underprivileged Transgender Indiv...UI:UX Design and Empowerment Strategies for Underprivileged Transgender Indiv...
UI:UX Design and Empowerment Strategies for Underprivileged Transgender Indiv...RitikaRoy32
 
How to Build a Simple Shopify Website
How to Build a Simple Shopify WebsiteHow to Build a Simple Shopify Website
How to Build a Simple Shopify Websitemark11275
 
call girls in Dakshinpuri (DELHI) 🔝 >༒9953056974 🔝 genuine Escort Service 🔝✔️✔️
call girls in Dakshinpuri  (DELHI) 🔝 >༒9953056974 🔝 genuine Escort Service 🔝✔️✔️call girls in Dakshinpuri  (DELHI) 🔝 >༒9953056974 🔝 genuine Escort Service 🔝✔️✔️
call girls in Dakshinpuri (DELHI) 🔝 >༒9953056974 🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...Pooja Nehwal
 
Call Girls Jalgaon Just Call 8617370543Top Class Call Girl Service Available
Call Girls Jalgaon Just Call 8617370543Top Class Call Girl Service AvailableCall Girls Jalgaon Just Call 8617370543Top Class Call Girl Service Available
Call Girls Jalgaon Just Call 8617370543Top Class Call Girl Service AvailableNitya salvi
 
Just Call Vip call girls Etawah Escorts ☎️9352988975 Two shot with one girl (...
Just Call Vip call girls Etawah Escorts ☎️9352988975 Two shot with one girl (...Just Call Vip call girls Etawah Escorts ☎️9352988975 Two shot with one girl (...
Just Call Vip call girls Etawah Escorts ☎️9352988975 Two shot with one girl (...gajnagarg
 
Anamika Escorts Service Darbhanga ❣️ 7014168258 ❣️ High Cost Unlimited Hard ...
Anamika Escorts Service Darbhanga ❣️ 7014168258 ❣️ High Cost Unlimited Hard  ...Anamika Escorts Service Darbhanga ❣️ 7014168258 ❣️ High Cost Unlimited Hard  ...
Anamika Escorts Service Darbhanga ❣️ 7014168258 ❣️ High Cost Unlimited Hard ...nirzagarg
 
Call Girls Basavanagudi Just Call 👗 7737669865 👗 Top Class Call Girl Service ...
Call Girls Basavanagudi Just Call 👗 7737669865 👗 Top Class Call Girl Service ...Call Girls Basavanagudi Just Call 👗 7737669865 👗 Top Class Call Girl Service ...
Call Girls Basavanagudi Just Call 👗 7737669865 👗 Top Class Call Girl Service ...amitlee9823
 
➥🔝 7737669865 🔝▻ dharamshala Call-girls in Women Seeking Men 🔝dharamshala🔝 ...
➥🔝 7737669865 🔝▻ dharamshala Call-girls in Women Seeking Men  🔝dharamshala🔝  ...➥🔝 7737669865 🔝▻ dharamshala Call-girls in Women Seeking Men  🔝dharamshala🔝  ...
➥🔝 7737669865 🔝▻ dharamshala Call-girls in Women Seeking Men 🔝dharamshala🔝 ...amitlee9823
 
❤Personal Whatsapp Number 8617697112 Samba Call Girls 💦✅.
❤Personal Whatsapp Number 8617697112 Samba Call Girls 💦✅.❤Personal Whatsapp Number 8617697112 Samba Call Girls 💦✅.
❤Personal Whatsapp Number 8617697112 Samba Call Girls 💦✅.Nitya salvi
 
High Profile Escorts Nerul WhatsApp +91-9930687706, Best Service
High Profile Escorts Nerul WhatsApp +91-9930687706, Best ServiceHigh Profile Escorts Nerul WhatsApp +91-9930687706, Best Service
High Profile Escorts Nerul WhatsApp +91-9930687706, Best Servicemeghakumariji156
 
The hottest UI and UX Design Trends 2024
The hottest UI and UX Design Trends 2024The hottest UI and UX Design Trends 2024
The hottest UI and UX Design Trends 2024Ilham Brata
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 

Último (20)

Sector 104, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 104, Noida Call girls :8448380779 Model Escorts | 100% verifiedSector 104, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 104, Noida Call girls :8448380779 Model Escorts | 100% verified
 
8377087607, Door Step Call Girls In Kalkaji (Locanto) 24/7 Available
8377087607, Door Step Call Girls In Kalkaji (Locanto) 24/7 Available8377087607, Door Step Call Girls In Kalkaji (Locanto) 24/7 Available
8377087607, Door Step Call Girls In Kalkaji (Locanto) 24/7 Available
 
Just Call Vip call girls diu Escorts ☎️9352988975 Two shot with one girl (diu )
Just Call Vip call girls diu Escorts ☎️9352988975 Two shot with one girl (diu )Just Call Vip call girls diu Escorts ☎️9352988975 Two shot with one girl (diu )
Just Call Vip call girls diu Escorts ☎️9352988975 Two shot with one girl (diu )
 
➥🔝 7737669865 🔝▻ jhansi Call-girls in Women Seeking Men 🔝jhansi🔝 Escorts S...
➥🔝 7737669865 🔝▻ jhansi Call-girls in Women Seeking Men  🔝jhansi🔝   Escorts S...➥🔝 7737669865 🔝▻ jhansi Call-girls in Women Seeking Men  🔝jhansi🔝   Escorts S...
➥🔝 7737669865 🔝▻ jhansi Call-girls in Women Seeking Men 🔝jhansi🔝 Escorts S...
 
VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...
VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...
VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...
 
Anupama Kundoo Cost Effective detailed ppt with plans and elevations with det...
Anupama Kundoo Cost Effective detailed ppt with plans and elevations with det...Anupama Kundoo Cost Effective detailed ppt with plans and elevations with det...
Anupama Kundoo Cost Effective detailed ppt with plans and elevations with det...
 
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)
 
UI:UX Design and Empowerment Strategies for Underprivileged Transgender Indiv...
UI:UX Design and Empowerment Strategies for Underprivileged Transgender Indiv...UI:UX Design and Empowerment Strategies for Underprivileged Transgender Indiv...
UI:UX Design and Empowerment Strategies for Underprivileged Transgender Indiv...
 
How to Build a Simple Shopify Website
How to Build a Simple Shopify WebsiteHow to Build a Simple Shopify Website
How to Build a Simple Shopify Website
 
call girls in Dakshinpuri (DELHI) 🔝 >༒9953056974 🔝 genuine Escort Service 🔝✔️✔️
call girls in Dakshinpuri  (DELHI) 🔝 >༒9953056974 🔝 genuine Escort Service 🔝✔️✔️call girls in Dakshinpuri  (DELHI) 🔝 >༒9953056974 🔝 genuine Escort Service 🔝✔️✔️
call girls in Dakshinpuri (DELHI) 🔝 >༒9953056974 🔝 genuine Escort Service 🔝✔️✔️
 
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Gi...
 
Call Girls Jalgaon Just Call 8617370543Top Class Call Girl Service Available
Call Girls Jalgaon Just Call 8617370543Top Class Call Girl Service AvailableCall Girls Jalgaon Just Call 8617370543Top Class Call Girl Service Available
Call Girls Jalgaon Just Call 8617370543Top Class Call Girl Service Available
 
Just Call Vip call girls Etawah Escorts ☎️9352988975 Two shot with one girl (...
Just Call Vip call girls Etawah Escorts ☎️9352988975 Two shot with one girl (...Just Call Vip call girls Etawah Escorts ☎️9352988975 Two shot with one girl (...
Just Call Vip call girls Etawah Escorts ☎️9352988975 Two shot with one girl (...
 
Anamika Escorts Service Darbhanga ❣️ 7014168258 ❣️ High Cost Unlimited Hard ...
Anamika Escorts Service Darbhanga ❣️ 7014168258 ❣️ High Cost Unlimited Hard  ...Anamika Escorts Service Darbhanga ❣️ 7014168258 ❣️ High Cost Unlimited Hard  ...
Anamika Escorts Service Darbhanga ❣️ 7014168258 ❣️ High Cost Unlimited Hard ...
 
Call Girls Basavanagudi Just Call 👗 7737669865 👗 Top Class Call Girl Service ...
Call Girls Basavanagudi Just Call 👗 7737669865 👗 Top Class Call Girl Service ...Call Girls Basavanagudi Just Call 👗 7737669865 👗 Top Class Call Girl Service ...
Call Girls Basavanagudi Just Call 👗 7737669865 👗 Top Class Call Girl Service ...
 
➥🔝 7737669865 🔝▻ dharamshala Call-girls in Women Seeking Men 🔝dharamshala🔝 ...
➥🔝 7737669865 🔝▻ dharamshala Call-girls in Women Seeking Men  🔝dharamshala🔝  ...➥🔝 7737669865 🔝▻ dharamshala Call-girls in Women Seeking Men  🔝dharamshala🔝  ...
➥🔝 7737669865 🔝▻ dharamshala Call-girls in Women Seeking Men 🔝dharamshala🔝 ...
 
❤Personal Whatsapp Number 8617697112 Samba Call Girls 💦✅.
❤Personal Whatsapp Number 8617697112 Samba Call Girls 💦✅.❤Personal Whatsapp Number 8617697112 Samba Call Girls 💦✅.
❤Personal Whatsapp Number 8617697112 Samba Call Girls 💦✅.
 
High Profile Escorts Nerul WhatsApp +91-9930687706, Best Service
High Profile Escorts Nerul WhatsApp +91-9930687706, Best ServiceHigh Profile Escorts Nerul WhatsApp +91-9930687706, Best Service
High Profile Escorts Nerul WhatsApp +91-9930687706, Best Service
 
The hottest UI and UX Design Trends 2024
The hottest UI and UX Design Trends 2024The hottest UI and UX Design Trends 2024
The hottest UI and UX Design Trends 2024
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
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
  • 4. 1.000 BIRDS 1.000 BIRDS BY FACTORY 1 0.0130 0.0120 2 0.0100 0.0120 3 0.0120 0.0170 4 0.0090 0.0120 5 0.0100 0.0120 6 0.0100 0.0080 7 0.0090 0.0090 8 0.0100 0.0100 9 0.0100 0.0200 10 0.0090 0.0120 REASON AVERAGE 0.0102 0.0124 1.215686 Birds better 10.000 BIRDS 10.000 BIRDS BY FACTORY 1 0.0190 0.0110 2 0.0200 0.0100 3 0.0200 0.0100 4 0.0210 0.0100 5 0.0320 0.0130 6 0.0500 0.0110 7 0.0200 0.0100 8 0.0200 0.0130 9 0.0190 0.0100 10 0.0230 0.0110 REASON AVERAGE 0.0244 0.0109 2.238532 Birds Factory better 100.000 BIRDS 100.000 BIRDS BY FACTORY 1 0.0860 0.0450 2 0.0830 0.0730 3 0.0870 0.0430 4 0.0970 0.0490 5 0.1040 0.0470 6 0.1020 0.0510 7 0.1480 0.0570 8 0.0870 0.0750 9 0.0850 0.0760 10 0.1500 0.0430 REASON AVERAGE 0.1029 0.0559 1.840787 Birds Factory better