- 1.
模版方法设计模式 -
在定义功能时,功能的一部分是确定的,一部分是不确定的。而确定的部分在使用不确定的部分,那么这时就将不确定的部分暴露出去。由该类的子类去完成。 - public
abstract class GeTime { -
public final void getTime(){ -
long start=System.currentTimeMillis(); -
runCode(); -
long end=System.currentTimeMillis(); -
//... -
} -
public abstract void runCode(); - }
- 2.
单例设计模式 - //饿汉式
- class
Single { -
private static Single s = new Single(); -
private Single() { -
} -
public static Single getInstance() { -
return s; -
} - }
- //懒汉式
-- 延迟加载 -- 线程安全问题 - public
class Single { -
private static Single s = null; -
private Single() { -
} -
public static Single getInstance() { -
if (s == null) { -
s = new Single(); -
} -
return s; -
} - }
- //线程安全的懒汉式
-- 双重判断 - public
class Single { -
private static Single s = null; -
private Single() { -
} -
public static Single getInstance() { -
if(s==null){ -
synchronized(Single.class){ -
if (s == null) { -
s = new Single(); -
} -
} -
} -
return s; -
} - }