내 프로그램에서 일부 개체는 다른 개체 (종속성)가 필요하며 Factory는 내 창조적 인 패턴으로 사용하고 있습니다.공장 패턴은이 방법으로 사용해도 괜찮습니까?
이제 간단한 종속성 문제를 어떻게 해결할 수 있습니까?
이것은 내 문제를 해결하기 위해 내가 수행 한 예입니다. Create 메소드에 필요한 객체를 보내는 것이 끔찍한 일이 아닌지 알고 싶습니다.
//AbstractBackground
// - SpecialBackground
// - ImageBackground
// - NormalBackground
class Screen{
List<AbstractBackground> list;
Cursor cursor;
ContentManager content;
public void load(string[] backgroundTypes){
//is this okay? --------------->
AbstractBackground background = BackgroundFactory.Create(backgroundTypes[0], cursor, content);
list.add(background);
}
}
class BackgroundFactory{
static public AbstractBackground Create(string type, Cursor cursor, ContentManager content){
if(type.Equals("special")){
return new SpecialBackground(cursor, content);
}
if(type.Equals("image")){
return new ImageBackground(content);
}
if(type.Equals("normal")){
return new NormalBackground();
}
}
}
http://codereview.stackexchange.com에 더 적합 – Leri
왜 잘못 되었습니까? 그렇지 않습니다. –
실생활 예제에서 공장 패턴에 대한 많은 경험이 없으므로 그 사실을 알고 싶었습니다. – OsakaHQ