Java源码中哪里用了设计模式

走着路睡觉
  • java
大约 2 分钟

创建型模式

抽象工厂

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 on CharBuffer, ShortBuffer, IntBuffer, LongBuffer, FloatBuffer and DoubleBuffer)

javax.swing.GroupLayout.Group#addComponent()

All implementations of java.lang.Appendable

java.util.stream.Stream.Builder

工厂模式

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

原型模式

java.lang.Object#clone() (the class has to implement java.lang.Cloneable)

单例模式

java.lang.Runtime#getRuntime()

java.awt.Desktop#getDesktop()

java.lang.System#getSecurityManager()

结构型模式

适配器模式

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()

桥接模式

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. The java.util.Collections#newSetFromMap() and singletonXXX() 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)

装饰器模式

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

javax.swing.JScrollPane

外观模式

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.

享元模式

java.lang.Integer#valueOf(int) (also on Boolean, Byte, Character, Short, Long and BigDecimal)

代理模式

java.lang.reflect.Proxy

java.rmi.*

javax.ejb.EJB (explanation here)

javax.inject.Inject (explanation here)

javax.persistence.PersistenceContext

行为型模式

Chain of responsibility (recognizeable by behavioral methods which (indirectly) invokes the same method in another implementation of same abstract/interface type in a queue)

java.util.logging.Logger#log()

javax.servlet.Filter#doFilter()

命令模式

All implementations of java.lang.Runnable

All implementations of javax.swing.Action

解释器模式

java.util.Pattern

java.text.Normalizer

All subclasses of java.text.Format

All subclasses of javax.el.ELResolver

迭代器模式

All implementations of java.util.Iterator (thus among others also java.util.Scanner!).

All implementations of java.util.Enumeration

中介者模式

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()

备忘录模式

java.util.Date (the setter methods do that, Date is internally represented by a long 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)

策略模式

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()

模板模式

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.

访问者模式

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

上次编辑于:
贡献者: zhaojingbo
Loading...