Patterns Used In Java
patternsjava
Intermediate
developer
Creational patterns
Abstract factory
- javax.xml.parsers.DocumentBuilderFactory#newInstance()
- javax.xml.transform.TransformerFactory#newInstance()
- javax.xml.xpath.XPathFactory#newInstance()
Builder
- 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
Factory Method
- 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
Prototype
- java.lang.Object#clone() (the class has to implement java.lang.Cloneable)
singleton
- java.lang.Runtime#getRuntime()
- java.awt.Desktop#getDesktop()
- java.lang.System#getSecurityManager()
- java.util.logging.LogManager
Structural patterns
Adapter
- 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
- 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.
Composite
- java.awt.Container#add(Component) (practically all over Swing thus)
- javax.faces.component.UIComponent#getChildren() (practically all over JSF UI thus)
Decorator
- All subclasses of java.io.InputStream, OutputStream, Reader and Writer, FilterInputStream have a constructor taking an instance of same or parent type.
- java.util.Collections, the checkedXXX(), synchronizedXXX() and unmodifiableXXX() methods.
- javax.servlet.http.HttpServletRequestWrapper and HttpServletResponseWrapper
- javax.swing.JScrollPane
Facade
- 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
- java.lang.Integer#valueOf(int) (also on Boolean, Byte, Character, Short, Long and BigDecimal)
Proxy
- java.lang.reflect.Proxy
- java.rmi.*
- javax.ejb.EJB (explanation here)
- javax.inject.Inject (explanation here)
- javax.persistence.PersistenceContext
Behavioral patterns
Chain of responsibility
Command
- All implementations of java.lang.Runnable
- All implementations of javax.swing.Action
Interpreter
- java.util.Pattern
- java.text.Normalizer
- All subclasses of java.text.Format
- All subclasses of javax.el.ELResolver
Iterator
- All implementations of java.util.Iterator (thus among others also java.util.Scanner!).
- All implementations of java.util.Enumeration
Mediator
- java.util.Timer (all
scheduleXXX()
methods) - java.util.concurrent.Executor#execute()
- java.util.concurrent.ExecutorService (the
invokeXXX()
andsubmit()
methods) - java.util.concurrent.ScheduledExecutorService (all
scheduleXXX()
methods) - java.lang.reflect.Method#invoke()
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
Observer
- java.util.Observer/java.util.Observable
- All implementations of java.util.EventListener (e.g. in Swing)
- javax.servlet.http.HttpSessionBindingListener
- javax.servlet.http.HttpSessionAttributeListener
- javax.faces.event.PhaseListener
State
- javax.faces.lifecycle.LifeCycle#execute() (controlled by FacesServlet, the behaviour is dependent on current phase (state) of JSF lifecycle)
Strategy
- java.util.Comparator#compare(), e.g. executed by List#sort().
- javax.servlet.http.HttpServlet, the
service()
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()
Template method
- 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.