简体   繁体   English

抽象工厂模式和属性

[英]Abstract Factory Pattern and Properties

Im somewhat new to design patterns and this is my first post in stackoverflow, so hopefully this question will make sense. 我有点新设计模式,这是我在stackoverflow中的第一篇文章,所以希望这个问题有意义。 Ive created an abstract factory to handle generating xml strings for different chart vendors (dundas, flash, etc...). 我已经创建了一个抽象工厂来处理为不同的图表供应商(dundas,flash等等)生成xml字符串。 Below is a code outline of my factory (I can include more if it helps.) Id like for my client to be able to set properties that will be common among all types of charts (caption, animation, etc.) So a client could do something like this: 下面是我工厂的代码大纲(如果它有帮助,我可以包含更多。)我希望我的客户能够设置所有类型的图表(标题,动画等)中常见的属性所以客户端可以做这样的事情:

        GraphCreator fusion = new FusionGraphs();

        //set the props for the graph
        fusion.Caption = "Fusion 2D Line Chart";

What is the best way to do this? 做这个的最好方式是什么? Right now, Im setting properties in the abstract creator so the client can have access to them, but Im also having to duplicate these properties in my factory so I can have access to them in building the xml. 现在,我在抽象创建器中设置属性,以便客户端可以访问它们,但我还必须在我的工厂中复制这些属性,以便我可以在构建xml时访问它们。

//this is the abstract factory //这是抽象工厂

public interface IXMLFactory
{

    //add interface methods
    IRoot makeRoot();
    IRootAttrib makeRootAttrib();
    INodes makeNodes();
    INodeAttrib makeNodeAttrib();

}

//this is the abstract creator //这是抽象的创造者

public abstract class GraphCreator
{

    public virtual Graph getGraph(Graph.Types graphType)
    {
        //abstract product
        Graph graph;

        //abstract product creation
        graph = buildGraph(graphType); 

        graph.draw(); 

        return graph;

    }

    public abstract Graph buildGraph(Graph.Types graphType);

}

//this is the concrete creator //这是具体的创造者

public class FusionGraphs : GraphCreator
{
    Graph g = null;

    //XML parts for fusion 2D multi series
    IXMLFactory xmlFactory;


    //use xml parts that are needed for the type of fusion graph requested
    public override Graph buildGraph(Graph.Types graphType)
    {
        switch (graphType)
        {
            case Graph.Types.Single2DLine:
                xmlFactory = new Fusion2DSingleXMLFactory();
                g = new Single2DLineGraph(xmlFactory);
                xmlFactory.Caption = base.Caption;                     
                break;
            case Graph.Types.Single2DBar:
                xmlFactory = new Fusion2DSingleXMLFactory();
                g = new Single2DBarGraph(xmlFactory);

                break;
        }


        return g;
    }


}

I'm not sure if I'm understanding the whole scope of this, but this seems like you should be able to create some object that represents the shared properties of the different types of graphs, and expose that object as a property of the abstract creator, accessible by the concrete creators, and maybe even passed as a parameter to the individual xmlFactory constructors. 我不确定我是否理解了整个范围,但这似乎你应该能够创建一些表示不同类型图形的共享属性的对象,并将该对象公开为抽象属性创建者,可由具体创建者访问,甚至可以作为参数传递给各个xmlFactory构造函数。 The caller can set these properties directly on that object by accessing the property that exposes it, and the concrete classes can read them from that object. 调用者可以通过访问公开它的属性直接在该对象上设置这些属性,具体类可以从该对象读取它们。 That does mean, however, that the caller goes through one more level of indirection to access these common properties. 但是,这确实意味着调用者通过一个更多级别的间接访问这些公共属性。

I don't quite understand what duplication you have. 我不太明白你有什么重复。 You have properties implemented on the abstract creator, but you said you also "duplicate these properties in the factory"? 您在抽象创建者上实现了属性,但您说您还“在工厂中复制这些属性”? Are you referring to the concrete creator? 你指的是具体的创造者吗? I don't see why -- you are referring to base.Caption, so why would you need to duplicate anything in FusionGraphs, if it's all inherited from GraphCreator, and you're using the base class implementation of it? 我不明白为什么 - 你指的是base.Caption,那么为什么你需要在FusionGraphs中复制任何东西,如果它都是从GraphCreator继承的,你正在使用它的基类实现?

I think somehow your problem is how to share configuration information between the class which calls the factory and the factory itself. 我想你的问题是如何在调用工厂的类和工厂本身之间共享配置信息。 Implement a separate class that holds the configuration information (eg caption), and then give a reference to it both to the factory and the creator. 实现一个包含配置信息(例如标题)的单独类,然后向工厂和创建者提供对它的引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM