I am learning GoF Java Design Patterns and I want to see some real life examples of them. Can you guys point to some good usage of these Design Patterns.(preferably in Java's core libraries).
Thank you
I am learning GoF Java Design Patterns and I want to see some real life examples of them. Can you guys point to some good usage of these Design Patterns.(preferably in Java's core libraries).
Thank you
Observable, Observer)ContainerAdapter, ComponentAdapter, FocusAdapter, KeyAdapter, MouseAdapter are not adapters; they are actually Null Objects. Poor naming choice by Sun.BufferedInputStream can decorate other streams such as FilterInputStream)java.lang.Math could be considered a kind of Singleton (final + all methods static)ButtonGroup for Mediator patternAction, AbstractAction may be used for different visual represntations to execute same code -> Command patternand many more I guess
The Abstract Factory pattern is used in various places. E.g., DatagramSocketImplFactory, PreferencesFactory. There are many more---search the Javadoc for interfaces which have the word "Factory" in their name.
Also there are quite a few instances of the Factory pattern, too.
Even though I'm sort of a broken clock with this one, Java XML API uses Factory a lot. I mean just look at this:
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(source);
String title = XPathFactory.newInstance().newXPath().evaluate("//title", doc);
...and so on and so forth.
Additionally various Buffers (StringBuffer, ByteBuffer, StringBuilder) use Builder.
clone() method can be used for this purpose.RMI is based on Proxy.
Should be possible to cite one for most of the 23 patterns in GoF:
I can't think of examples in Java for 10 out of the 23, but I'll see if I can do better tomorrow. That's what edit is for.
You can find an overview of a lot design patterns in Wikipedia. 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.
java.util.Calendar#getInstance()java.util.Arrays#asList()java.util.ResourceBundle#getBundle()java.net.URL#openConnection()java.sql.DriverManager#getConnection()java.sql.Connection#createStatement()java.sql.Statement#executeQuery()java.text.NumberFormat#getInstance()java.lang.management.ManagementFactory (all getXXX() methods)java.nio.charset.Charset#forName()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()java.lang.Appendablejava.lang.Object#toString() (overrideable in all subclasses)java.lang.Class#newInstance()java.lang.Integer#valueOf(String) (also on Boolean, Byte, Character, Short, Long, Float and Double)java.lang.Class#forName()java.lang.reflect.Array#newInstance()java.lang.reflect.Constructor#newInstance()java.lang.Object#clone() (the class has to implement java.lang.Cloneable)java.io.InputStreamReader(InputStream) (returns a Reader)java.io.OutputStreamWriter(OutputStream) (returns a Writer)javax.xml.bind.annotation.adapters.XmlAdapter#marshal() and #unmarshal()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.util.Map#putAll(Map)java.util.List#addAll(Collection)java.util.Set#addAll(Collection)java.nio.ByteBuffer#put(ByteBuffer) (also on CharBuffer, ShortBuffer, IntBuffer, LongBuffer, FloatBuffer and DoubleBuffer)java.awt.Container#add(Component) (practically all over Swing thus)java.io.InputStream, OutputStream, Reader and Writer have a constructor taking an instance of same type.java.util.List, Set and Map have a constructor taking an instance of same type.java.util.Collections, the checkedXXX(), synchronizedXXX() and unmodifiableXXX() methods.javax.servlet.http.HttpServletRequestWrapper and HttpServletResponseWrapperjavax.faces.webapp.FacesServlet, it internally uses under each the abstract/interface types ServletContext, LifeCycle, ViewHandler, NavigationHandler and many more without that the enduser has to worry about it (which are however overrideable by injection).java.lang.reflect.Proxyjava.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.lang.Runnablejavax.swing.Actionjava.util.Patternjava.text.Normalizerjava.text.Formatjavax.el.ELResolverjava.util.Iterator (thus under each also java.util.Scanner!).java.util.Enumerationjava.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)java.io.Serializablejavax.faces.component.StateHolderjava.util.Observer/java.util.Observable (rarely used in real world though)java.util.EventListener (practically all over Swing thus)javax.servlet.http.HttpSessionBindingListenerjavax.servlet.http.HttpSessionAttributeListenerjavax.faces.event.PhaseListenerjava.util.Iteratorjavax.faces.lifecycle.LifeCycle#execute() (controlled by FacesServlet, the behaviour is dependent on current phase (state) of JSF lifecycle)java.util.Comparator#compare(), executed by under each 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()java.io.InputStream, java.io.OutputStream, java.io.Reader and java.io.Writer.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.