This chapter outlined how a Java class can be instantiated . The chapter started out by describing the factory problem and why it was necessary. Although writing your own factory is not that difficult, it is better to use a formalized factory approach, as defined in the Lang package. The purpose of the factory is to reduce the number of hard dependencies that can occur in a project. Hard dependencies are bad because they cause source code to become brittle and difficult to modify. As source code becomes brittle, more and more problems surface, which increases maintenance costs. The factory always returns an interface that represents some instance of an implementation that the user of the interface is not aware of. The factory is the missing piece of the Commons Bridge best practice.
However, using a factory like the one presented in the lang package is not the only way to instantiate an object. There are other approaches, because developers often write frameworks that have plug-ins. For those situations, the factory is totally inappropriate to use since it is assumed that the factory knows which implementation to instantiate. Using the Discovery package, a developer can let the system administrator define the implementation. Of course, even with the Discovery package the client sees only an interface instance.
Finally, for those situations where a separate task needs to be instantiated, the Launcher package comes to assistance. Using the Launcher package, you can start another JVM process and assign it a number of characteristics. However, the Launcher package's downside is that the client has no interface to interact with. The client interacts only with the class Launcher , or the client has to establish some type of inter-process communication.
The objective of this chapter was to illustrate that creating an instance of a class is not always a simple and straightforward task. There are different strategies and objectives to solve, and you should consider each strategy carefully .
On the CD The sources to the concepts presented in this chapter are located under the directory [CDROM]/jseng/src/com.devspace. jseng .create .