2012-12-31 40 views
3

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

更适合http://codereview.stackexchange.com – Leri

+0

为什么会这样错?不是。 –

+0

我想知道,因为我在现实生活中没有那么多的工厂模式经验。 – OsakaHQ

回答

4

简单的答案,它看起来不错。如果你抽象地想到这一点,你就是通过create方法将这些对象注入到构造函数中。这种技术没什么问题,并且是我推荐的。后来,如果您需要更改实现,则可以根据需要创建其他创建方法而不会破坏任何内容。

1

除了事实上,如果您的依赖关系树增长您创建的工厂方法将变得复杂,事实上在您的代码中没有这么丑。 为了分解具有各种关联依赖关系的类型,最好选择基于IoC的工厂。通过在容器中注册依赖项,您将拥有具有所需依赖项的自动注入构造函数。

+0

我可以问一个例子吗?我遇到过很多例子,但有些解决方案似乎太复杂,无法解决简单的问题,或者我不知道解决我想要的问题的例子。 – OsakaHQ

+0

@ user658091如果你的架构应该保持简单,留下来。正如所说的那样,你的代码目前没有错。 –

5

它是功能性的,但是,如果添加更多类型,它可能会变得很麻烦。
根据我个人的喜好了简单工厂的实现将是:

enum BackgroundFactoryType 
{ 
    Special, 
    Image, 
    Normal, 
} 

static class BackgroundFactory{ 

    static Dictionary<BackgroundFactoryType, Func<Cursor, ContentManager, AbstractBackground>> constructors; 

    static BackgroundFactory() 
    { 
    //initialize the constructor funcs 
    constructors = new Dictionary<BackgroundFactoryType, Func<Cursor, ContentManager, AbstractBackground>>(); 
    constructors.Add(BackgroundFactoryType.Special, (cursor, content) => new SpecialBackground(cursor, content)); 
    constructors.Add(BackgroundFactoryType.Image, (_, content) => new ImageBackground(content)); 
    constructors.Add(BackgroundFactoryType.Normal, (_, __) => new NormalBackground()); 
    } 

    static public AbstractBackground Create(BackgroundFactoryType type, Cursor cursor, ContentManager content) 
    { 
    if (!constructors.ContainsKey(type)) 
     throw new ArgumentException("the type is bogus"); 

    return constructors[type](cursor, content); 
    } 
} 

,或者你可以简单地做:

static class BackgroundFactory{ 

    static public AbstractBackground Create(BackgroundFactoryType type, Cursor cursor, ContentManager content) 
    { 
    switch (type) 
    { 
     case BackgroundFactoryType.Special: 
     return new SpecialBackground(cursor, content); 
     case BackgroundFactoryType.Image: 
     return new ImageBackground(content); 
     case BackgroundFactoryType.Normal: 
     return new NormalBackground(); 
     default: 
     throw new ArgumentException("the type is bogus"); 
    } 
    } 
} 

这种方法的一个很好的副作用,是它只需一点点工作,使这个东西配置驱动,而不是硬编码。

+0

由于'BackgroundFactory'只有静态成员不应该被标记为'静态'? – Leri

+0

是的,它应该和构造函数字典:) – SWeko

相关问题