在我的程序中,一些对象需要其他对象(依赖关系),而我使用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