本文主要是介绍在Java 核心类库中关于GOF设计模式的例子,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
原文地址:http://blog.csdn.net/jiangkai528/article/details/45195915
在Java 核心类库中关于GOF设计模式的例子
问题: 我正在学习GOF的java 设计模式,我想找一些它们中鲜活的例子,在java 核心类库中有哪些好的例子呢?
回答:你可以在维基百科上获得许多设计模式概述。同时也会提到哪些是由GOF提出的。在这里我总结一下,我尽可能多的从java SE 和java EE 的 API 中选取一些设计模式的实现
1.抽象工厂模式 (abstract factory)
javax.xml.parsers.DocumentBuilderFactory#newInstance()
javax.xml.transform.TransformerFactory#newInstance()
javax.xml.xpath.XPathFactory#newInstance()
java.lang.StringBuilder#append()
(不同步,线程不安全)java.lang.StringBuffer#append()
(同步的,线程安全)java.nio.ByteBuffer#put()
(同时还有CharBuffer
,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)
(每次协议返回单个对象)
java.lang.Object#clone()
(这个类必须实现java.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()
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, butuses them. Thejava.util.Collections#newSetFromMap()
and singletonXXX()
methods however comes close.
8.组合模式(composite)
java.awt.Container#add(Component)
(practically all over Swing thus)javax.faces.component.UIComponent#getChildren()
(practically all over JSF UI thus)
- All subclasses of
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
10.外观模式(facade)
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)
(同时还有Boolean
,Byte
,Character
,Short
and Long
)
12.代理模式(proxy)
java.lang.reflect.Proxy
java.rmi.*
, 整个API
java.util.logging.Logger#log()
javax.servlet.Filter#doFilter()
- All implementations of
java.lang.Runnable
- All implementations of
javax.swing.Action
15. 解析器模式(interpreter)
java.util.Pattern
java.text.Normalizer
- All subclasses of
java.text.Format
- All subclasses of
javax.el.ELResolver
16. 迭代器模式 (iterator)
- All implementations of
java.util.Iterator
(thus among others alsojava.util.Scanner
!). - All implementations of
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()
18.备忘录模式(memento)
java.util.Date
(the setter methods do that,Date
is internally represented by along
value)- All implementations of
java.io.Serializable
- All implementations of
javax.faces.component.StateHolder
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.HttpSessionBindingListener
javax.servlet.http.HttpSessionAttributeListener
javax.faces.event.PhaseListener
javax.faces.lifecycle.LifeCycle#execute()
(controlled by FacesServlet
, the behaviour is dependent on current phase (state) of JSF lifecycle)
21.策略模式(strategy)
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()
22. 模板方法模式(template method)
- All non-abstract methods of
java.io.InputStream
,java.io.OutputStream
,java.io.Reader
andjava.io.Writer
. - All non-abstract methods of
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
这篇关于在Java 核心类库中关于GOF设计模式的例子的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!