Java design Pattern-Factory mode (Springweb as an example)

Source: Internet
Author: User

In general, the factory model is divided into 3 types, simple Factory mode, factory method mode, abstract Factory mode. These three kinds of factory models go deep in layers.

one, from the Springweb.jar package using an example of an abstract factory pattern chat

Prior to the spring various obsession, so when the need to send HTTP requests, with the spring comes with the HTTP client, the code:

ImportJava.io.InputStream;ImportJava.net.URI;ImportJava.nio.charset.Charset;ImportOrg.springframework.http.HttpMethod;Importorg.springframework.http.client.ClientHttpRequest;Importorg.springframework.http.client.ClientHttpRequestFactory;ImportOrg.springframework.http.client.ClientHttpResponse;Importorg.springframework.http.client.SimpleClientHttpRequestFactory;Importorg.springframework.util.StreamUtils; Public classClient { Public Static voidMain (string[] args)throwsexception{uri URI=NewURI ("https://www.cnblogs.com/"); //Create an abstract clienthttprequest factoryClienthttprequestfactory CHRF =Newsimpleclienthttprequestfactory (); //produce an abstract clienthttprequestClienthttprequest req =chrf.createrequest (URI, httpmethod.get); //Clienthttprequest Execute Execute methodClienthttpresponse res =Req.execute (); InputStream is=Res.getbody (); String strbody= Streamutils.copytostring (IS, Charset.forname ("UTF-8"));        Is.close ();            System.out.println (strbody); }}

On the UML diagram, the first is the factory class:

Product class, because the product class is a little complex, first look at the definition of product class interface, see the definition of this product class, you will think spring is so complex why, why not directly open a unified interface HttpRequest,

It would be nice to put the getbody in the Httpoutputmessage.

In fact, the reason why spring is so designed is to follow the "interface isolation principle".

Why should we follow this principle? Because after watching the Spring-web-release.jar bag you will find that the Httpmessage is three interface extends, respectively, is httpoutputmessage,httpinputmessage,httprequest.

These three interfaces have more than 10 implementation classes, and if they are together, they need to be repeated 3 times in three interfaces.

Talk about Httpoutputmessage,httpinputmessage, these two interfaces for SPRINGMVC is the heavy weight, is the carrier of SPRINGMVC transmission, we will meet them after the SPRINGMVC framework.

Look at the specific product realization class, the author prefers to put the method into the class diagram, so a little bit bloated. As we saw earlier, there are 5 interface methods in the Clienthttprequest interface that require subclasses to implement.

I guess spring is like this:

1, first define a few abstract class implement that Clienthttprequest interface, and then in the abstract class to do the basic implementation of clienthttprequest, and before the author analysis Spring.core.io package inside the same idea.

This is used in the design mode of the "template method" mode, but the template method is relatively simple, do not open a separate chat.

2, 5 interface methods, implemented in Abstractclienthttprequest getheaders (), GetBody (), execute () 3 methods,

It was then not easy to add two abstract methods to his subclass Getbodyinternal (httpheaders headers), executeinternal (httpheaders headers),

And then quietly tell you, these two abstract methods have an abstract class and two specific implementation classes to implement the abstract method, that is, our product implementation class diagram has not finished painting, just painted the tip of the iceberg, but glimpse visible, will see it.

3. The remaining two interface methods, theGeturi,getmethod () method, are implemented in the simplestreamingclienthttprequest specific implementation class.

4, finally analyze the bottom simplebufferingclienthttprequest of the implementation class. Geturi,getmethod () is implemented in this simplebufferingclienthttprequest underlying class . At the same time the 2nd mentioned two not easy abstract methods in Simplebufferingclienthttprequest 's parent abstract class Abstractbufferingclienthttprequest A concrete implementation has been made.

5. Summary, the above several classes basically an interface method corresponds to a @override, I guess this is to conform to the Richter scale substitution principle (each parent class can use, his sub-class substitution in the past will have no effect).

In fact, I really look forward to the parent class override the Grandfather class interface method, and then, the grandson class again override the parent class's method, seems to have rarely this usage.

Spring uses this kind of factory model, it should be the most complex abstract factory model , inheritance tree, product family What, really good complex.

Back to the beginning of the demand, in fact, if only to launch a simple HTTP request, with the factory method mode or simple Factory mode is OK.

II: Factory method mode and simple Factory mode

For a chestnut, let's cut off the product families of the above abstract factories, and so on, some classes and interfaces that are abstracted out to be expanded, and the class diagram becomes this way.

The author deliberately than the above class diagram painting a factory implementation class, let clienthttprequestfactory this interface will not appear very chicken. Using the factory approach model has been able to greatly increase the expansion of the program.

By deleting the specific implementation class of the Okhttp3clienthttprequestfactory factory, it becomes the class diagram of the simple factory model.

At this point, 3 kinds of factory models have been introduced, if there are mistakes, but also please bo friends criticize.

Java design Pattern-Factory mode (Springweb as an example)

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.