Caching of features [message #1092844] |
Fri, 23 August 2013 04:58  |
Eclipse User |
|
|
|
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.
|
|
|
Re:Caching of features [message #1093855 is a reply to message #1092844] |
Sat, 24 August 2013 15:48  |
Eclipse User |
|
|
|
There is no caching of features in the framework, since you could
possibly return different features for the same context due to
the state of the diagram, or many other reasons. However, nothing
prevents you either from implementing such a caching yourself. A
LayoutFeature can be very easily implemented with the Singleton
Pattern, if it needs no state.
----Android NewsGroup Reader----
http://www.piaohong.tk/newsgroup
|
|
|
Powered by
FUDForum. Page generated in 0.03841 seconds