Re: Factory pattern and building object
- From: Abhijat Vatsyayan <abhijatdotvatsyayan@xxxxxxxxxxxxxxxx>
- Date: Tue, 27 Sep 2005 18:08:33 -0700
opamail1-googlenews@xxxxxxxxx wrote:
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
May be you should create a DiagramItemObjectState class instances of which should store all the state data for a given DiagramItem object. You should then serialize the state object and optionally provide this state information to the factory to restore the state of the newly created object.
I know its not very clear , so let me try to write some rough code -
DiagramItemObjectState
{
}
SquareObjectState extends DiagramItemObjectState
{
}DiagramItemDescriptor
{
/**
* Can not be null ;
**/
DiagramItemType type ;
/**
* May be null for newly created objects (or do something
* more elegant ).
**/
DiagramItemObjectState state ; get/setType() ;
get/setState() ;
}
DiagramItem
{
/**
* This probably should not handle a null state !
* [Question]: Should you allow this only for not yet
* initialized objects ?
**/
void initializeFrom(DiagramItemObjectState newState)
{
setState(newState) ;
}
private void setState(DiagramItemObjectState newState) ;
/**
* Use this method to store the state of the DiagramItem object
**/
DiagramItemDescriptor getDescriptor()
{
return new DiagramItemDescriptor(getState(), getType());
}
}Add a method in your factory class -
public DiagramItem createItem(DiagramItemDescriptor desc)
{
DiagramItem item = createDiagramItem(desc.getType()) ;
item.initializeFrom(desc.getState()) ;
return item ;
}
There are several decisions that you will have to make -
[*] Do you need two createItem methods ?
[*] Should the initializeFrom use a Descriptor ?
[*] Is the name "descriptor" good ?and so on .. but most of these will depend more on you and your application.
The key idea as far as I am concerned is that you should separate the state of the DescriptorItem object in another class and store the state in the objects of this new class.
If you change the type of your object by using a different factory,
the state object may not be compatible with this new type and "initializeFrom" may fail.
Another problem is that it introduces parallel hierarchies as well as doubles the number of classes in the set.
.
- References:
- Factory pattern and building object
- From: opamail1-googlenews
- Factory pattern and building object
- Prev by Date: (Slightly)OT : Any patterns/idioms for implementing a decision table in OO language such as Java
- Next by Date: Really Stupid Question
- Previous by thread: Re: Factory pattern and building object
- Next by thread: Re: Factory pattern and building object
- Index(es):