Take Java code as an example to explain the simple factory pattern in design pattern _java

Source: Internet
Author: User

1. Simple Factory Model Introduction
The Simple factory pattern (simply Factory) is also called the "static factory method pattern." It belongs to the "Create pattern" (the schema that creates the object) and is a special implementation of the "factory method" pattern.
Typically, we use a simple factory pattern to create a class. For example, getting a thread pool object is done through a simple factory pattern. Its structure diagram looks like this:

Factory: The factory is the core of the simple factory model, providing the external interface. The client or other program acquires the product object through the factory interface.
Abstract Products: Abstract products are (many) abstracted from different products. Product can be an interface or an abstract class.
Product: The Product object returned in the factory is actually created by Concreteproduct.

2. Simple factory Model 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. The practical application of the simple factory model

When we write a servlet to handle client requests, a servlet often handles multiple business logic, such as:

protected void DoPost (HttpServletRequest request, httpservletresponse response) {
 String flag = Request.getparameter ("flag");
 if (Flag.equals ("Service1")) {
  Service1 ();
 } else if (flag.equals ("Service2")) {
  service2 ();
 }
 ...
}

These are the usual ways for our servlet to handle multiple-business logic, to write a lump if else statement. A better approach would be to detach the requested distribution from the servlet and have the servlet handle only the business logic. We regard the various requested servlet as the product class, Javax.servlet.HttpServlet is the product parent class, Javax.servlet.Servlet is the product interface, so we define a servletfactory, Resolve the URL request in the filter and give it to servletfactory to handle it. 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, where we agreed to request all/servletname form
  String names[] = Myrequest.getrequesturi (). Trim (). Split ("/");
  Servletname = names[2];
  if (servletname!= null) {
  //below is the typical example of two simple factories the
   servlet servlet = Servletfactory.createservlet (servletname); C13/>servlet.service (request, response);
  else
   Chain.dofilter (request, response);
 

Every time we come up with a request, we use a factory to produce a servlet, which eliminates the need to configure a large number of Servlet path information in XML. It also makes the logic clearer, and the servlet simply deals with the business at the business level.
The factory class is 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");}}

The above factory class does not throw away the tedious if else, but the idea of using a simple factory still solves some problems. A simple factory is a very simple design pattern that is not a design pattern, and the problem is limited. The above request for distribution of the large Java EE frameworks has been implemented, such as STRUTS2, of course, the framework is not a simple factory.

4. Summary

Simple factory model, summed up is a factory class, a product interface (in fact, it can also be an abstract class, or even an ordinary parent) and a group of products that implement the product interface, and this factory class, according to the incoming parameters to create a specific implementation class, and upward transition to the interface as a result of return.

Related Article

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.