|
|
|
|
|
|
|
|
|
|
|
|
|
|
Using a reusable code approach to HTML select option lists (continued)
package step.one;
import java.util.ArrayList;
/**
* <p>This interface specifies the required methods for an "Option
* List" source, which returns the name/value pairs for the options
* related to an HTML "select" tag.</p>
*/
public interface OptionListSource {
/**
* Returns an <code>ArrayList</code> of <code>LabelValueBean</code>
* objects defining the available options.
*
* @return an <code>ArrayList</code> of <code>LabelValueBean</code>
* objects defining the available options
*/
public ArrayList getOptions();
}
|
Create an Abstract class for the common code One of the nicest features of Java is its support of inheritance. When you want to create a collection of similar-but-different objects for any purpose, you can consolidate all of the "similar" into a base abstract class and then implement the "different" in any number of extensions of that class. Our base class will implement our interface and provide as much of the common logic as possible without attempting to implement any of the potential optional approaches. Our abstract class is presented in Listing 2:
package step.one;
import java.util.ArrayList;
/**
* This absract class implements the required methods for an "Option
* List" source, which returns the name/value pairs for the options
* related to an HTML "select" tag.
*
* This class must be extended by a concrete subclass which must
* implement the specified abstract method <code>loadOptions()</code>
* to load the list of available options from the implementation-specific
* data source.
*/
public abstract class OptionListSourceBase implements OptionListSource {
private ArrayList options = null;
/**
* Returns an <code>ArrayList</code> of <code>LabelValueBean</code>
* objects defining the available options.
*
* @return an <code>ArrayList</code> of <code>LabelValueBean</code>
* objects defining the available options
*/
public ArrayList getOptions() {
if (options == null) {
loadOptionsArray();
}
return options;
}
/**
* Obtains the available options from the implementation-specific
* load method.
*/
protected void loadOptionsArray() {
loadOptions();
if (options == null) {
options = new ArrayList();
System.out.println(this.getClass().getName() + ".loadOptions() produced no result.");
}
}
/**
* Loads the list of available options from the implementation-specific
* data source.
*/
protected abstract void loadOptions();
/**
* Sets the <code>ArrayList</code> of <code>LabelValueBean</code>
* objects defining the available options.
*
* @param options the <code>ArrayList</code> of <code>LabelValueBean</code>
* objects defining the available options
*/
protected void setOptions(ArrayList options) {
this.options = options;
}
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- Advertisement --
Learn Notes and Domino 8 at your place and pace!
Learn Notes and Domino in your office and/or home! TLCC's highly acclaimed distance learning courses for users, developers, and admins will enhance your career and your resume.
The many included activities and demos will make you a pro! Expert instructor help is a click away.
Click here to try a FREE demo course!! |
-- Advertisement --
Teamstudio Edition 25 has shipped
It's finally here! Now that Teamstudio Edition 25 has shipped, listen to our latest Tool Time audio program to find out what's changed. Updates to all your favorite Teamstudio tools will be discussed.
Plus, you'll get an introduction to Teamstudio Undo (formerly known as Teamstudio Snapper).
Tap here to get started! |
|
|
|
|
|
|
|
|
|
|