How should I rewrite this?
Hello, I have an abstract base class DataModelNode which has a number of
concrete subclasses. Each node can have a set of actions associated with
it. Action is an abstract base class with a number of concrete
subclasses. A given Action is created by an ActionFactory. I need be
able to register which ActionFactories work with a given node
dynamically so I wrote the following class:
package action;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import factory.action.ActionFactory;
import node.DataModelNode;
public class ActionRegistry {
public static void register(DataModelNode node, ActionFactory action) {
Set<ActionFactory> actions = null;
if (actionDecorations.containsKey(node)) {
actions = actionDecorations.get(node);
}
else {
actions = new HashSet<ActionFactory>();
}
actions.add(action);
actionDecorations.put(node, actions);
}
public static Set<ActionFactory> getActionFactories(DataModelNode
node) {
Set<ActionFactory> actionFactories = actionDecorations.get(node);
return actionFactories;
}
private static final Map<DataModelNode, Set<ActionFactory>>
actionDecorations = new HashMap<DataModelNode, Set<ActionFactory>>();
}
But when I was ready to register a certain ActionFactory with a certain
subclass of DataModelNode (i.e., when I was going to call register()), I
realised my mistake. This code requires an instance of a DataModelNode,
that won't work. I don't have any objects of the nodes when I need to
register. How should I rewrite this registry-class so I can associate
actionfactories with subclasses of DataModelNode? I can post more code
(a small, self-contained test case) if you weren't able to parse the
grammar of my question. Thanks!
- Fencer