1. Introduction to the simple factory model
Simple Factory mode, also known as the "static factory method mode". It belongs to the "create mode" (the pattern for creating an object) and is a special implementation of the "factory method" pattern.
Usually, we use the simple factory pattern to create classes. For example, obtaining thread pool objects is achieved through simple factory mode. Its structure diagram is as follows:
Factory: The factory is the core of the simple factory model and provides external interfaces. When a client or other program wants to obtain Product objects, they are obtained through the Factory interface.
Abstract Products: Abstract products are abstracted by (many) different products. Product can be an interface or an abstract class.
Specific product: The product object returned in the factory is actually created through ConcreteProduct.
2. Simple factory pattern code model
public class Factory { public static Product newInstance() { return new ConcreteProduct(); }}public abstract Product {}public class ConcreteProduct extends Product { public ConcreteProduct() {}} Class diagram of the model
3. Practical application of simple factory model
When we write a Servlet to handle client requests, we often handle multiple business logics in one Servlet, for example:
protected void doPost(HttpServletRequest request, HttpServletResponse response) { String flag = request.getParameter("flag"); if(flag.equals("service1")) { service1(); }else if(flag.equals("service2")) { service2(); } ...}The above is the general method of our Servlet to handle multi-business logic, and write a bunch of if else statements. A better way is to separate the requested distribution from the Servlet, so that the Servlet only handles business logic. We regard various requested Servlets as product classes. javax.servlet.HttpServlet is the product parent class, and javax.servlet.Servlet is the product interface. In this way, we define a ServletFactory, parse the url request in the filter and hand it over to the ServletFactory for processing. This is a typical simple factory application.
@WebFilter("/TransRequest")public class TransRequest implements Filter{ private String servletName; @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest myRequest = (HttpServletRequest)request; //Get the requested servlet name, here we agree that the request is in the form of /servletName String names[] = myRequest.getRequestURI().trim().split("/"); servletName = names[2]; if( servletName != null) { //The following are two most typical examples of simple factories Servlet servlet = ServletFactory.createServlet(servletName); servlet.service(request, response); }else chain.doFilter(request, response); } Each time we ask for a request, we use the factory to produce a servlet, which can avoid configuring a large amount of servlet path information in the XML. And this will also make the logic clearer, and servlets only handle business at the business level.
Factory categories are as follows:
public class ServletFactory { public static Servlet createServlet(String servletName) throws ServletException { if(servletName.equals("servletName1")) { return new Service1(); }else if(servletName.equals("servletName2")){ return new Service2(); }else{ throw new ServletException("No such servlet"); } }}Although the above factory class does not put aside the cumbersome if else, the idea of using the simple factory still solves some problems. A simple factory is a very simple design pattern that cannot be considered a design pattern, and the problems solved are also limited. The above request distribution has been implemented. For example, Struts2, of course, the framework is not a simple factory.
4. Summary
The simple factory model, to sum up, is a factory class, a product interface (in fact, it can also be an abstract class, or even an ordinary parent class) and a group of specific products that implement product interfaces. This factory class creates a specific implementation class based on the passed parameters and transforms it upward into an interface as the result.