本文首發於 www.yidooo.net/2018/06/08/… 轉載請註明出處html
《設計模式:可複用面向對象軟件的基礎》一書中提出了24中經典的設計模式,這些設計模式被普遍地運用於項目實戰之中。這篇博客的重點並不在於講解這些設計模式以及使用,而主要列舉了在Java Core Libraries中間所用到的設計模式。Java Core Libraries中的API設計基本涵蓋了GOF書中所提到的絕大部分設計模式,能夠說是API設計的典範,很是值得借鑑和學習。java
建立型模式
這些設計模式提供了一種在建立對象的同時隱藏建立邏輯的方式,而不是使用 new 運算符直接實例化對象。這使得程序在判斷針對某個給定實例須要建立哪些對象時更加靈活。設計模式
工廠模式(Factory Pattern)
- 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.util.EnumSet#of()
- javax.xml.bind.JAXBContext#createMarshaller() and other similar methods
抽象工廠模式(Abstract Factory Pattern)
- javax.xml.parsers.DocumentBuilderFactory#newInstance()
- javax.xml.transform.TransformerFactory#newInstance()
- javax.xml.xpath.XPathFactory#newInstance()
單例模式(Singleton Pattern)
- java.lang.Runtime#getRuntime()
- java.awt.Desktop#getDesktop()
- java.lang.System#getSecurityManager()
建造者模式(Builder Pattern)
- java.lang.StringBuilder#append() (unsynchronized)
- java.lang.StringBuffer#append() (synchronized)
- java.nio.ByteBuffer#put() (also on CharBuffer, ShortBuffer, IntBuffer, LongBuffer, FloatBuffer and DoubleBuffer)
- javax.swing.GroupLayout.Group#addComponent()
原型模式(Prototype Pattern)
結構型模式
這些設計模式關注類和對象的組合。繼承的概念被用來組合接口和定義組合對象得到新功能的方式app
適配器模式(Adapter Pattern)
- java.util.Arrays#asList()
- java.util.Collections#list()
- java.util.Collections#enumeration()
- java.io.InputStreamReader(InputStream) (returns a Reader)
- java.io.OutputStreamWriter(OutputStream) (returns a Writer)
- javax.xml.bind.annotation.adapters.XmlAdapter#marshal() and #unmarshal()
橋接模式(Bridge Pattern)
TODOide
過濾器模式(Filter、Criteria Pattern)
組合模式(Composite Pattern)
- java.awt.Container#add(Component)
- javax.faces.component.UIComponent#getChildren()
裝飾器模式(Decorator Pattern)
- All subclasses of java.io.InputStream, OutputStream, Reader and Writer have a constructor taking an instance of same type.
- java.util.Collections, the checkedXXX(), synchronizedXXX() and unmodifiableXXX() methods.
- javax.servlet.http.HttpServletRequestWrapper and HttpServletResponseWrapper
外觀模式(Facade Pattern)
- javax.faces.context.FacesContext, it internally uses among others the abstract/interface types LifeCycle, 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 uses ServletContext, HttpSession, HttpServletRequest, HttpServletResponse, etc.
享元模式(Flyweight Pattern)
- java.lang.Integer#valueOf(int) (also on Boolean, Byte, Character, Short, Longand BigDecimal)
代理模式(Proxy Pattern)
- java.lang.reflect.Proxy
- java.rmi.*
- javax.ejb.EJB (explanation here)
- javax.inject.Inject (explanation here)
- javax.persistence.PersistenceContext
行爲型模式
責任鏈模式(Chain of Responsibility Pattern)
- java.util.logging.Logger#log()
- javax.servlet.Filter#doFilter()
命令模式(Command Pattern)
- All implementations of java.lang.Runnable
- All implementations of javax.swing.Action
解釋器模式(Interpreter Pattern)
- java.util.Pattern
- java.text.Normalizer
- All subclasses of java.text.Format
- All subclasses of javax.el.ELResolver
迭代器模式(Iterator Pattern)
- All implementations of java.util.Iterator (thus among others also java.util.Scanner!).
- All implementations of java.util.Enumeration
中介者模式(Mediator Pattern)
- java.util.Timer (all scheduleXXX() methods)
- java.util.concurrent.Executor#execute()
- java.util.concurrent.ExecutorService (the invokeXXX() and submit() methods)
- java.util.concurrent.ScheduledExecutorService (all scheduleXXX() methods)
- java.lang.reflect.Method#invoke()
備忘錄模式(Memento Pattern)
- java.util.Date (the setter methods do that, Date is internally represented by a longvalue)
- All implementations of java.io.Serializable
- All implementations of javax.faces.component.StateHolder
觀察者模式(Observer Pattern)
- 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
狀態模式(State Pattern)
- javax.faces.lifecycle.LifeCycle#execute() (controlled by FacesServlet, the behaviour is dependent on current phase (state) of JSF lifecycle)
空對象模式(Null Object Pattern)
TODO學習
策略模式(Strategy Pattern)
- java.util.Comparator#compare(), executed by among others Collections#sort().
- javax.servlet.http.HttpServlet, the service() and all doXXX() methods take HttpServletRequest and HttpServletResponse and the implementor has to process them (and not to get hold of them as instance variables!).
- javax.servlet.Filter#doFilter()
模板模式(Template Pattern)
- All non-abstract methods of java.io.InputStream, java.io.OutputStream, java.io.Reader and java.io.Writer.
- All non-abstract methods of java.util.AbstractList, java.util.AbstractSet and java.util.AbstractMap. javax.servlet.http.HttpServlet, all the doXXX() 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 Pattern)
- javax.lang.model.element.AnnotationValue and AnnotationValueVisitor
- javax.lang.model.element.Element and ElementVisitor
- javax.lang.model.type.TypeMirror and TypeVisitor
- java.nio.file.FileVisitor and SimpleFileVisitor
- javax.faces.component.visit.VisitContext and VisitCallback
參考資料