Tag archive for: Design Patterns

Ways to implement the Singleton pattern in Java

After you got started with Java programming, chances are you will want to use some design patterns to organise your Java classes.

One common object oriented design pattern is the Singleton pattern. The Singleton pattern is a technique where you can apply to ensure that there is only one instance of your Java class per runtime.

The Singleton pattern is usually used for grouping functionalities that allow callers to act on a single set of state. Examples of cases where the Singleton pattern can be applied include:

  • A class that keeps track of a customized configurations from a configuration file.
  • A class that facilitates interactions with a database system.
  • A class that performs logging.
  • A class that caches data read from previous database calls.

This post documents some ways to implement the Singleton pattern in Java.

Implementing the Singleton Pattern in Visual Basic.Net

Object creation can be expensive in terms of processing time and memory utilization. While there are cases where multiple object instances are inevitable to getting things done, there are certainly cases where only a single instance of object is sufficient. For example, if your program is only going to access a single database instance, it suffice to dedicate a single object that manages database connection(s) to your database server. Other examples are objects used for logging messages, objects used for loading/saving configuration details, objects used for managing threads and objects used for managing access to shared hardware devices such as the printer.

My thoughts on “Head First Design Patterns”

Cover page of Head First Design Patterns

Many software projects fail because of their programmers having the “just produce the code” mentality. Indeed, the motivation of design patterns is to give programmers a suite of techniques to isolate areas of changes, so that changes become less painful when a bug occurs or when customers demand more functionality out of the software. Personally, before picking up this book, I had learnt design patterns through goggling and piecing up different fragments of knowledge. However, I feel that I need more assurance in order to pioneer the development of a software project.