JDK Source Reading-2-design mode-Create pattern

Source: Internet
Author: User

A. Create pattern

    1. Abstraction Factory (abstract Factory)

      javax.xml.parsers.DocumentBuilderFactory

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/6F/A6/wKiom1Wjy1-R1kHUAADm1diI0RY705.jpg "title=" Abstractfactory documentbuilderfactory.jpg "alt=" Wkiom1wjy1-r1khuaadm1dii0ry705.jpg "/>

Documentbuilderfactory instantiate specific factory through Factoryfinder.


Example of use: documentbuilderfactory docbuilderfactory = documentbuilderfactory.newinstance ();D Ocumentbuilder DocBuilder = Docbuilderfactory.newdocumentbuilder ();D ocument doc = docbuilder.parse (new File ("Xxx.xml"));


  javax.xml.transform.TransformerFactory

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/6F/B1/wKiom1WlGZmgxa6gAAE7aRCpl9c233.jpg "title=" Transformerfactory.jpg "alt=" Wkiom1wlgzmgxa6gaae7arcpl9c233.jpg "/>

Example of use: streamsource Source = new Streamsource (...); Streamsource Stylesource = new Streamsource (...); Transformerfactory factory = Transformerfactory.newinstance (); Transformer Transformer = Factory.newtransformer (Stylesource); Streamresult result = new Streamresult (System.out); Transformer.transform (source, result)

  javax.xml.xpath.XPathFactory

The structure is almost the same as the previous.

Xpathfactory xpathfactory = Xpathfactory.newinstance (); XPath XPath = Xpathfactory.newxpath (); XPathExpression expr = Xpath.compile (xpath_expression_string); object result = Expr.evaluate (object item, QName ReturnType);



2. Generator Mode (builder)

java.lang.Appendable

 java.lang.StringBuilder#append()

 java.lang.StringBuffer#append()

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/6F/BB/wKiom1WmZcWRsuYhAADz5E0mWPM594.jpg "title=" Stringbuilder.jpg "alt=" Wkiom1wmzcwrsuyhaadz5e0mwpm594.jpg "/>

Here we see that the transformation of the State will return objects of the original type, so that the build chain can be built.


 java.nio.ByteBuffer#put()(CharBuffer, ShortBuffer, IntBuffer, LongBuffer , and more ) FloatBuffer DoubleBuffer

See the example above.


3. Factory methods (Factory method)

 java.util.Calendar#getInstance()

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/6F/B8/wKioL1Wma_ujElhuAADSTk71wnQ429.jpg "title=" Calendar-factory method.jpg "alt=" Wkiol1wma_ujelhuaadstk71wnq429.jpg "/>

The method of constructing the factory method is on the type to be generated. This abstract factory is very different.

Others can be found in:

 java.util.ResourceBundle#getBundle()

 java.text.NumberFormat#getInstance()

 java.nio.charset.Charset#forName()

 java.net.URLStreamHandlerFactory#createURLStreamHandler(String)

4. Prototype mode (Prototye)
    • java.lang.Object#clone()


5. Single case mode (Singleton)

 java.lang.Runtime#getRuntime()

 java.awt.Desktop#getDesktop()



JDK Source Reading-2-design mode-Create pattern

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.