JDK中运用的设计模式
JDK中使用的设计模式
1.抽象工厂模式
实例1:
public static Calendar getInstance()
{
Calendar cal = createCalendar(TimeZone.getDefaultRef(),Locale.getDefault());
cal.sharedZone = true;
return cal;
}
Calendar类将构造方法进行私有话,这样获取对象实例只能通过getInstance()方法了。每次访问该方法返回的是一个新生成的对象。
有意思的是Locale.getDefault()也是一个利用了工厂模式,而且是将单例模式和工厂模式的结构来生成一个单例对象。
实例2:
public static Connection getConnection(String url,
String user, String password) throws SQLException {
java.util.Properties info = new java.util.Properties();
// Gets the classloader of the code that called this method, may
// be null.
ClassLoader callerCL = DriverManager.getCallerClassLoader();
if (user != null) {
info.put("user", user);
}
if (password != null) {
info.put("password", password);
}
return (getConnection(url, info, callerCL));
}
使用JDBC的人都调用过该方法。该方法也是典型工厂模式。
因为该方法是对外的入口,所以将该方法生命为public的。虽然该类拥有很多构造方法,但是最后返回实例的都是依靠getConnection(url, info, callerCL)方法,因为他会将传入的参数都封装到properties对象中。最后再调用该方法。做到了很好的封装与代码复用。而且getConnection(url, info, callerCL)也被设计成是是有的方法,就能保住该方法只给其余构造方法进行调用了。
2.单例模式
实例:
private static Runtime currentRuntime = new Runtime();
public static Runtime getRuntime() {
return currentRuntime;
}
Runtime是一个典型的单例模式,而且是懒汉式的单例模式。
3.模版模式
实例:
OutputStream类的
public abstract void write(int b) throws IOException;
OutputStream的写操作交给了子类去实现,例如FileOutputStream类会调用native的write方法。
public native void write(int b) throws IOException;注意native方法没有实现体。
4.策略模式
TreeMap类的
public TreeMap(Comparator<? super K> comparator) {
this.comparator = comparator;
}
comparator类是一个接口,里面只包含比较两个对象的方法。
treemap会根据传入的自定义比较器的compare方法判断插入的值应该存放的位置。
即传入的参数对象可能有不同的实现类
5.命令模式
Runnable接口
Runnable是一个典型的命令模式,即每个线程都需要执行不同的操作,因此定义一个命令接口,即Runnable接口。然后使用时需要将Runnable对象传入给Thread类中,Thread就相当于一个调用者(机箱),然后用户对thread对像进行调用就可以执行runnable中定义的操作了。
1.抽象工厂模式
实例1:
public static Calendar getInstance()
{
Calendar cal = createCalendar(TimeZone.getDefaultRef(),Locale.getDefault());
cal.sharedZone = true;
return cal;
}
Calendar类将构造方法进行私有话,这样获取对象实例只能通过getInstance()方法了。每次访问该方法返回的是一个新生成的对象。
有意思的是Locale.getDefault()也是一个利用了工厂模式,而且是将单例模式和工厂模式的结构来生成一个单例对象。
实例2:
public static Connection getConnection(String url,
String user, String password) throws SQLException {
java.util.Properties info = new java.util.Properties();
// Gets the classloader of the code that called this method, may
// be null.
ClassLoader callerCL = DriverManager.getCallerClassLoader();
if (user != null) {
info.put("user", user);
}
if (password != null) {
info.put("password", password);
}
return (getConnection(url, info, callerCL));
}
使用JDBC的人都调用过该方法。该方法也是典型工厂模式。
因为该方法是对外的入口,所以将该方法生命为public的。虽然该类拥有很多构造方法,但是最后返回实例的都是依靠getConnection(url, info, callerCL)方法,因为他会将传入的参数都封装到properties对象中。最后再调用该方法。做到了很好的封装与代码复用。而且getConnection(url, info, callerCL)也被设计成是是有的方法,就能保住该方法只给其余构造方法进行调用了。
2.单例模式
实例:
private static Runtime currentRuntime = new Runtime();
public static Runtime getRuntime() {
return currentRuntime;
}
Runtime是一个典型的单例模式,而且是懒汉式的单例模式。
3.模版模式
实例:
OutputStream类的
public abstract void write(int b) throws IOException;
OutputStream的写操作交给了子类去实现,例如FileOutputStream类会调用native的write方法。
public native void write(int b) throws IOException;注意native方法没有实现体。
4.策略模式
TreeMap类的
public TreeMap(Comparator<? super K> comparator) {
this.comparator = comparator;
}
comparator类是一个接口,里面只包含比较两个对象的方法。
treemap会根据传入的自定义比较器的compare方法判断插入的值应该存放的位置。
即传入的参数对象可能有不同的实现类
5.命令模式
Runnable接口
Runnable是一个典型的命令模式,即每个线程都需要执行不同的操作,因此定义一个命令接口,即Runnable接口。然后使用时需要将Runnable对象传入给Thread类中,Thread就相当于一个调用者(机箱),然后用户对thread对像进行调用就可以执行runnable中定义的操作了。