 |
| |  |
Home In This Issue Email a Friend EasyPrint
 | |
|
Using a reusable code approach to HTML select option lists, part IV (continued)
This factory/consumer relationship is illustrated in Figure A.
FIGURE A
This diagram demonstrates the factory/consumer relationship. Click picture for a larger image.
The consumer orders the component, the factory consults the configuration parameters in effect and instantiates an object based on those parameters, and then the factory returns the object to the consumer. The consumer then uses the object to perform whatever function the object is designed to perform. Both the factory and the configured object interface directly with configuration and source data, fully insulating the consumer from the implementation-specific nature of the process.
Creating an OptionListSourceFactory We have to take just a little bit of liberty with the design of our factory here so that we can build something that works without getting into the complexities of application-specific configuration strategies. The factory needs access to some kind of configuration, and there are a number of approaches to providing configuration data to applications. A discussion of such strategies is well beyond the scope of this piece, so we're just going to create a little hard-coded set of parameters for demonstration purposes with the understanding that an actual factory would employ whatever mechanisms are already in place in your application framework for obtaining configuration information.
Other than that, our OptionListSourceFactory, presented below, is pretty straightforward.
package step.four;
import java.io.InputStream;
import java.util.Properties;
/**
* Factory for producing OptionListSource objects.
*/
public class OptionListSourceFactory {
private Properties config = getConfigProperties();
/**
* This is just a quick way to get some configuration
* properties to the factory. Actual implementations of a
* factory would use their own approach here.
*
* @return the configuration parameters in effect
*/
private static Properties getConfigProperties() {
Properties config = new Properties();
String name = "step/four/SampleConfiguration.properties";
InputStream is = null;
// Load the specified property resource
try {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
is = classLoader.getResourceAsStream(name);
if (is != null) {
config.load(is);
is.close();
}
} catch (Throwable t) {
// here is where you would invoke your own event management or logging
System.out.println("load properties error: " + t.toString());
} finally {
if (is != null) {
try {
is.close();
} catch (Throwable t) {
// here is where you would invoke your own event management or logging
System.out.println("load properties error: " + t.toString());
}
}
}
return config;
}
/**
* Returns a fully configured OptionListSource
* object based on the configuration parameters in effect.
*
* @param name the identifier of the specific OptionListSource
* @return a fully configured OptionListSource
* object based on the configuration parameters in effect
*/
public static OptionListSource getOptionListSource(String name) {
OptionListSourceFactory factory = new OptionListSourceFactory();
return factory.getOptListSource(name);
}
/**
* Returns a fully configured OptionListSource
* object based on the configuration parameters in effect.
*
* @param name the identifier of the specific OptionListSource
* @return a fully configured OptionListSource
* object based on the configuration parameters in effect
*/
public OptionListSource getOptListSource(String name) {
OptionListSource factoryProduct = null;
String typeProperty = name + ".option.list.source.type";
String type = config.getProperty(typeProperty);
if (type == null) {
// here is where you would invoke your own event management or logging
System.out.println("Requested OptionListSource (\"" + name + "\") not configured.");
} else if (type.equalsIgnoreCase("simple")) {
factoryProduct = getSimpleOptionListSource(name);
} else if (type.equalsIgnoreCase("properties")) {
factoryProduct = getPropertiesOptionListSource(name);
} else if (type.equalsIgnoreCase("sql")) {
factoryProduct = getSqlOptionListSource(name);
} else {
// here is where you would invoke your own event management or logging
System.out.println("Configured OptionListSource type (\"" + type + "\") not supported.");
}
return factoryProduct;
}
/**
* Returns a SimpleOptionListSource object
* based on the configuration parameters in effect.
*
* @param name the identifier of the specific OptionListSource
* @return a fully configured OptionListSource
* object based on the configuration parameters in effect
*/
private OptionListSource getSimpleOptionListSource(String name) {
String stringProperty = name + ".option.list.source.string";
String configString = config.getProperty(stringProperty);
return new SimpleOptionListSource(configString);
}
/**
* Returns a PropertiesOptionListSource object
* based on the configuration parameters in effect.
*
* @param name the identifier of the specific OptionListSource
* @return a fully configured OptionListSource
* object based on the configuration parameters in effect
*/
private OptionListSource getPropertiesOptionListSource(String name) {
String fileNameProperty = name + ".option.list.source.filename";
String fileName = config.getProperty(fileNameProperty);
return new PropertiesOptionListSource(fileName);
}
/**
* Returns a SqlOptionListSource object
* based on the configuration parameters in effect.
*
* @param name the identifier of the specific OptionListSource
* @return a fully configured OptionListSource
* object based on the configuration parameters in effect
*/
private OptionListSource getSqlOptionListSource(String name) {
String dataSourceNameProperty = name + ".option.list.source.dsname";
String dataSourceName = config.getProperty(dataSourceNameProperty);
String unfilteredSqlProperty = name + ".option.list.source.unfiltered.sql";
String unfilteredSql = config.getProperty(unfilteredSqlProperty);
String filteredSqlProperty = name + ".option.list.source.filtered.sql";
String filteredSql = config.getProperty(filteredSqlProperty);
return new SqlOptionListSource(dataSourceName, unfilteredSql, filteredSql);
}
}
|
[ Prev | Next ]
|
|
-- Advertisement --
EASY DEDICATED AND VIRTUAL DEDICATED SERVERS FOR AS LOW AS $67.99 PER MONTH
Customize and configure your own dedicated server. Simply choose one of our popular plans or select your own Linux or Windows server and plan options.
NO LONG WAITS. Server provisioned within hours.
Tap here now and be up and running with your own server tonight. |
-- Advertisement --
Influencer. Recommender. Decision Maker.
They all read WebSpherePower Magazine. They all rely on WebSpherePower Magazine.
If you want to reach the inner-circle of IBM IT professionals, you won't find a better resource than WebSpherePower Magazine.
Click for our Media Kit |
Copyright © 2008, ZATZ Publishing. All rights reserved worldwide.
|