Your SlideShare is downloading. ×
Hw11 refactoringcreation
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Hw11 refactoringcreation

258
views

Published on

An alternative to creation methods

An alternative to creation methods


0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
258
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. UTAH STATE UNIVERSITY COMPUTER SCIENCE CS-7350 Creation Refactoring, based on “Refactoring to Patterns” Chapter 6 by Kerievsky, J. Jorge Edison Lascano Spring 2012 02-22-2012Solving the nonstandard liability.Instead of using creation methods I think that a better way torefactor many constructors(keeping a standard to instantiate) is passing an object as parameter toevery constructor. I made an experiment with a class Person and constructors to initializestudents, workers and professors; I am assuming no inheritance is needed. Themechanics(omitting compile and test) is 1) find a class with several constructors:Person, 2)implement a base constructor:Person(), 3) implement one class per constructor, every class withan initialization constructor and public attributes as in the original constructors:Student{},Worker{}, Professor{}, 4) review chained constructors, 5) find all callers and update them toinstantiate with the new constructors (see code).So far I am still using new after refactoring, andthe code looks clearer in the callers; but,with some over-engineering.See attached code for fulljava implementation.PersonaRefactorestRef=new PersonaRefactor(new Student("edi", "SLC",15, 2));PersonaRefactorwrkRef=new PersonaRefactor(new Worker("Genie", "Logan",15, 2,30));
  • 2. packagejavaperson;/** * * @author elascano */public class Main { /** * @paramargs the command line arguments */public static void main(String[] args) { // THIS CODE WITHOUT REFACTORING Persona per=new Persona("dani","logan",41);Persona est=new Persona("edi","logan",41,3);Persona wrk=new Persona("vicki","logan",41,3,40); Persona prf=new Persona("stephen","logan",41,4,20,5);System.out.println("----------------Persona--------------");per.print();System.out.println("----------------Student--------------");est.print();System.out.println("----------------Worker--------------");wrk.print();System.out.println("----------------Professor--------------");prf.print(); // THIS CODE WITH REFACTORINGPersonaRefactorperRef=new PersonaRefactor("george","quito",18);PersonaRefactorestRef=new PersonaRefactor(new Student("hao", "SLC", 15, 2));PersonaRefactorwrkRef=new PersonaRefactor(new Worker("Genie", "Logan", 15, 2,30));PersonaRefactorprfRef=new PersonaRefactor(new Professor("Edison", "North Logan", 15, 4, 20, 6)); //System.out.println("----------------Persona--------------"); //per.print();System.out.println("----------------Student--------------");estRef.print();System.out.println("----------------Worker--------------");wrkRef.print();System.out.println("----------------Professor--------------");prfRef.print(); }}packagejavaperson;/** * * @authorelascano */class Persona {private String name="";private String city="";privateint age=0;privateintstudyLevel=0;privateintweekWorkHours=0;privateinttaughtCourses=0; //construct a personpublic Persona(String n, String c, int a){name=n;city=c;age=a; } //contruct a student
  • 3. public Persona(String n, String c, int a, int l){ //new Persona(n, c, a);this(n,c,a);studyLevel=l; } //contruct a workerpublic Persona(String n, String c, int a, int l, int h){ //new Persona(n, c, a);this(n,c,a,l);weekWorkHours=h; } //contruct a professorpublic Persona(String n, String c, int a, int l, int h, int t){ //new Persona(n, c, a);this(n,c,a,l,h);taughtCourses=t; }public void print(){System.out.println("name : "+name);System.out.println("city : "+city);System.out.println("age : "+age);System.out.println("level of studies : "+studyLevel);System.out.println("weekly hours : "+weekWorkHours);System.out.println("taught courses : "+taughtCourses); }}packagejavaperson;/** * * @author elascano */public class PersonaRefactor {private String name="";private String city="";privateint age=0;privateintstudyLevel=0;privateintweekWorkHours=0;privateinttaughtCourses=0; //construct a personpublicPersonaRefactor(String n, String c, int a){name=n;city=c;age=a; } //contruct a studentpublicPersonaRefactor(Student S){ //new Persona(n, c, a);this(S.name, S.city, S.age);studyLevel=S.studyLevel; } //contruct a workerpublicPersonaRefactor(Worker W){ //new Persona(n, c, a);this(new Student(W.name, W.city, W.age, W.studyLevel));weekWorkHours=W.weekWorkHours; } //contruct a professorpublicPersonaRefactor(Professor P){ //new Persona(n, c, a);
  • 4. this(new Worker(P.name, P.city, P.age, P.studyLevel, P.weekWorkHours));taughtCourses=P.taughtCourses; }public void print(){System.out.println("name : "+name);System.out.println("city : "+city);System.out.println("age : "+age);System.out.println("level of studies : "+studyLevel);System.out.println("weekly hours : "+weekWorkHours);System.out.println("taught courses : "+taughtCourses); }}class Student{public String name="";public String city="";publicint age=0;publicintstudyLevel=0; //construct a personpublic Student(String n, String c, int a, int l){name=n;city=c;age=a;studyLevel=l; }}class Worker{public String name="";public String city="";publicint age=0;publicintstudyLevel=0;publicintweekWorkHours=0; //construct a personpublic Worker(String n, String c, int a, int l, int h){name=n;city=c;age=a;studyLevel=l;weekWorkHours=h; }}class Professor{public String name="";public String city="";publicint age=0;publicintstudyLevel=0;publicintweekWorkHours=0;publicinttaughtCourses=0; //construct a personpublic Professor(String n, String c, int a, int l, int h, int t){name=n;city=c;age=a;studyLevel=l;weekWorkHours=h;taughtCourses=t; }}

×