Replace Constructor with Factory Method <IMG SRC = "http://www.refactoring.com/catalog/updated.gif" border=0>
2003. 8. 2. 15:02
Replace Constructor with Factory Method
Replace the constructor with a factory method.
Employee (int type) {
_type = type;
}
![](http://www.refactoring.com/catalog/arrow.gif)
static Employee create(int type) {
return new Employee(type);
}
For more information see page 304 of Refactoring
Additional Comments
Dimitri Paltchoun pointed out that as well as using Class.forName() and a string for a client to specify the created class, you can also use the class object itself. This would lead you to method like
static Employee create(Class c){
try{
return (Employee)c.newInstance();
}catch(Exception e){
throw new IllegalException("Unable to instantiate" +c);
}
}
This would be called from this code
Employee.create(Engineer.class);
***** 아름다운프로님에 의해서 게시물 복사 + 카테고리변경되었습니다 (2003-12-18 17:27)