设计模式是软件工程领域中非常关键的主题。每个人都想越来越深入地学习它们。有许多文献可用,如GOF书等。我们可以在书中找到大量关于概念解释和设计模式示例的例子,但我想写下关于模式定义及其标准用法在JDK源码中的体现。
单例模式
确保只创建一个类的实例并为该对象提供全局访问点。
例如:
- java.lang.Runtime#getRuntime()
private static Runtime currentRuntime = new Runtime();
public static Runtime getRuntime() {
return currentRuntime;
}
复制代码
- java.awt.Desktop#getDesktop()
/**
* Returns the <code>Desktop</code> instance of the current
* browser context. On some platforms the Desktop API may not be
* supported; use the {@link #isDesktopSupported} method to
* determine if the current desktop is supported.
* @return the Desktop instance of the current browser context
* @throws HeadlessException if {@link
* GraphicsEnvironment#isHeadless()} returns {@code true}
* @throws UnsupportedOperationException if this class is not
* supported on the current platform
* @see #isDesktopSupported()
* @see java.awt.GraphicsEnvironment#isHeadless
*/
public static synchronized Desktop getDesktop(){
if (GraphicsEnvironment.isHeadless()) throw new HeadlessException();
if (!Desktop.isDesktopSupported()) {
throw new UnsupportedOperationException("Desktop API is not " +
"supported on the current platform");
}
sun.awt.AppContext context = sun.awt.AppContext.getAppContext();
Desktop desktop = (Desktop)context.get(Desktop.class);
if (desktop == null) {
desktop = new Desktop();
context.put(Desktop.class, desktop);
}
return desktop;
}
复制代码
工厂模式
创建对象而不将实例化逻辑暴露给客户端,并通过公共接口引用新创建的对象。
例如:
- java.lang.Object#toString() (在所有子类中重写)
- java.lang.Class#newInstance()
- java.lang.Integer#valueOf(String)
- java.lang.Class#forName()
- java.lang.reflect.Array#newInstance()
- java.lang.reflect.Constructor#newInstance()
抽象工厂
提供用于创建一系列相关对象的接口,而无需显式指定其类。 我们可以简单地说它是工厂模式的更高层次的抽象
例如:
- java.util.Calendar#getInstance()
- java.util.Arrays#asList()
- java.util.ResourceBundle#getBundle()
- java.net.URL#openConnection()
- java.sql.DriverManager#getConnection()
- java.sql.Connection#createStatement()
- java.sql.Statement#executeQuery()
- java.text.NumberFormat#getInstance()
- java.lang.management.ManagementFactory (所有getXXX()方法)
- java.nio.charset.Charset#forName()
- javax.xml.parsers.DocumentBuilderFactory#newInstance()
- javax.xml.transform.TransformerFactory#newInstance()
- javax.xml.xpath.XPathFactory#newInstance()
观察者模式
定义对象之间的一对多依赖关系,以便当一个对象更改状态时,将自动通知和更新其所有依赖项。
例如:
- java.util.Observer/java.util.Observable
- java.util.EventListener的所有实现类
- javax.servlet.http.HttpSessionBindingListener
- javax.servlet.http.HttpSessionAttributeListener
- javax.faces.event.PhaseListener
构造者模式
定义用于创建对象的实例,但让子类决定实例化哪个类并允许更好地控制构造过程。我们可以说最简单的构建器与工厂模式类似,Builder允许提供创建对象的选项。
例如:
- java.lang.StringBuilder#append() (unsynchronized)
- java.lang.StringBuffer#append() (synchronized)
- java.nio.ByteBuffer#put() (另外还有CharBuffer, ShortBuffer, IntBuffer, LongBuffer, FloatBuffer 和 DoubleBuffer)
- javax.swing.GroupLayout.Group#addComponent()
- java.lang.Appendable 的实现类
原型模型
使用原型实例指定要创建的对象类型,并通过复制此原型来创建新对象。
例如:
- java.lang.Object#clone() (类必须实现java.lang.Cloneable)
责任链模式
它避免了将请求的发送者附加到其接收者,这使得其他对象也可以处理请求。
对象成为链的一部分,请求从一个对象发送到另一个对象,直到其中一个对象将处理它。
例如:
- java.util.logging.Logger#log()
- javax.servlet.Filter#doFilter()