当前位置: 技术问答>java相关
Think in Patterns问题求解一,设计模式达人入
来源: 互联网 发布时间:2017-03-25
本文导语: //: c01:SingletonPattern.java // The Singleton design pattern: you can // never instantiate more than one. // Since this isn't inherited from a Cloneable // base class and cloneability isn't added, // making it final prevents cloneability from // being...
//: c01:SingletonPattern.java
// The Singleton design pattern: you can
// never instantiate more than one.
// Since this isn't inherited from a Cloneable
// base class and cloneability isn't added,
// making it final prevents cloneability from
// being added through inheritance:
final class Singleton {
private static Singleton s = new Singleton(47);
private int i;
private Singleton(int x) { i = x; }
public static Singleton getReference() {
return s;
}
public int getValue() { return i; }
public void setValue(int x) { i = x; }
}
public class SingletonPattern {
public static void main(String[] args) {
Singleton s = Singleton.getReference();
System.out.println(s.getValue());
Singleton s2 = Singleton.getReference();
s2.setValue(9);
System.out.println(s.getValue());
try {
// Can't do this: compile-time error.
// Singleton s3 = (Singleton)s2.clone();
} catch(Exception e) {
e.printStackTrace(System.err);
}
}
} ///:~
SingletonPattern.java always creates an object, even if it’s never used. Modify this program to use lazy initialization, so the singleton object is only created the first time that it is needed.
// The Singleton design pattern: you can
// never instantiate more than one.
// Since this isn't inherited from a Cloneable
// base class and cloneability isn't added,
// making it final prevents cloneability from
// being added through inheritance:
final class Singleton {
private static Singleton s = new Singleton(47);
private int i;
private Singleton(int x) { i = x; }
public static Singleton getReference() {
return s;
}
public int getValue() { return i; }
public void setValue(int x) { i = x; }
}
public class SingletonPattern {
public static void main(String[] args) {
Singleton s = Singleton.getReference();
System.out.println(s.getValue());
Singleton s2 = Singleton.getReference();
s2.setValue(9);
System.out.println(s.getValue());
try {
// Can't do this: compile-time error.
// Singleton s3 = (Singleton)s2.clone();
} catch(Exception e) {
e.printStackTrace(System.err);
}
}
} ///:~
SingletonPattern.java always creates an object, even if it’s never used. Modify this program to use lazy initialization, so the singleton object is only created the first time that it is needed.
|
使用lazy initialization在第一次调用getReference()是创建对象
public static Singleton getReference() {
return s;
}
改为:
public static Singleton getReference() {
if(s==null){
s = new Singleton(47);
}
return s;
}
public static Singleton getReference() {
return s;
}
改为:
public static Singleton getReference() {
if(s==null){
s = new Singleton(47);
}
return s;
}
|
public class LazySingleton {
private static LazySingleton lazy = null;
private int i;
public LazySingleton() {
}
private LazySingleton(int x)
{
i=x;
}
public static LazySingleton getInstance()
{
if(lazy==null)
{
lazy = new LazySingleton(47);
}
return lazy;
}
public int getValue()
{
return i;
}
public void setValue(int x)
{
i = x;
}
}
private static LazySingleton lazy = null;
private int i;
public LazySingleton() {
}
private LazySingleton(int x)
{
i=x;
}
public static LazySingleton getInstance()
{
if(lazy==null)
{
lazy = new LazySingleton(47);
}
return lazy;
}
public int getValue()
{
return i;
}
public void setValue(int x)
{
i = x;
}
}
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。