如何将特定属性设置为由抽象工厂创建的类? [英] How do you set specific properties to a class created by an abstract factory?

查看:137
本文介绍了如何将特定属性设置为由抽象工厂创建的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用抽象工厂模式,有可能让具体的工厂为他们创建具有类型特定参数的具体类吗?或者由各自的具体工厂创建的不同具体类别需要具有相同的字段?

Is it possible to have concrete factories create concrete classes with type specific parameters for them, using the abstract factory pattern? Or do the different concrete classes created by their respective concrete factories need to to have the same fields?

Ex)在下面的图片中,您将如何实例化WinButton和OSXButton与客户端(应用程序)给出的不同参数集?

Ex) In the image below how would you go about to instantiate the WinButton and the OSXButton with different set of arguments given by the client (Application)?

推荐答案

这不容易,没有更多的细节。我会做的一件事是尝试将参数传递给具体工厂类的构造函数。对于不同的争论,你必须创建不同的工厂实例,这可能不是最好的解决方案,但是,这取决于细节。

This is not easy without having more details. One thing I would do, is to try to pass the arguments to constructor of the concrete factory class. For different arguments you would have to create different factory instance, which might not be the best solution, but as said, this depends on details.

示例:


    GUIFactory factory = new WinFactory(WIN_9x_THEME);
    Button greyButton = factory.createButton();
    factory = new WinFactory(WIN_VISTA_THEME, 50);
    Button semiTransparentButton = factory.createButton();

这篇关于如何将特定属性设置为由抽象工厂创建的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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