1
\$\begingroup\$

This is a scenario where we keep a single instance(as in Singleton Pattern) to get the configuration of an application.

Below is the main class and i set values to the configuarion values here and call the methods of the other classes to check if they are getting the correct values.

public class A{
    public static void main(String []args){
        System.out.println("My Singleton example");
        System.out.println("Setting values to configs\n");
        configs conf = configs.getInstance();
        conf.setName("Home Page");
        conf.setUrl("www.google.com");

        B.showConfigValues();
        C.showConfigValues();

    }
}

Below is the singleton class.

public class configs{

    private static configs appConfig;
    private String name;
    private String url;

    private configs(){}

    public static configs getInstance(){
        if( appConfig == null){
            appConfig = new configs();       
        }
        return appConfig;
    }

    public void setUrl(String url){
        this.url = url;
    }

    public void setName(String name){
        this.name = name;
    }

    public String getUrl(){
        return url;
    }

    public String getName(){
        return name;
    }
}

Below is class B and C. They access the configuration values in their methods.

public class B{

    public static void showConfigValues(){
        configs conf = configs.getInstance();
        System.out.println("Geting configs in Class B");
        System.out.println("url : "+conf.getUrl());
        System.out.println("name : "+conf.getName()+"\n");
    }
}

public class C{

    public static void showConfigValues(){
        configs conf = configs.getInstance();
        System.out.println("Geting configs in Class C");
        System.out.println("url : "+conf.getUrl());
        System.out.println("name : "+conf.getName()+"\n");
    }
}

Below is the output.

$ java A
My Singleton example
Setting values to configs

Geting configs in Class B
url : www.google.com
name : Home Page

Geting configs in Class C
url : www.google.com
name : Home Page

My question is, is this a good example to explain the singleton pattern?

\$\endgroup\$

closed as off-topic by forsvarir, Peilonrayz, Jamal Dec 14 '16 at 14:08

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ It's not a great example. Whilst it may cover the shared nature of the singleton, the instance is constructed in an unpopulated state. What happens if B or C is used before A sets up there data? \$\endgroup\$ – forsvarir Dec 14 '16 at 9:30
  • \$\begingroup\$ @forsvarir can this code be changed to overcome the issues what you have mentioned? \$\endgroup\$ – Harsha Jayamanna Dec 14 '16 at 9:36
  • \$\begingroup\$ I have rolled back the last edit. Please see what you may and may not do after receiving answers. \$\endgroup\$ – Heslacher Dec 14 '16 at 9:45
0
\$\begingroup\$

For explaining the Singleton pattern the ABC naming is confusing. Just come up with some real name, e.g. Database, Logger etc. Something that might need access to a common config. Instead of System.out the config, you might want to add some mock code to init the database connection or get the logging directory.

Please keep in mind that Java classes usually are starting with a capital letter.

The static instance variable is usually called only instance.

In which context do you want to explain the pattern. Maybe we find a more suitable example?

Check http://www.oodesign.com/singleton-pattern.html for a nice explanation and some examples and drawbacks.

Please be aware that a Singleton might seem to help you sometimes in production code, but it definitely makes your life harder in testing your code. See "Dependency Injection" for an alternate approach.

\$\endgroup\$
  • \$\begingroup\$ Thanks for information and this will definitely be helpful to sharpen my knowledge. I have corrected the class name. I think i will be able to find a much more proper example in the link you have provided. Using DI as an alternative to Singleton is the next thing in my mind to study. \$\endgroup\$ – Harsha Jayamanna Dec 14 '16 at 9:42

Not the answer you're looking for? Browse other questions tagged or ask your own question.