작성
·
129
0
public interface ABC {
void hello();
}
public class A implements ABC {
@Autowired
private Hello hello;
void hello() {
// codes..
}
}
public class B implements ABC {
@Autowired
private Hello hello;
void hello() {
// codes..
}
}
public class C implements ABC {
@Autowired
private Hello hello;
void hello() {
// codes..
}
}
public class ABCFactory implements FactoryBean<ABC> {
private Type type;
@Override
public ABC getObject() throws Exception {
if (type == Type.A) {
return new A();
} else if (type == Type.B) {
return new B();
}
return new C();
}
@Override
public Class<?> getObjectType() {
return ABC.class;
}
@Override
public boolean isSingleton() {
return true;
}
public void setType(Type type) {
this.type = type;
}
}
// 사용 시점
@Service
public class Use {
// factory class
public void use (Type type) {
// type 이 A 일 경우 A객체 반환
// type 이 B 일 경우 B객체 반환
// type 이 C 일 경우 C객체 반환
return // obj
}
}
답변