Creational patterns
Abstract factory (recognizeable by creational methods returning an abstract/interface type)
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(allgetXXX()methods)java.nio.charset.Charset#forName()javax.xml.parsers.DocumentBuilderFactory#newInstance()javax.xml.transform.TransformerFactory#newInstance()javax.xml.xpath.XPathFactory#newInstance()java.net.URLStreamHandlerFactory#createURLStreamHandler(String)(Returns singleton object per protocol)
Builder (recognizeable by creational methods returning the instance itself)
java.lang.StringBuilder#append()(unsynchronized)java.lang.StringBuffer#append()(synchronized)java.nio.ByteBuffer#put()(also onCharBuffer,ShortBuffer,IntBuffer,LongBuffer,FloatBufferandDoubleBuffer)javax.swing.GroupLayout.Group#addComponent()- All implementations of
java.lang.Appendable
Factory method (recognizeable by creational methods returning a concrete type)
java.lang.Object#toString()(overrideable in all subclasses)java.lang.Class#newInstance()java.lang.Integer#valueOf(String)(also onBoolean,Byte,Character,Short,Long,FloatandDouble)java.lang.Class#forName()java.lang.reflect.Array#newInstance()java.lang.reflect.Constructor#newInstance()
Prototype (recognizeable by creational methods returning a different instance of itself with the same properties)
Singleton (recognizeable by creational methods returning the same instance (usually of itself) everytime)
Structural patterns
Adapter (recognizeable by creational methods taking an instance of different abstract/interface type and returning an implementation of own/another abstract/interface type which decorates/overrides the given instance)
java.io.InputStreamReader(InputStream)(returns aReader)java.io.OutputStreamWriter(OutputStream)(returns aWriter)javax.xml.bind.annotation.adapters.XmlAdapter#marshal()and#unmarshal()
Bridge (recognizeable by creational methods taking an instance of different abstract/interface type and returning an implementation of own abstract/interface type which delegates/uses the given instance)
- None comes to mind yet. A fictive example would be
new LinkedHashMap(LinkedHashSet<K>, List<V>)which returns an unmodifiable linked map which doesn't clone the items, but uses them. Thejava.util.Collections#newSetFromMap()andsingletonXXX()methods however comes close.
Composite (recognizeable by behavioral methods taking an instance of same abstract/interface type)
java.util.Map#putAll(Map)java.util.List#addAll(Collection)java.util.Set#addAll(Collection)java.nio.ByteBuffer#put(ByteBuffer)(also onCharBuffer,ShortBuffer,IntBuffer,LongBuffer,FloatBufferandDoubleBuffer)java.awt.Container#add(Component)(practically all over Swing thus)
Decorator (recognizeable by creational methods taking an instance of same abstract/interface type)
- All subclasses of
java.io.InputStream,OutputStream,ReaderandWriterhave a constructor taking an instance of same type. - Almost all implementations of
java.util.List,SetandMaphave a constructor taking an instance of same type. java.util.Collections, thecheckedXXX(),synchronizedXXX()andunmodifiableXXX()methods.javax.servlet.http.HttpServletRequestWrapperandHttpServletResponseWrapper
Facade (recognizeable by behavioral methods which internally uses instances of different independent abstract/interface types)
javax.faces.context.FacesContext, it internally uses among others the abstract/interface typesLifeCycle,ViewHandler,NavigationHandlerand many more without that the enduser has to worry about it (which are however overrideable by injection).javax.faces.context.ExternalContext, which internally usesServletContext,HttpSession,HttpServletRequest,HttpServletResponse, etc.
Flyweight (recognizeable by creational methods returning a cached instance, a bit the "multiton" idea)
Proxy (recognizeable by creational methods which returns an implementation of given abstract/interface type which in turn delegates/uses a different implementation of given abstract/interface type)
The Wikipedia example is IMHO a bit poor, lazy loading has actually completely nothing to do with the proxy pattern at all.
Behavioral patterns
Chain of responsibility (recognizeable by behavioral methods which (indirectly) invokes the same method inanother implementation of same abstract/interface type in a queue)
Command (recognizeable by behavioral methods in an abstract/interface type which invokes a method in an implementation of a different abstract/interface type which has been encapsulated by the command implementation during its creation)
- All implementations of
java.lang.Runnable - All implementations of
javax.swing.Action
Interpreter (recognizeable by behavioral methods returning a structurally different instance/type of the given instance/type; note that parsing/formatting is not part of the pattern, determining the pattern and how to apply it is)
java.util.Patternjava.text.Normalizer- All subclasses of
java.text.Format - All subclasses of
javax.el.ELResolver
Iterator (recognizeable by behavioral methods sequentially returning instances of a different type from a queue)
- All implementations of
java.util.Iterator(thus among others alsojava.util.Scanner!). - All implementations of
java.util.Enumeration
Mediator (recognizeable by behavioral methods taking an instance of different abstract/interface type (usually using the command pattern) which delegates/uses the given instance)
java.util.Timer(allscheduleXXX()methods)java.util.concurrent.Executor#execute()java.util.concurrent.ExecutorService(theinvokeXXX()andsubmit()methods)java.util.concurrent.ScheduledExecutorService(allscheduleXXX()methods)java.lang.reflect.Method#invoke()
Memento (recognizeable by behavioral methods which internally changes the state of the whole instance)
java.util.Date(the setter methods do that,Dateis internally represented by alongvalue)- All implementations of
java.io.Serializable - All implementations of
javax.faces.component.StateHolder
Observer (or Publish/Subscribe) (recognizeable by behavioral methods which invokes a method on an instance of another abstract/interface type, depending on own state)
java.util.Observer/java.util.Observable(rarely used in real world though)- All implementations of
java.util.EventListener(practically all over Swing thus) javax.servlet.http.HttpSessionBindingListenerjavax.servlet.http.HttpSessionAttributeListenerjavax.faces.event.PhaseListener
State (recognizeable by behavioral methods which changes its behaviour depending on the instance's state which can be controlled externally)
- All implementations of
java.util.Iterator javax.faces.lifecycle.LifeCycle#execute()(controlled byFacesServlet, the behaviour is dependent on current phase (state) of JSF lifecycle)
Strategy (recognizeable by behavioral methods in an abstract/interface type which invokes a method in an implementation of a different abstract/interface type which has been passed-in as method argument into the strategy implementation)
java.util.Comparator#compare(), executed by among othersCollections#sort().javax.servlet.http.HttpServlet, theservice()and alldoXXX()methods takeHttpServletRequestandHttpServletResponseand the implementor has to process them (and not to get hold of them as instance variables!).javax.servlet.Filter#doFilter()
Template method (recognizeable by behavioral methods which already have a "default" behaviour definied by an abstract type)
- All non-abstract methods of
java.io.InputStream,java.io.OutputStream,java.io.Readerandjava.io.Writer. - All non-abstract methods of
java.util.AbstractList,java.util.AbstractSetandjava.util.AbstractMap. javax.servlet.http.HttpServlet, all thedoXXX()methods by default sends a HTTP 405 "Method Not Allowed" error to the response. You're free to implement none or any of them.
Visitor (recognizeable by two different abstract/interface types which has methods definied which takes each theother abstract/interface type; the one actually calls the method of the other and the other executes the desired strategy on it)
javax.lang.model.element.AnnotationValueandAnnotationValueVisitorjavax.lang.model.element.ElementandElementVisitorjavax.lang.model.type.TypeMirrorandTypeVisitor
---------------------------------------------------------------------------------------------------------------------------------------------------------
下面是JDK中有关23个经典设计模式的示例,在stakeoverflow也有相应的讨论:
http://stackoverflow.com/questions/1673841/examples-of-gof-design-patterns
Structural(结构模式)
把一个接口或是类变成另外一种。
- java.util.Arrays#asList()
- javax.swing.JTable(TableModel)
- java.io.InputStreamReader(InputStream)
- java.io.OutputStreamWriter(OutputStream)
- javax.xml.bind.annotation.adapters.XmlAdapter#marshal()
- javax.xml.bind.annotation.adapters.XmlAdapter#unmarshal()
Bridge:
把抽象和实现解藕,于是接口和实现可在完全独立开来。
- AWT (提供了抽象层映射于实际的操作系统)
- JDBC
Composite:
让使用者把单独的对象和组合对象混用。
- javax.swing.JComponent#add(Component)
- java.awt.Container#add(Component)
- java.util.Map#putAll(Map)
- java.util.List#addAll(Collection)
- java.util.Set#addAll(Collection)
Decorator:
为一个对象动态的加上一系列的动作,而不需要因为这些动作的不同而产生大量的继承类。这个模式在JDK中几乎无处不在,所以,下面的列表只是一些典型的。
- java.io.BufferedInputStream(InputStream)
- java.io.DataInputStream(InputStream)
- java.io.BufferedOutputStream(OutputStream)
- java.util.zip.ZipOutputStream(OutputStream)
- java.util.Collections#checked[List|Map|Set|SortedSet|SortedMap]()
Facade:
用一个简单的接口包状一组组件,接口,抽象或是子系统。
- java.lang.Class
- javax.faces.webapp.FacesServlet
Flyweight:
有效率地存储大量的小的对象。
- java.lang.Integer#valueOf(int)
- java.lang.Boolean#valueOf(boolean)
- java.lang.Byte#valueOf(byte)
- java.lang.Character#valueOf(char)
Proxy:
用一个简单的对象来代替一个复杂的对象。
- java.lang.reflect.Proxy
- RMI
Creational(创建模式)
创建一组有关联的对象实例。这个模式在JDK中也是相当的常见,还有很多的framework例如Spring。我们很容易找到这样的实例。
- java.util.Calendar#getInstance()
- java.util.Arrays#asList()
- java.util.ResourceBundle#getBundle()
- java.sql.DriverManager#getConnection()
- java.sql.Connection#createStatement()
- java.sql.Statement#executeQuery()
- java.text.NumberFormat#getInstance()
- javax.xml.transform.TransformerFactory#newInstance()
Builder:
主要用来简化一个复杂的对象的创建。这个模式也可以用来实现一个 Fluent Interface。
- java.lang.StringBuilder#append()
- java.lang.StringBuffer#append()
- java.sql.PreparedStatement
- javax.swing.GroupLayout.Group#addComponent()
Factory:
简单来说,按照需求返回一个类型的实例。
- java.lang.Proxy#newProxyInstance()
- java.lang.Object#toString()
- java.lang.Class#newInstance()
- java.lang.reflect.Array#newInstance()
- java.lang.reflect.Constructor#newInstance()
- java.lang.Boolean#valueOf(String)
- java.lang.Class#forName()
Prototype:
使用自己的实例创建另一个实例。有时候,创建一个实例然后再把已有实例的值拷贝过去,是一个很复杂的动作。所以,使用这个模式可以避免这样的复杂性。
- java.lang.Object#clone()
- java.lang.Cloneable
Singleton:
只允许一个实例。在 Effective Java中建议使用Emun.
- java.lang.Runtime#getRuntime()
- java.awt.Toolkit#getDefaultToolkit()
- java.awt.GraphicsEnvironment#getLocalGraphicsEnvironment()
- java.awt.Desktop#getDesktop()
Behavioral(行为模式)
Chain of responsibility:
把一个对象在一个链接传递直到被处理。在这个链上的所有的对象有相同的接口(抽象类)但却有不同的实现。
- java.util.logging.Logger#log()
- javax.servlet.Filter#doFilter()
Command:
把一个或一些命令封装到一个对象中。
- java.lang.Runnable
- javax.swing.Action
Interpreter:
一个语法解释器的模式。
- java.util.Pattern
- java.text.Normalizer
- java.text.Format
Iterator:
提供一种一致的方法来顺序遍历一个容器中的所有元素。
- java.util.Iterator
- java.util.Enumeration
Mediator:
用来减少对象单的直接通讯的依赖关系。使用一个中间类来管理消息的方向。
- java.util.Timer
- java.util.concurrent.Executor#execute()
- java.util.concurrent.ExecutorService#submit()
- java.lang.reflect.Method#invoke()
Memento:
给一个对象的状态做一个快照。Date类在内部使用了一个long型来做这个快照。
- java.util.Date
- java.io.Serializable
Null Object:
这个模式用来解决如果一个Collection中没有元素的情况。
- java.util.Collections#emptyList()
- java.util.Collections#emptyMap()
- java.util.Collections#emptySet()
Observer:
允许一个对象向所有的侦听的对象广播自己的消息或事件。
- java.util.EventListener
- javax.servlet.http.HttpSessionBindingListener
- javax.servlet.http.HttpSessionAttributeListener
- javax.faces.event.PhaseListener
State:
这个模式允许你可以在运行时很容易地根据自身内部的状态改变对象的行为。
- java.util.Iterator
- javax.faces.lifecycle.LifeCycle#execute()
Strategy:
定义一组算法,并把其封装到一个对象中。然后在运行时,可以灵活的使用其中的一个算法。
- java.util.Comparator#compare()
- javax.servlet.http.HttpServlet
- javax.servlet.Filter#doFilter()
Template method:
允许子类重载部分父类而不需要完全重写。
- java.util.Collections#sort()
- java.io.InputStream#skip()
- java.io.InputStream#read()
- java.util.AbstractList#indexOf()
Visitor:
作用于某个对象群中各个对象的操作. 它可以使你在不改变这些对象本身的情况下,定义作用于这些对象的新操作.
- javax.lang.model.element.Element 和javax.lang.model.element.ElementVisitor
- javax.lang.model.type.TypeMirror 和javax.lang.model.type.TypeVisitor
JDK设计模式示例
本文列举了JDK中各种设计模式的应用实例,包括创建型、结构型和行为型模式,如单例模式的java.lang.Runtime#getRuntime(),适配器模式的java.io.InputStreamReader(InputStream),以及迭代器模式的java.util.Iterator等。
6930

被折叠的 条评论
为什么被折叠?



