Design Patterns in the Java class Library

Source: Internet
Author: User

Original posts: Http://stackoverflow.com/questions/1673841/examples-of-gof-design-patterns

Question: I am learning Gof's " Design model " and want to know some examples of their application in practice.

Can you give me some good examples of using design patterns? Especially in the Java class Library.

Get the highest answer in favor of:

You can have a general understanding of design patterns through Wikipedia. Wikipedia has also raised the GOF of the model involved. Let me summarize here and try to point out how these patterns are used in the API of Javase and Java EE.

Create design pattern Abstract Factory mode

Feature: The Create method returns a factory class that can be used to create an abstract class or interface.

    • Javax.xml.parsers.documentbuilderfactory#newinstance ()
    • Javax.xml.transform.transformerfactory#newinstance ()
    • Javax.xml.xpath.xpathfactory#newinstance ()
Generator mode

Feature: The Create method returns the instance itself.

    • Java.lang.stringbuilder#append () not synchronizing
    • Java.lang.stringbuffer#append () Synchronization
    • Java.nio.bytebuffer#put () (besides, Charbuffer,shortbuffer,intbuffer,longbuffer,floatbuffer and DoubleBuffer)
    • Javax.swing.grouplayout.group#addcomponent ()
    • All implementations of java.lang.Appendable
Factory method Mode

Feature: The Create method returns an implementation of an abstract class or interface.

    • Java.util.calendar#getinstance ()
    • Java.util.resourcebundle#getbundle ()
    • Java.text.numberformat#getinstance ()
    • Java.nio.charset.charset#forname ()
    • Java.net.urlstreamhandlerfactory#createurlstreamhandler (String) returns a singleton object for each protocol (protocol)
Prototype mode

Features: The Create method returns an instance of the same type with the same property.

    • Java.lang.object#clone () (this class must implement Java.lang.Cloneable)
Single-Case mode

feature: The Creation method returns the same instance, regardless of when it is called.

    • Java.lang.runtime#getruntime ()
    • Java.awt.desktop#getdesktop ()
Structural Mode Adapter Mode

Features: The Create method accepts an instance of an abstract class or interface that differs from the current class as a parameter, returning an implementation of an abstract class or interface that has been decorated or overridden for a given instance of the parameter.

    • Java.util.arrays#aslist ()
    • Java.io.InputStreamReader (InputStream) returns a reader object
    • Java.io.OutputStreamWriter (OutputStream) returns a writer object
    • Javax.xml.bind.annotation.adapters.xmladapter#marshal () and #unmarshal ()
Bridging mode

Features: The Create method accepts an instance of an abstract class or interface that differs from the current class as a parameter, returning an implementation of an abstract class or interface that is proxied or uses a given number of parameters.

    • Temporary did not think. An example that can be thought of is new Linkedhashmap (Linkedhashset, List). This method returns an linkedmap that cannot be changed. It does not copy the elements in the parameters (item), but uses them directly. The Java.util.collections#newsetfrommap () and Singletonxxx () methods are similar.
Combination mode

feature: The Behavior method transforms an instance of the same abstract class or interface into a tree structure.

    • Java.awt.container#add (Component) (nearly all swing applies)
    • Javax.faces.component.uicomponent#getchildren () (nearly all JSF UIs apply)
Decoration mode

Feature: Create a method with an abstract class or an instance of an interface as a parameter. The return value is an instance of the parameter with additional methods added.

    • Java.io.InputStream, OutputStream, Reader, writer all subclasses of these classes have a constructor that accepts the same type as a parameter.
    • Java.util.Collections checkedxxx (), synchronizedxxx (), Unmodifiablexxx () method
    • Javax.servlet.http.HttpServletRequestWrapper and Httpservletresponsewrapper
Appearance mode

feature: behavioral methods are encapsulated internally using an instance of a completely different abstract class or interface.

    • Javax.faces.context.FacesContext, this class internally uses abstract classes or interfaces lifecycle, Viewhandler, Navigationhandler and other classes that the user does not need to be concerned with (usually these classes are rewritten by injection).

    • Javax.faces.context.ExternalContext, this class uses internal ServletContext, HttpSession, HttpServletRequest, HttpServletResponse and so on.
Enjoy meta mode

Feature: The Create method returns a cached instance. It is somewhat similar to multiple models.

    • java.lang.integer#valueof (int) (in addition to this.) Similar to Boolean, Byte, Character, short, Long)
Proxy mode

Features: The Creation method returns an instance of a given abstract class or interface, and there is an implementation of this instance proxy or using the given abstract class or interface.

    • Java.lang.reflect.Proxy
    • All classes under the java.rmi.* package

With all due respect, the class on Wikipedia is not very good, and lazy loading actually doesn't have anything to do with proxy mode.

Behavioral Mode responsibility Chain mode

Features: Behavioral methods Indirect calls to the same abstract class or interface in the queue also have an instance of the same name method.

    • Java.util.logging.logger#log ()
    • Javax.servlet.filter#dofilter ()
Command mode

feature: A behavior method call in an abstract class or interface also has a method that is implemented by an abstract class or interface that is wrapped by the command method at the time of creation.

    • All implementations of java.lang.Runnable
    • All implementations of Javax.swing.Action
Interpreter mode

feature: The Behavior method returns instances of different abstract classes or interfaces on the structure, and it is important to note that the parsing or formatting process is not part of this pattern. This process determines how the interpreter is going to apply and implement the transformation.

    • Java.util.Pattern
    • Java.text.Normalizer
    • All subclasses of the Java.text.Format class
    • All subclasses of the Javax.el.ELResolver class
Iterative mode

feature: The behavior method continuously returns different instances of the same object in the queue.

    • All implementations of the Java.util.Iterator class (Java.util.Scanner also similar)
    • All implementations of the Java.util.Enumeration class
Broker mode

feature: A behavior method accepts an instance of a different abstract class or interface (typically using a command pattern) as a parameter, and this parameter also proxies an instance of another given abstract class or interface at the same time.

    • Java.util.Timer all Schedulexxx () methods
    • Java.util.concurrent.executor#execute ()
    • Java.util.concurrent.ExecutorService's Invokexxx () and submit () method
    • Java.util.concurrent.ScheduledExecutorService all Schedulexxx () method
    • Java.lang.reflect.method#invoke ()
Memo Mode

feature: The behavior method internally changes the state of the entire instance.

    • Java.util.Date (setter method is a typical example, the Date object is represented by a long value in the content)
    • All implementations of java.io.Serializable
    • All implementations of Javax.faces.component.StateHolder
Observer (announcement/subscription) mode

feature: The Behavior method calls the method of an abstract class or interface instance according to its own state.

    • Java.util.observer/java.util.observable is less used in practice.
    • All implementations of Java.util.EventListener (nearly all swing objects apply)
    • Javax.servlet.http.HttpSessionBindingListener
    • Http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpSessionAttributeListener.html
    • Javax.faces.event.PhaseListener
State mode

characteristic: The behavior method changes its behavior according to the state it can control.

    • Javax.faces.lifecycle.lifecycle#execute () is controlled by Facesservlet and its behavior depends on the phase of the current JSF's life cycle
Policy mode

feature: The Behavior method in an abstract class or interface invokes a method that is passed in as a policy implementation and that has an abstract class or interface instance.

    • Java.util.comparator#compare (), this method is called by Collections#sort ()
    • Service () and all doxxx () in the Javax.servlet.http.HttpServlet class The method accepts both HttpServletRequest and httpservletresponse (not to accommodate them in such a way as member variables) so that all of its subclasses must handle them
    • Javax.servlet.filter#dofilter ()
Template method Mode

Features: A behavior method that defines the default behavior by an abstract type.

    • All non-abstract methods of Java.io.InputStream, Java.io.OutputStream, Java.io.Reader and Java.io.Writer classes
    • All non-abstract methods of Java.util.AbstractList, Java.util.AbstractSet and Java.util.AbstractMap
    • All Doxxx () methods of the Javax.servlet.http.HttpServlet class default send HTTP 405 "method different meaning" This error to response, you are free to choose whether to implement them
Interview mode

Features: by two different abstract classes or interfaces, they all have the method to accept the other side to do the parameter, the method called will call the method of another object, and then run according to the established strategy.

    • Javax.lang.model.element.AnnotationValue and Annotationvaluevisitor
    • Javax.lang.model.element.Element and Elementvisitor
    • Javax.lang.model.type.TypeMirror and Typevisitor

References translation:

Http://www.importnew.com/12526.html

Design Patterns in the Java class Library

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.