Java design pattern-factory pattern (springweb as an example), design pattern springweb
Generally, there are three factory models: simple factory model, factory method model, and abstract factory model. Let's dive deeper into these three factory models.
1. Start with an example of using the abstract factory mode in the springWeb. jar package.
I have been obsessed with spring before, so when I need to send an http request, I use the http client that comes with spring and run the Code:
Import java. io. inputStream; import java.net. URI; import java. nio. charset. charset; import org. springframework. http. httpMethod; import org. springframework. http. client. clientHttpRequest; import org. springframework. http. client. clientHttpRequestFactory; import org. springframework. http. client. clientHttpResponse; import org. springframework. http. client. simpleClientHttpRequestFactory; import org. springframework. util. streamUtils; public class Client {public static void main (String [] args) throws Exception {URI uri = new URI ("https://www.cnblogs.com /"); // create an abstract ClientHttpRequest factory ClientHttpRequestFactory chrf = new SimpleClientHttpRequestFactory (); // produce an abstract ClientHttpRequest req = chrf. createRequest (uri, HttpMethod. GET); // ClientHttpRequest execute the execute method ClientHttpResponse res = req.exe cute (); InputStream is = res. getBody (); String strBody = StreamUtils. copyToString (is, Charset. forName ("UTF-8"); is. close (); System. out. println (strBody );}}
The first thing to do is factory:
Product class, because the product class is a little complicated, Let's first look at the definition of the product class interface. Looking at the definition of this product class, you will think that spring is so complicated, why not directly open a unified interface HttpRequest,
Put the getBody in httpOutputMessage.
In fact, the reason why spring is designed in this way is to follow"Interface isolation principle ".
Why should we follow this principle? After reading the spring-web-release.jar package, you will find that httpMessage is extends by three interfaces: HttpOutputMessage, HttpInputMessage, HttpRequest.
These three interfaces have more than 10 Implementation classes. If they are combined, the three interfaces need to be repeated three times.
Let's talk about HttpOutputMessage and HttpInputMessage. These two interfaces are the most important interfaces for springMVC. They are the carriers of springMVC transmission. We will also talk about them later.
Looking at the specific product implementation class, I prefer to put the method into the class diagram, so it looks a little bloated. As we can see above, the clientHttpRequest interface has a total of five interface methods that need to be implemented by subclass.
I guess spring is like this:
1. First define the ClientHttpRequest interface of the implement abstract class, and then perform basic implementation on clientHttpRequest in the abstract class, which is exactly the same as the idea in the spring. core. io package analyzed by the author.
This is used in the design mode."Template method"Mode, but the template method is relatively simple, so you will not post it separately.
In the interface methodsGetHeaders (), getBody (), execute ()Three methods,
Then, two Abstract METHODS getBodyInternal (HttpHeaders headers), executeInternal (HttpHeaders headers), and ),
The two abstract methods have an abstract class and two specific implementation classes to implement the abstract method. That is, the class diagram of the product implementation class has not been completed yet, it's just a tip of the iceberg, but we can see it in the shadows. Let's take a look.
3. The remaining two interface methods,GetURI, getMethod ()Method inSimpleStreamingClientHttpRequestThe specific implementation class.
4. Analyze it again.SimpleBufferingClientHttpRequestThe underlying implementation class.GetURI, getMethod ()Is in thisSimpleBufferingClientHttpRequest underlying class. At the same time, the two abstract methods mentioned at areSimpleBufferingClientHttpRequestParent abstract classAbstractBufferingClientHttpRequestThe specific implementation has been carried out.
5. Conclusion,The above classes basically correspond to an interface method @ Override, I guess this is to matchRys replacement principle(Where each parent class can be used, replacement of its child class will not affect it in the past ).
In fact, I expect the interface methods of the parent class override class. Later, the method of the grandson class override parent class seems to be rarely used.
The factory model used by spring should be the most complex.Abstract Factory ModelIt's really complicated to inherit the tree or product family.
Back to the initial requirement, if you only want to initiate a simple http request, use the factory method or simple factory mode.
Ii. Factory method mode and simple factory Mode
For example, we will cut down some of the products and interfaces abstracted for expansion in the abstract factory, and the class diagram will look like this.
The author intentionally draws a factory implementation class more than the above class diagram, so that the ClientHttpRequestFactory interface will not look very bad. In fact, the factory method can greatly increase the scalability of the program.
Delete the specific factory implementation class OkHttp3ClientHttpRequestFactory, and it becomes a class diagram of the simple factory mode.
So far, the three factory models have been introduced. If there are any mistakes, please criticize and correct them.
Join the study exchange group 569772982 to learn and exchange.