singleton class VS static class



I've been reading a little about the difference between using a
singleton pattern vs using a static class (ie all methods are static,
ctor is private, state is in static vars).

A lot of blogs show that there are differences between these two
approaches. I can't understand most of them. Any help would be appreciated.


No worries, I can't understand most blogs either - mainly because most
blogs suck.

---

Assertion found on web: "The Singleton pattern is useful when the object
itself needs to keep some state. If no such thing is needed, a static
method can do the job as well."

My POV: No, you can put a state in a static var and access it through
static methods.


The singleton pattern is useful to ensure that 'only one instance of
that class' is ever created and available during the running of your
application. Its almost irrelevant that it holds member variables and
their state, as this is more of a value added capability.

The most common implementation of a singleton in Java uses a factory
method to return the instance.

class MySingleton {
private static MySingleton instance = new MySingleton();
//private constructor here....

public MySingleton getInstance() { return instance; }
}

This ensures that only one instance of MySingleton Class is avaiable
to the application at runtime.

Then these singletons then tend to be given member variables to store
that are needed through out the application. e.g. property file
values loaded into it, main frame of the UI, etc....


For this kind of ability, a singleton is a natural (though not
necessarily nice) choice to make.

A static method is only useful for utility methods (like the
getInstance factory method above) where they dont need to access
instance variables or maintain state.

Yes its true you could maintain state in static variables which static
methods call, but then you have basically create a class that can't be
used like any other class - polymorphically.

There is a middle ground though.

Its called the MonoState pattern. This is where you have a call with
only static member variables, all methods are still not static
methods. This means evey use of the class has to create an instance
of it, but every instance shares the same static member variable
state. This hybrid pattern allows the benefits of polymorphism to
continue, whilst allowing the system wide state of the member
variables. Its useful if you are doing a lot of JUnit tests, where you
need to override the monostate behaviour for your tests.

That said...the whole issue is moot... Basically if you need only one
instance of the class... then just create one, not two.

The biggest problem with Singletons, Static methods, Monostates etc...
is that people are lazy. They can't think how to designt he system so
that the dont have to pass around all over the place, the single
instance of a class they created, so they go with a singleton, not
because they actually need a singleton...just because its easier. Its
less coding and requires less thinking.


---

Flexibility of the factory mehod: You can simply create two subclasses,
MyDevelopmentSingleton and MyProductionSingleton, and let your
MySingleton.getinstance() function decide which to return.

My POV: If you declare you ctor private to enforce the singleton
pattern, you cannot subclass it. If you declare the ctor protected, is
it still a true singleton? Users can then create more than one object...

---

Assertion found on web: The main difference between static and Singleton
classes is that static classes don't ever get initialised.

My POV: I don't understand. What about static initializers?

---

So, in the end it's not clear for me why one should prefer the Singleton
pattern over the static class (should I?). I mean

SingletonClass.getInstance().setSomething(int a);
SingletonClass.getInstance().getSomething();

is quite quivalent to

StaticClass.setSomething(int a);
StaticClass.getSomething();

Any comments are welcome. Best regards
Phil


Best regards

Andrew

转载于:https://www.cnblogs.com/kamome/archive/2009/10/13/1582497.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一个类被称为单例类,意味着该类的实例只能被创建一次。在给定的代码中,class singleton声明了一个私有静态singleton对象,并在对象的声明处进行了实例化。 通过将构造函数设置为私有,限制了对该类的实例化,并通过创建一个静态的方法或者属性来提供获取该类实例的途径。 这种单例模式的实现方式被称为饿汉式单例模式,因为在类加载的时候就已经初始化了唯一的实例。这样可以确保在多线程环境下对实例的访问是安全的。 由于构造函数被设置为私有,所以外部的代码无法直接创建类的实例。相反,我们可以通过使用类的静态方法来获取实例,例如singleton.getInstace()。 这种单例模式的优点是简单直接,且线程安全。而且由于在类加载的时候就对实例进行初始化,所以在使用时可以提高访问速度。 然而,饿汉式单例模式也存在一些缺点。首先,由于实例在类加载时就被初始化,所以如果类的实例并没有被使用,那么就会造成资源的浪费。其次,由于实例的创建是在类加载时进行的,所以当类的实例化过程比较耗时或者需要获取外部资源时,可能会对程序的性能产生一定的影响。 总而言之,单例模式可以用于需要全局唯一实例的情况,饿汉式单例模式在多线程环境下线程安全,并且实现简单。但对于资源消耗较大的情况,需要考虑是否使用延迟加载的方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值