Using the java.util.Properties Object to Decouple Applications

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:
data observability

Data Observability Explained

Data is the lifeblood of any successful business, as it is the driving force behind critical decision-making, insight generation, and strategic development. However, due to its intricate nature, ensuring the

Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular