Using the java.util.Properties Object to Decouple Applications

Assume that you have a set of classes that need to provide the same functionality but the way they provide it is different. In essence, these are the classes that implement the same interface.

The problem lies in writing the logic to decide which of these class to instantiate at runtime. An easy way to design it, while making it totally decoupled, is to store the class names in a properties file which can be loaded into the java.util.Properties object.

Assume that the classes are implementing the interface Intf. And as mentioned their names are already loaded into the Properties object using the load() method.

At runtime, a factory can be used which accepts a parameter to identify the implementing class to be instantiated:

 public class Factory{private Intf interfc;public Intf getObject(String classIdentifier){ // Read the class corresponding to classIdentifier from _the Propertiesobject. Class cls = Class.forName(classIdentifier); interfc = cls.newInstance(); return interfc;}}

The biggest advantage of this approach is that it helps you to totally decouple your application logic from the set of different classes that the factory might instantiate. Moreover, it helps you do away with the ‘if loops’ to decide which class is to be instantiated.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS