读书人

JAVA JDK 中的设计方式

发布时间: 2012-12-25 16:18:29 作者: rapoo

JAVA JDK 中的设计模式

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.


Creational patterns

Abstract factory?(recognizeable by creational methods returning an abstract/interface type)
  • 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()

    Builder?(recognizeable by creational methods returning the instance itself)
    • 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

      Factory method?(recognizeable by creational methods returning a concrete type)
      • java.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()

        Prototype?(recognizeable by creational methods returning a?different?instance of itself with the same properties)
        • java.lang.Object#clone()?(the class has to implement?java.lang.Cloneable)

          Singleton?(recognizeable by creational methods returning the?same?instance (usually of itself) everytime)
          • java.lang.Runtime#getRuntime()
          • java.awt.Desktop#getDesktop()

            Structural patterns

            Adapter?(recognizeable by creational methods taking an instance of?different?abstract/interface type and returning an implementation of own/another abstract/interface type which?decorates/overrides?the given instance)
            • java.io.InputStreamReader(InputStream)?(returns a?Reader)
            • java.io.OutputStreamWriter(OutputStream)?(returns a?Writer)
            • javax.xml.bind.annotation.adapters.XmlAdapter#marshal()?and?#unmarshal()

              Bridge?(recognizeable by creational methods taking an instance of?different?abstract/interface type and returning an implementation of own abstract/interface type which?delegates/uses?the given instance)
              • 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?(recognizeable by behavioral methods taking an instance of?same?abstract/interface type)
                • 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)

                  Decorator?(recognizeable by creational methods taking an instance of?same?abstract/interface type)
                  • All subclasses of?java.io.InputStream,?OutputStream,?Reader?and?Writer?have a constructor taking an instance of same type.
                  • Almost all implementations of?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?HttpServletResponseWrapper

                    Facade?(recognizeable by behavioral methods which internally uses instances of?different?independent abstract/interface types)
                    • 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,font-size: 14px; vertical-

读书人网 >软件开发

热点推荐