抽象工厂模式和属性 [英] Abstract Factory Pattern and Properties

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

问题描述

我对设计模式有些陌生,这是我在stackoverflow上的第一篇文章,因此希望这个问题会有意义。 Ive创建了一个抽象工厂来处理为不同的图表供应商(dundas,flash等)生成xml字符串。下面是我工厂的代码大纲(如果有帮助,我可以提供更多内容。)我希望客户能够设置所有类型的图表(标题,动画等)通用的属性。做这样的事情:

Im somewhat new to design patterns and this is my first post in stackoverflow, so hopefully this question will make sense. Ive created an abstract factory to handle generating xml strings for different chart vendors (dundas, flash, etc...). 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";

执行此操作的最佳方法是什么?现在,我在抽象创建器中设置属性,以便客户端可以访问它们,但是林还必须在我的工厂中复制这些属性,以便在构建xml时可以访问它们。

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.

//这是抽象工厂

public interface IXMLFactory
{

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

}

//这是抽象创建者

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;
    }


}


推荐答案

我不确定我是否了解它的整个范围,但是看来您应该能够创建一些代表不同类型图的共享属性的对象,并将其公开对象是抽象创建者的属性,可以由具体创建者访问,甚至可以作为参数传递给各个xmlFactory构造函数。调用者可以通过访问公开它的属性直接在该对象上设置这些属性,而具体的类可以从该对象读取它们。但是,这的确意味着调用者要经过一个更高级别的间接访问才能访问这些公共属性。

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. 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.

我不太了解您的重复项。您已经在抽象创建者上实现了属性,但是您还说过在工厂中复制了这些属性吗?您指的是具体的创造者吗?我不明白为什么-您指的是base.Caption,所以为什么要复制FusionGraphs中的任何内容(如果这些内容都是从GraphCreator继承的,并且您正在使用它的基类实现)?

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?

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

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