
本站地址:http://www.bajiao123.com

设计模式---Singleton
单态的作用就不用说了,经常用过,就是一个类只产生一个对象。
它有两个种实现方法。实现在大同小异,可以根据需要改进。
例程如下:
package com.pdw.pattern;
class SingletonA{
?private SingletonA(){
??
?}
?private static SingletonA _instance=new SingletonA();
?public static SingletonA getInstance(){
??return _instance;
?}
}
class SingletonB{
?private static SingletonB _instance;
?public static SingletonB getInstance(){
??if(_instance==null){
???return new SingletonB();
??}else{
???return _instance;
??}
??
?}
}
public class SingletonImpl {
?/**
? * @param args
? */
?public static void main(String[] args) {
??// TODO Auto-generated method stub
?}
}
本站地址:http://www.bajiao123.com

