S
Scaramanga
Hello Java Experts!
I'm rather new to designpatterns and I tried to make a Factory of some
kind that can return Commands of various types (create, find, update,
delete) and of different families (different cases).
The code below works but I would like to hear from you experts what you
think of it. CanI go with this design? I specify the Factories and
Commands in .properties files.
T.I.A.
/S
****************************************************************************************
CLIENT
[...]
CommandFactory cf = CommandFactory.getFactory("casetwo");
Command c = cf.getCommand("update");
System.out.println(c.class.getName());
[...]
Gives the output
[...].command.UpdateCaseTwoCommand
And that's what I want; a command of "update" type from the "casetwo"
factory...
****************************************************************************************
public abstract class CommandFactory {
private static Hashtable factories;
Hashtable commands = new Hashtable();
static {
try {
factories = classProperties(CommandFactory.class);
//<-------- ???
}
catch (Exception ex) {
throw new RuntimeException(ex);
}
}
public CommandFactory(Class c) throws FactoryException {
try {
commands = classProperties(c);
}
catch (Exception ex) {
throw new FactoryException(ex);
}
}
public static CommandFactory getFactory(String factory) throws
FactoryException {
return (CommandFactory) factories.get(factory);
}
private static Hashtable classProperties(Class c) throws
ClassNotFoundException, InstantiationException, IllegalAccessException,
IOException {
String classname = c.getName();
String prefix = classname.substring(classname.lastIndexOf(".") +
1);
String suffix = "properties";
Properties propsfile = new Properties();
propsfile.load(c.getResourceAsStream(prefix + "." + suffix));
Hashtable properties = new Hashtable();
Map.Entry entry = null;
for (Iterator i = propsfile.entrySet().iterator(); i.hasNext(); ) {
entry = (Map.Entry) i.next();
properties.put( (String) entry.getKey(),
ObjectCreator.createObject( (String) entry.getValue()));
}
return properties;
}
public abstract Command getCommand(String action) throws
FactoryException;
}
****************************************************************************************
public class CaseOneCommandFactory extends CommandFactory {
public CaseOneCommandFactory() throws FactoryException {
super(CaseOneCommandFactory.class);
//<-------- ???
}
public Command getCommand(String action) throws FactoryException {
return (Command) commands.get(action);
}
}
****************************************************************************************
And the property file for the factory...
"CommandFactory.properties"
caseone=[...].factory.CaseOneCommandFactory
casetwo=[...].factory.CaseTwoCommandFactory
casethree=[...].factory.CaseThreeCommandFactory
casefour=[...].factory.CaseFourCommandFactory
And one command property file for each factory (four of them)
"CaseOneCommandFactory.properties"
create=[...].command.CreateCaseOneCommand
find=[...].command.FindCaseOneCommand
update=[...].command.UpdateCaseOneCommand
delete=[...].command.DeleteCaseOneCommand
"CaseTwoCommandFactory.properties"
create=[...].command.CreateCaseTwoCommand
find=[...].command.FindCaseTwoCommand
update=[...].command.UpdateCaseTwoCommand
delete=[...].command.DeleteCaseTwoCommand
and so on...
I'm rather new to designpatterns and I tried to make a Factory of some
kind that can return Commands of various types (create, find, update,
delete) and of different families (different cases).
The code below works but I would like to hear from you experts what you
think of it. CanI go with this design? I specify the Factories and
Commands in .properties files.
T.I.A.
/S
****************************************************************************************
CLIENT
[...]
CommandFactory cf = CommandFactory.getFactory("casetwo");
Command c = cf.getCommand("update");
System.out.println(c.class.getName());
[...]
Gives the output
[...].command.UpdateCaseTwoCommand
And that's what I want; a command of "update" type from the "casetwo"
factory...
****************************************************************************************
public abstract class CommandFactory {
private static Hashtable factories;
Hashtable commands = new Hashtable();
static {
try {
factories = classProperties(CommandFactory.class);
//<-------- ???
}
catch (Exception ex) {
throw new RuntimeException(ex);
}
}
public CommandFactory(Class c) throws FactoryException {
try {
commands = classProperties(c);
}
catch (Exception ex) {
throw new FactoryException(ex);
}
}
public static CommandFactory getFactory(String factory) throws
FactoryException {
return (CommandFactory) factories.get(factory);
}
private static Hashtable classProperties(Class c) throws
ClassNotFoundException, InstantiationException, IllegalAccessException,
IOException {
String classname = c.getName();
String prefix = classname.substring(classname.lastIndexOf(".") +
1);
String suffix = "properties";
Properties propsfile = new Properties();
propsfile.load(c.getResourceAsStream(prefix + "." + suffix));
Hashtable properties = new Hashtable();
Map.Entry entry = null;
for (Iterator i = propsfile.entrySet().iterator(); i.hasNext(); ) {
entry = (Map.Entry) i.next();
properties.put( (String) entry.getKey(),
ObjectCreator.createObject( (String) entry.getValue()));
}
return properties;
}
public abstract Command getCommand(String action) throws
FactoryException;
}
****************************************************************************************
public class CaseOneCommandFactory extends CommandFactory {
public CaseOneCommandFactory() throws FactoryException {
super(CaseOneCommandFactory.class);
//<-------- ???
}
public Command getCommand(String action) throws FactoryException {
return (Command) commands.get(action);
}
}
****************************************************************************************
And the property file for the factory...
"CommandFactory.properties"
caseone=[...].factory.CaseOneCommandFactory
casetwo=[...].factory.CaseTwoCommandFactory
casethree=[...].factory.CaseThreeCommandFactory
casefour=[...].factory.CaseFourCommandFactory
And one command property file for each factory (four of them)
"CaseOneCommandFactory.properties"
create=[...].command.CreateCaseOneCommand
find=[...].command.FindCaseOneCommand
update=[...].command.UpdateCaseOneCommand
delete=[...].command.DeleteCaseOneCommand
"CaseTwoCommandFactory.properties"
create=[...].command.CreateCaseTwoCommand
find=[...].command.FindCaseTwoCommand
update=[...].command.UpdateCaseTwoCommand
delete=[...].command.DeleteCaseTwoCommand
and so on...