rahkim
rahkim

Reputation: 941

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

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.

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


}

Upvotes: 3

Views: 1661

Answers (2)

BlueMonkMN
BlueMonkMN

Reputation: 25601

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.

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?

Upvotes: 1

Antti Huima
Antti Huima

Reputation: 25522

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 (e.g. caption), and then give a reference to it both to the factory and the creator.

Upvotes: 0

Related Questions