0

The greatness of Singleton pattern is: Initialize the object (instance) only once. Then we can interact with non-static members. But what more powerful about Singleton pattern is the Constructor.

If we dont need a constructor that initialize objects, then the singleton pattern is meaningless and we can use static class.

But what if we do need to get parameters into the constructor?

The constructor has one role with two options: one to initialize objects to their initial values and the second is to initialize objecs to the values it gets via parameters, so can we say that singleton pattern omits the second one?

Otherwise, what are the diffrences between singleton pattern and static class?

Thanks, Jacob

Jacob
  • 3,598
  • 4
  • 35
  • 56
  • 1
    (That duplicate is for the "static class" part.) Fundamentally, if a singleton needs parameters, you've got problems - you should consider a factory class instead. (What happens if the second call to `GetInstance` or whatever uses different values?) – Jon Skeet Sep 26 '13 at 07:20
  • After you decide what to do, here is a page with versions for singleton implementation: http://www.yoda.arachsys.com/csharp/singleton.html – ilansch Sep 26 '13 at 07:22

3 Answers3

1

"Singleton pattern is: Initialize the object (instance) only once", so you can do something like this:

public class MyClass   {

    MyClass me  = null;

    //private CTOR 
    private MyClass  () {
    }

    public static MyClass  ConstructMe(..parameters..) {

         if(me != null) 
            return me;

         me = new MyClass();
         ....setup parameters .... 
         return me,
    }

}

You pass parameters, and you can have only one instance.

If this is not what you're asking for, please clarify.

Tigran
  • 61,654
  • 8
  • 86
  • 123
1

1) There is nothing stopping you implementing an initialiser in the singleton pattern that takes in arguments, as long as it will ALWAYS require those arguments. pseudo code:

class single{
    private static single instance;
    private single(){
        default args;
    }
    private single(arg1, arg2){

    }
    public static init(){
        if(instance == null){
           instance = new single();
        }
        return instance;
    }
    public static init(arg1, arg2){
        if(instance == null){
           instance = new single(arg1, arg2);
        }
        else{
           instance.setArg1(arg1);
           instance.setArg2(arg2);
        }
        return instance;
    }
}

2) A singleton class can be extended, and can extend other classes - giving the full power of OOP, a static class is just a structured way of organising functions.

Zack Newsham
  • 2,810
  • 1
  • 23
  • 43
0

If you need parameters in your singleton's constructor you have a design flaw IMO.

You can either use setters to initialize your members after instantiation or pass the needed instances as parameters of your member functions.

public class MyClass {

    static MyClass single = null;
    MyMember member;

    private MyClass() {

    }

    public static MyClass getInstance() {
        if(single == null)
            single = new MyClass();
        return single;

    }

    //------------------------------------------------------------
    // First approach using setter
    public void setMember(MyMember member) {
        this.member = member;
    }

    public void doSomething() {
        member.foo();
        // do something
    }

    //------------------------------------------------------------
    // Second approach pass by parameter
    public void doSomething(MyMember param) {
        param.foo();
        // do something
    }
}
Sambuca
  • 1,224
  • 18
  • 30