Caching of features [message #1092844] |
Fri, 23 August 2013 08:58 |
Albert Hofkamp Messages: 41 Registered: August 2009 |
Member |
|
|
While working on layout features, I noticed the following pattern:
1. The feature provider finds which feature to provide for layout of an element.
2. When found, it creates the new feature.
3. The feature itself has a check (canLayout) whether an element can be layout.
Before today, I just duplicated those checks. Today, I did the following.
In the layout feature:
public class EventLayoutFeature extends AbstractLayoutFeature {
...
public static boolean canLayout(PictogramElement pe) {
String val = pes.getPropertyValue(pe, EVENT_ELEMENT);
return val != null && val.equals(NAME);
}
@Override
public boolean canLayout(ILayoutContext context) {
PictogramElement pe = context.getPictogramElement();
return canLayout(pe);
}
@Override
public boolean layout(ILayoutContext context) {
...
}
}
And in the feature provider:
@Override
public ILayoutFeature getLayoutFeature(ILayoutContext context) {
PictogramElement pe = context.getPictogramElement();
if (EventLayoutFeature.canLayout(pe)) {
return new EventLayoutFeature(this);
}
...
} I literally perform the same check in both occasions, except I cannot call "canLayout(ILayoutContext context)", because it is an instance method.
This leads to the question whether you cannot make an EventLayoutFeature once, and use that instead (the layout feature class has no state in my code). It would simplify the feature provider code to "if(instance.canLayout(context)) return instance;".
However, is this allowed by Graphiti, and is this wanted. Does eg Graphiti itself have this caching? (although in the latter case, you may want to just give all features to graphiti at startup instead of this querying and creation on demand.
Looking at other places in the feature provider, I now realize I do this everywhere. It would reduce code duplication, and possibly eliminate creation of many feature objects.
|
|
|
|
Powered by
FUDForum. Page generated in 0.02440 seconds