向抽象工厂添加属性 [英] Adding Properties to Abstract Factory

查看:89
本文介绍了向抽象工厂添加属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对设计模式还比较陌生,并且在我正在研究的项目中使用GangOfFour Abstract Factory模式。我想知道引入名为FileName的字符串类型的属性的最佳方法,这是混凝土工厂生产的所有抽象产品所必需的。我是否会

I am relatively new to design patterns and am playing around with the GangOfFour Abstract Factory pattern in a project I am working on. I wanted to know the best way to introduce a property of type string called FileName which is needed for all of the Abstract Products produced by a Concrete Factory. Would I

将其添加到Abstract Factory接口中,以便必须在树下实现该结构并将其传递到返回的Product的构造函数中。即使该接口只与创建工厂有关?

Add it to the Abstract Factory interface so that it has to be implemented down the tree and passed into the constructor of the returned Product. even though that interface is only concerned with creating factories?

我将使用GoF .Net Optomised代码的一部分,以示例为例,其他任何人都可以轻松地学习这些模式将很熟悉它,并可能在将来提供一个很好的参考点。

I will use a section of the GoF .Net Optomised code to use an example as its simple any anybody else learning these patterns will be familiar with it and may provide a good reference point in the future.

/// <summary>
/// The 'AbstractFactory' interface. 
/// </summary>
interface IContinentFactory
{
    // Define property in here??
    IHerbivore CreateHerbivore();
    ICarnivore CreateCarnivore();
}

/// <summary>
/// The 'ConcreteFactory1' class.
/// </summary>
class AfricaFactory : IContinentFactory
{
    // Implement property in here??
    public IHerbivore CreateHerbivore()
    {
        return new Wildebeest(PassInPropertyHere??);
    }

    public ICarnivore CreateCarnivore(PassInPropertyHere??)
    {
        return new Lion();
    }
}


推荐答案

I认为这取决于工厂和工厂自己创建的对象的性质。如果无法在没有某些信息的情况下创建数据(例如本例中的FileName),则有两种选择:工厂知道该数据或需要将其提供给工厂(这里的一种选择是在方法调用期间传递该数据)。否则,如果从创建的角度来看此信息是可选的,则您不应为此烦杂工厂,并将此负担加在调用代码上(例如,可以在对象从工厂返回后立即将所创建对象的FileName属性设置为必要的值) )。

I think it depends on the nature of objects that are created by factory and factories themselves. If it cannot be created without some information (like FileName in this case) you have two choices: either your factories know that data or you need to supply it to factories (and one the options here is to pass that data during method call). Otherwise if this information is optional from creation point of view you should not clutter factories with this and put this burden on the calling code (for example, FileName property of the created object can be set to necessary value right after the object is returned from factory).

这篇关于向抽象工厂添加属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆