.NET Questions (CLOSED)

Questions and Answers on any aspect of .NET. Now closed.

This discussion group is now closed.

Have a question about .NET development? Try stackoverflow.com, a worldwide community of great developers asking and answering questions 24 hours a day.

The archives of .NET Questions contain years of Q&A. Even older .NET Questions are still online, too.

Singleton

OK, shame on me, I can't even do a singleton properly. I have implemented this a thousand dozen times, but I guess I've now been awake too long.

Here's the relevant snippet. When I compile, it rightly points out that the member _plain, in the method GetPlain(int, int) requires an object reference. But I cannot do a this._plain in that method too because it is a static method.

How on earth do I get out of this circle. I bet this is a trivial thing and I have done it countless times but just now my brain is revolting.


public class Plain: IRectangularGrid, IDisposable /*It is difficult to spell Plateau every time, so I am naming it Plain*/
    {
        private Plain _plain = null;    /*Singleton*/

        public static Plain GetPlain(POINT upperRight)
        {
            return GetPlain(upperRight.x, upperRight.y);
        }

        public static Plain GetPlain(int upperRightX, int upperRightY)
        {
            if (_plain == null)
                _plain = new Plain(upperRightX, upperRightY);

            return _plain;    
        }

        private Plain(int upperRightX, int upperRightY)
        {
                                      //some stuff
        }


My head is swirling round. Please tell me what I am missing.
Sathyaish Chakravarthy Send private email
Sunday, July 16, 2006
 
 
Sorry about the stupid question. I forgot to name my field _plain as static.
Sathyaish Chakravarthy Send private email
Sunday, July 16, 2006
 
 
You should probably do more thinking about the interface.  It looks as though the "upper right point" is only intended to be initialized once, yet you'll have to pass in parameters (or null) every time you call GetPlain, even though after the first call they'll have no effect.
Kyralessa Send private email
Sunday, July 16, 2006
 
 
I believe it's not a singleton you're looking for since you pass some parameters in the ctor. What happens the second time you request your Plateau (with different coordinates)?
smalltalk Send private email
Monday, July 17, 2006
 
 
Other than the two comments saying the parameters are meaning less.....

class Singleton
{
  private Singleton() {}

  private static Singleton _data = null;
  public static Singleton Get()
  {
    if ( _data == null ) _data = new Singleton();
    return _data;
  }
}

The data needs to be static as well. You also want the constructor to be private. You could use a static constructor as well and not do the test for null if you wanted.
Lorad
Monday, July 17, 2006
 
 
Try this MSDN article on for size:
 http://tinyurl.com/30wa
Green Eggs and Ham Send private email
Monday, July 17, 2006
 
 
> yet you'll have to pass in parameters (or null) every time you call GetPlain, even though after the first call they'll have no effect.

Wow! Wonderful thought. Although, not really essential (I mean the second time even if I passed null in the place of the UpperRight corner, nothing would go wrong and I would still get the old plain), but for the semantics would be wrong, so for that sake, it might do me good if I had a parameterless overload called GetPlain() without any arguments.

But then, again, I risk it being called the first time too, in which case I can always initialize the plain with a 1 square unit area by assuming that the upper right corner is also 0, 0 -- the same as the bottomLeft.


> What happens the second time you request your Plateau (with different coordinates)?

Nothing goes wrong. I still get the first/old/singleton plain. LOOK THERE.

        public static Plain GetPlain(POINT upperRight)
        {
            return GetPlain(upperRight.x, upperRight.y);
        }

        public static Plain GetPlain(int upperRightX, int upperRightY)
        {
            /* LOOK HERE*/
            if (_plain == null)
                _plain = new Plain(upperRightX, upperRightY);

            return _plain;    
        }

        private Plain(int upperRightX, int upperRightY)
        {
            //stuff happens here
        }


> You also want the constructor to be private.

The ctor is already private.

private Plain(int upperRightX, int upperRightY)
        {
            //stuff happens here
        }




Thanks for the good discussion. Much appreciated.
Sathyaish Chakravarthy Send private email
Tuesday, July 18, 2006
 
 
.NET 2.0 introduces static classes. From my experience, those more than suffice for most cases where a singleton is needed.

FWIW...

Donnie
soccerdad Send private email
Tuesday, July 18, 2006
 
 
".NET 2.0 introduces static classes."

You could have those in .NET 1.1 too; you just didn't have the static keyword on the class to enforce that all methods had to be static.  And unfortunately VB .NET still doesn't have corresponding Shared classes even in .NET 2.0.
Kyralessa Send private email
Tuesday, July 18, 2006
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz