![]() |
< Day Day Up > |
![]() |
Recipe 3.15 Converting Constructors to Factory Methods3.15.1 ProblemYou want to convert a constructor to a factory method. 3.15.2 SolutionIn Eclipse 3.0, select a constructor declaration or a call to the
constructor in the JDT editor, and then select Refactoring
3.15.3 DiscussionEclipse 3.0 enables you to convert constructors into factory methods.
To do that, you select a constructor declaration or a call to the
constructor and then select Refactoring For example, say you had this call to a class's constructor: public class DisplayApp {
private String text;
public static void main(String[] args) {
DisplayApp DisplayApp = new DisplayApp("Hello");
}
public DisplayApp(String text) {
super( );
this.text = text;
}
} Selecting the constructor call and then selecting
Refactoring public class DisplayApp { private String text; public static void main(String[] args) { DisplayApp DisplayApp = createDisplayApp("Hello"); } public static DisplayApp createDisplayApp(java.lang.String text) { return new DisplayApp(text); } /** * @param text */ private DisplayApp(String text) { super( ); this.text = text; } } |
![]() |
< Day Day Up > |
![]() |