Chapter 15. Building a Reusable Config Class
One of the most common external files you will find with any application is a configuration file. The purpose of a configuration, or config, file is to externalize settings so that they can be adjusted without needing to recompile the application. Though not mandatory, XML is usually the format of choice for creating a config file, and will be the format targeted for the Config class in this chapter to consume.
Defining the XML
Before you can get started on your class's architecture, you need to define what your XML will look like. Listing 15.1 is the basic structure you will be using.
<?xml version="1.0" encoding="UTF-8" ?> <config> <property name="exampleProperty1" value="I am value #1." /> <property name="exampleProperty2" value="I am value #2." /> <property name="exampleProperty3" value="I am value #3." /> </config>
Depending on the application, these properties could be a lot of things ranging from data refresh intervals to the location of other external files. By standardizing on a format like this, you can write a class that is reusable in all your applications. Should the need ever arise to make a more complicated config file, you can subclass your
Config class to handle the exceptions. This chapter looks at that later, but first let's focus on building the class itself.
Defining capability requirements
To begin, list the capabilities that you would like your
Config class to have. You want the
Config class to be pretty ...