I am looking for a pattern that applies to configuring placement of graphic objects.
Here’s my problem:
I have some data objects with properties such as name, description, image etc.
I want to draw representations of these objects onto a canvas.
There are different “layouts” that can be applied. All the layout definitions are there at compile time.
In the layout description I want to be able to define the following for any of the object properties:
size: for text of course this doesn’t mean the length/height of the whole text but of a character
position:
-
either absolute to the starting point of the representation
-
or relative to other displayed properties (e.g.
thisPropertyDisplay.x = anotherPropertyDisplay.x + anotherPropertyDisplay.length)
Of course I could explicitly code the placement in n blocks/methods, one for each layout. Is there a better practise or an idiom how to implement this problem in Java?
What I actually want to do is I want to describe something like that:
property1.x = 0; property1.y = 0;
property2.x = property1.Endpoint.x; property2.y = property1.y;
property3.x = property1.Endpoint.x; property3.y = property2.Endpoint.y;
That definition is not explicitly coded in n Java methods, but there is one method able to parse all layout definition and apply them to the objects.
If the computation of the text size is beyond a useful pattern, just assume the length and height is always given.
Does that make sense? Is there a pattern / idiom name for it?
Decorator pattern seems to be the right one:
http://en.wikipedia.org/wiki/Decorator_pattern
The idea is you will have another class that will extend the behavior to handling the position and size information.
Hope it helps