开发者

What is the difference between static class and singleton class desgin pattern? [duplicate]

开发者 https://www.devze.com 2023-03-19 23:19 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: Difference between static class and singleton pattern?
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Difference between static class and singleton pattern?

i am not able to understabnd the difference b/w static class and singleton class.

in single ton class we make sure we cretae only one object and no more objects are created.

in a the static class also there is no need to create an object we can call the properties and methods directly using the static class name.

here both looks same s开发者_StackOverflow中文版o whats teh use of using creating single ton class.

any help on this would be great.


In Static class, there is no object. You directly call methods on the static class.

In Singleton, there is an object however, there can only be one instance of it.

Singleton is useful in conditional creation a resources intensive object. For ex, your application might need a connection to remote database. You might want to make it as singleton to limit the number of connections and also to ensure that its only created when required.

Static class and methods are more like utility functions which can be called whenever required.


The difference, obviously, is that on the one hand, you're working in a static context, and on the other, you're dealing with a normal object instance. I'd say the primary consequence of this is that because static members aren't inherited, a static class can't benefit from inheritance or polymorphism, where a singleton can. By working in a static context, you're losing a lot of the object-orientedness of Java.


With a Singleton, we are assured that only one instance of a class (object) is created. This is highly useful when the object is expensive to create, and there's only ever a need for one.

A static class on the other hand, can have no instances of the class created. This is appropriate when methods don't below to a given object and, instead, act on existing objects.


First let's see an example to explain static/instance (code is in as3 but same principle in any languages)

class Blob {
   private var i:int=0;

   public function Blob() {
      i++;
      trace("I value : " + i);
   }
}
new Blob();
new Blob();

------
output
I 1
I 1

you have 2 instances of your Blob class and i var is create each time.

class StaticBlob {
  private static var i:int =0;

  public function StaticBlob() {
    i++;
    trace("I value : " + i);
  }
}
new StaticBlob();
new StaticBlob();

--------
output

I 1
I 2

you have 2 instances of your StaticBlob class too but i var is only create once and "keep in memory" for all the instance

Now it's more easy to understand Singleton. Singleton garantees you to have ONLY ONE INSTANCE of a class (because it uses a static property to keep the reference of your instance and return it). So it can be use to instanciate one time an object (for example if your object consume too much resource to create)


A singleton class is very different from a static class in that a singleton class can contain static and non static members/properties/methods.

So a singleton object can persist information/state like a normal non static object. Most importantly a singleton is designed to be Thread safe, that is it has a sync root object (generally) that can be used to block threads that are trying to do the same thing.

A static class can only contain static members/properties/members because of this it is impossible to maintain thread safety you cannot lock a static object.

0

精彩评论

暂无评论...
验证码 换一张
取 消