public class Lazy Singleton
private static LazySingleton newInstance = null;
if (newInstance == null)
newInstance = new LazySingleton ();
}
return newInstance;
}
singleton 限制了实例个数,有利于gc的回收。
实例一:浅拷贝
public class Prototype implements Cloneable {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Object clone() {
try {
return super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
return null;
}
}
}
public class TestMain {
public static void main(String[] args) {
testPrototype();
}
private static void testPrototype(){
Prototype pro = new Prototype();
pro.setName("original object");
Prototype pro1 = (Prototype)pro.clone();
pro.setName("changed object1");
System.out.println("original object:" + pro.getName());
System.out.println("cloned object:" + pro1.getName());
}
}
结果:
original object:changed object1
cloned object:original object
public class ModuleA { // 示意方法 public void testA(){ System.out.println( "调用ModuleA中的testA方法" ); } }
public class ModuleB { // 示意方法 public void testB(){ System.out.println( "调用ModuleB中的testB方法" ); } }
public class ModuleC { // 示意方法 public void testC(){ System.out.println( "调用ModuleC中的testC方法" ); } }
门面角色类:
public class Facade { // 示意方法,满足客户端需要的功能 public void test(){ ModuleA a = new ModuleA(); a.testA(); ModuleB b = new ModuleB(); b.testB(); ModuleC c = new ModuleC(); c.testC(); } }