最近在看JDK源码,想在毕业前再好好提高一下写代码的能力,JDK是个优秀的源码阅读范本(Spring的源码也不错)。JDK目录下的src.zip里可以直接获得源码,我也push到了我Github的一个repo里。
网上搜了JDK设计模式,coolshell里也有一篇,不过我还是参照了Stackoverflow(原文链接)上的一个“Examples of GoF Design Patterns”的答复,详细列举了JDK里具体对应的类和函数,并结合了wiki上的许多整理好的优秀词条内容。放到这个博客上,也方便自己深入学习,好好体会下设计模式内涵,光看书也会很枯燥,况且纸质的代码看起来也不舒服。
You can find an overview of a lot of design patterns inWikipedia. It also mentions which patterns are mentioned by GoF. I'll sum them up here and try to assign as much as possible pattern implementations found in both the Java SE and Java EE API's.
javax.xml.parsers.DocumentBuilderFactory#newInstance()
javax.xml.transform.TransformerFactory#newInstance()
javax.xml.xpath.XPathFactory#newInstance()
java.lang.StringBuilder#append()
(unsynchronized)java.lang.StringBuffer#append()
(synchronized)java.nio.ByteBuffer#put()
(also onCharBuffer
,ShortBuffer
,IntBuffer
,LongBuffer
,FloatBuffer
andDoubleBuffer
)javax.swing.GroupLayout.Group#addComponent()
java.lang.Appendable
java.util.Calendar#getInstance()
java.util.ResourceBundle#getBundle()
java.text.NumberFormat#getInstance()
java.nio.charset.Charset#forName()
java.net.URLStreamHandlerFactory#createURLStreamHandler(String)
(Returns singleton object per protocol)java.lang.Object#clone()
(the class has to implementjava.lang.Cloneable
)java.lang.Runtime#getRuntime()
java.awt.Desktop#getDesktop()
java.util.Arrays#asList()
java.io.InputStreamReader(InputStream)
(returns aReader
)java.io.OutputStreamWriter(OutputStream)
(returns aWriter
)javax.xml.bind.annotation.adapters.XmlAdapter#marshal()
and#unmarshal()
new LinkedHashMap(LinkedHashSet, List)
which returns an unmodifiable linked map which doesn't clone the items, butusesthem. Thejava.util.Collections#newSetFromMap()
andsingletonXXX()
methods however comes close.java.awt.Container#add(Component)
(practically all over Swing thus)javax.faces.component.UIComponent#getChildren()
(practically all over JSF UI thus)java.io.InputStream
,OutputStream
,Reader
andWriter
have a constructor taking an instance of same type.java.util.Collections
, thecheckedXXX()
,synchronizedXXX()
andunmodifiableXXX()
methods.javax.servlet.http.HttpServletRequestWrapper
andHttpServletResponseWrapper
javax.faces.context.FacesContext
, it internally uses among others the abstract/interface typesLifeCycle
,ViewHandler
,NavigationHandler
and 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.java.lang.Integer#valueOf(int)
(also onBoolean
,Byte
,Character
,Short
andLong
)java.lang.reflect.Proxy
java.rmi.*
, the whole API actually.The Wikipedia example is IMHO a bit poor, lazy loading has actually completely nothing to do with the proxy pattern at all.
java.util.logging.Logger#log()
javax.servlet.Filter#doFilter()
java.lang.Runnable
javax.swing.Action
java.util.Pattern
java.text.Normalizer
java.text.Format
javax.el.ELResolver
java.util.Iterator
(thus among others alsojava.util.Scanner
!).java.util.Enumeration
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()
java.util.Date
(the setter methods do that,Date
is internally represented by along
value)java.io.Serializable
javax.faces.component.StateHolder
java.util.Observer
/java.util.Observable
(rarely used in real world though)java.util.EventListener
(practically all over Swing thus)javax.servlet.http.HttpSessionBindingListener
javax.servlet.http.HttpSessionAttributeListener
javax.faces.event.PhaseListener
javax.faces.lifecycle.LifeCycle#execute()
(controlled byFacesServlet
, the behaviour is dependent on current phase (state) of JSF lifecycle)java.util.Comparator#compare()
, executed by among othersCollections#sort()
.javax.servlet.http.HttpServlet
, theservice()
and alldoXXX()
methods takeHttpServletRequest
andHttpServletResponse
and the implementor has to process them (and not to get hold of them as instance variables!).javax.servlet.Filter#doFilter()
java.io.InputStream
,java.io.OutputStream
,java.io.Reader
andjava.io.Writer
.java.util.AbstractList
,java.util.AbstractSet
andjava.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.javax.lang.model.element.AnnotationValue
andAnnotationValueVisitor
javax.lang.model.element.Element
andElementVisitor
javax.lang.model.type.TypeMirror
andTypeVisitor