Question

“Singleton design pattern provides stricter control over global variables.” Justify this statement with your own logic....

“Singleton design pattern provides stricter control over global
variables.”

Justify this statement with your own logic.
Compare Singleton design pattern and global variable (similarities and differences)
Implement any example (in JAVA), representing concept of global variable and Singleton design
pattern, highlight the differences with coding and output.

Homework Answers

Answer #1

Singleton design pattern - it restricts the instantiation of a class to only one instance. It will be useful if only one object is needed across the system.for this class only one entry point will be created using an accessor method ,like getInstance().

“Singleton design pattern provides stricter control over global variables.” : Singleton design pattern avoids polluting the namespace with global variables which are storing only singleton instances.

For small applications global variables are good because it helps the code to easily pass and access data from other classes and methods

In Large applications it destroys the idea of encapsulatoin in OOP.it will be hard to maintain and read in these kind of applications.

Singleton Classes can not be sub-classed and they provide a lazy way to create an instance .They deviate from single responsibility principle because it has the responsibilty of creating its own instance and other business responsibilities

Using global variable:

class Global {
  static String variable = "Global Value";
}

class A {
  A() {
    System.out.println("A: "+Global.variable);
    Global.variable += " & something else";
} }
class B {
  B() {
    System.out.println("B: "+Global.variable);
} }

class Main {  
  public static void main(String[] args) {  
    A o1 = new A();
    B o2 = new B();
} }

/*
Output:
A: Globallobal Value
B: Globallobal Value & something else
*/

using Singleton design pattern:

public class Global {

        private static Global obj; 
        String variable = "Global Value";
        
    private Global() {} 
  
    public static Global getInstance() 
    { 
        if (obj==null) 
            obj = new Global(); 
        return obj; 
    } 
}


public class A {
        A() {
            Global g = Global.getInstance();
            System.out.println(g.variable);
            g.variable=g.variable+" & something";
        }

}

public class B {

        B() {
            Global g = Global.getInstance();
            System.out.println(g.variable);
        }
}

public class Main {
        public static void main(String[] args) {  
            A o1 = new A();
            B o2 = new B();
        }

}

/*
Output:
Global Value
Global Value & something
*/

in this small application global variables are preferred but in large application global variables pollute global namespace with unnecessary variables.both provides global access but we have choose wisely based on the resources.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions