jeudi 19 novembre 2015

Suggestions for implementing a solution using Singleton design pattern

I need to implement a solution as part of Test framework & I am considering singleton pattern for reasons explained below. However, I am not able to achieve my intended solution & therefore would need some suggestions/inputs on possible implementations.

Problem Statement : I have a environment (env of the product I am testing) configuration properties file which I want to load & make the value of the parameters accessible globally to the test framework. I figured using the singleton pattern because these properties are one-time values (should report an exception if tried to initialize more than once), should be available globally & have an one-point access to the methods.

However, the list of properties/parameters is really long & therefore it's wise to break it into modules (classes). For the below explanation, I tried with composition.

For e.g.

public class Configuration {
    private static Configuration configObj;
    private static Database dbDetails;
    private static Machine macDetails;
    //...
    //... many more modules

    public static synchronized void createInstance(Properities envProps){
          //Should create only one instance of Configuration 
          // and should also initialize Database & Machine objects.
    }

    public static Configuration getConfigObject(){
         return configObj;
    }
}


public class Database {
    private static String dbConnectString;

    public Database(String dbcs){
         dbConnectString = dbcs;
    }

    public static String getDbConnectString(){
         return dbConnectString;
    }
}


public class Machine {
    private static String hostname;
    private static String loginUsername;

    public Machine(String hostname,String loginUsername){
        this.hostname = hostname; //It may include some trimming/cleaning
        this.loginUsername = loginUsername;
    }

    public static String getHostName(){
        return hostname;
    }
}

PS: Just a sample typed-in code for the understanding of my problem statement.

Expectation : The expectation now is that when trying to get the hostname, I should have a single point of access via Configuration static object (assuming that I have initialized all member variables successfully) i.e.

String hostname = Configuration.getHostname();

OR

String hostname = Configuration.getConfigObject().getHostname();

Current Issue : How to create one static object that will refer to all methods using either composition or inheritance (Conceptually, composition would be the right approach).

Multiple Inheritance would have solved the issue but Java doesn't support so ruled out. Cannot consider Interfaces either because overriding all methods is tedious & lengthy & the parameters/methods will keep changing over-time.

Please bare with me if I have not been able to express my problem statement clearly. Also, please excuse the code mentioned in Configuration class since I have just tried to showcase what I am expecting & at this point I am not clear how to implement it.

All suggestions are welcome even if it requires to scrap this design pattern & try something different. Appreciate everyone's input in advance !!!

Aucun commentaire:

Enregistrer un commentaire