Re: Factory pattern and building object
- From: iamfractal@xxxxxxxxxxx
- Date: 27 Sep 2005 00:18:08 -0700
opamail1-googlenews@xxxxxxxxx skrev:
> I am creating a FlowChart type GUI application which has several
> diagram items. I have a DiagramItemFactory which creates concrete
> diagram item objects. I call this factory to create diagram items at
> runtime via drag and drop, insert or other. Then I serialize my diagram
> to an XML file. Everything is ok so far.
>
>
> Sample C# code:
> public class DiagramItemFactory
> {
>
> public DiagramItem CreateItem(DiagramItemType itemType)
> {
> DiagramItem diagramItem = null;
> switch (itemType)
> {
> case DiagramItemType.Square:
> Square square = new Square(); // square class
> diagramItem = square;
> break;
>
> case DiagramItemType.Circle:
> Circle cirlce = new Circle(); // circle class
> diagramItem = square;
> break;
> }
> return diagramItem;
> }
> }
>
> My problem is when I open the file a go through the diagram items
> defined in the file, I want to the DiagramItemFactory again to create
> the diagram items for the diagram, but the problem is that
> the factory only creates the default diagram items and the file
> also contains size, color, coordinate and other properties.
>
> Is there another pattern I should be using for opening up the file
> and rendering the flow chart diagram?
>
> Thank You,
>
> Opa
I don't know any clever way to do this.
The problem with serialising is that you are saving just the data of
the object, not the behaviour; so by saving the data of a DiagramItem,
you lose all association with the behaviour of the Square (for example)
of which it is an abstraction.
The only way around this is to save the assocation between the data and
the behaviour in the data itself, i.e., each shape must be created with
its associated DiagramItemType, which is also serialised. Thus, the
factory can reconstruct the original object from the DiagramItemType it
reads from the XML.
Not pretty, but as long as you encapsulate the
serialisation/deserialisation functionality away from everything else,
it shouldn't be difficult to change it when something more appropriate
comes along (if ever).
..ed
--
www.EdmundKirwan.com - Home of The Fractal Class Composition.
.
- References:
- Factory pattern and building object
- From: opamail1-googlenews
- Factory pattern and building object
- Prev by Date: Re: Interface complexity problem in game
- Next by Date: Breaking the Last Dependency in C++?
- Previous by thread: Re: Factory pattern and building object
- Next by thread: Re: Factory pattern and building object
- Index(es):
Relevant Pages
|