Java data access

Page 175

Chapter 10: Building the Singleton Pattern In This Chapter • Understanding the purpose of the Singleton pattern • Understanding the structure of the Singleton pattern • Implementing the Singleton pattern • Making a Singleton thread−safe In Chapter 9, I introduced the subject of design patterns and presented the 23 patterns in the book Design Patterns by Gamma, Helm, Johnson, and Vlissides. In this chapter, I go into more detail about one of the more useful and frequently used patterns, the Singleton. The Singleton pattern is a creational pattern that you use to ensure that only one instance of an object exists. This means that other objects share the same instance. Why would you want only one instance of an object? Think of the Singleton as a dispatcher. All communications must go through the same “person.” The dispatcher has complete control over what gets communicated. The Singleton pattern is advantageous when you need to emulate a single point of contact as with the dispatcher just described. I begin the chapter with a discussion of the details of the Singleton pattern. Next, I present its structure and present two implementation examples. The first example shows the basic implementation of the Singleton pattern, and the second shows how to apply it to JDBC programming to create a connection manager.

What Is a Singleton Pattern? The purpose of most creational patterns is to create other objects. The purpose of the Singleton pattern, however, is to ensure that only one instance of an object exists at any one time. Other client objects, instead of instantiating another Singleton, retrieve a reference to the active object. A Singleton object also maintains responsibility for controlling its only instance. It hides its constructor to prevent other objects from instantiating it. A Singleton object provides an access method with which clients may retrieve a reference to the instance as opposed to calling the constructor directly. Using a Singleton pattern is not the only technique you can use to create a single−object instance. For example, you can declare a global variable in a class and instantiate it once within your application. However, if you are creating a toolkit or application framework you may not want other objects, over which you have no control, to create another instance. Singletons provide the solution to this problem. So when would you want to implement an object as a Singleton? One example is when you need to implement a “manager” object of some sort. Manager objects can do many things — such as controlling other objects, providing objects to clients, or overseeing services such as printing. By making a manager a Singleton you ensure that all requests funnel through the same object. With respect to JDBC programming, a connection manager is an ideal candidate to implement as a Singleton. The connection manager can provide a single protected point of access into the database. You can pre−configure connection attributes such as username, password, and JDBC URL so they are consistent and also eliminate duplicate connection code throughout your application. You can also use a connection manager 163


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.