Android Design Pattern series (8)-factory method pattern of SDK source code

Source: Internet
Author: User

The factory method mode is often the beginner's Entry Mode of the design mode. Indeed, some people call it the most typical and most enlightening mode.
There are too many factory classes in Android, which use the factory method mode. Of course, many factories do not use the factory method mode, but only the tool management class.
Today, threadfactory is used as an example to illustrate the simple factory mode and factory method mode.
Factory method mode, factory method, simple method, not simple application.

1. Intention
Defines an interface used to create objects, so that the subclass determines which class to instantiate. The factory mode delays the instantiation of a class to its subclass.
Popular Words:The virtual constructor delays creating object subclasses. 

2. structure chart andCode
Let's first look at the structure of the standard factory method:


Abstract product classes, abstract factory classes, and then use the specific factory of the client to produce specific products, but the client does not know how the specific products are produced, the production process is encapsulated in the factory. Therefore, to some extent, the factory method pattern changes the way we create objects directly using new. This is a good start and is of great significance.
Take threadfactory as an example:

This figure is actually slightly different from the original structure, that is, the factory is parameterized, And it is somewhat different in the business sense, but the idea is the same.
Let's take a look at the specific code:

 
// Abstract product public interface runnable {public abstract void run ();} // Abstract Factory public interface threadfactory {thread newthread (runnable R );}

The specific implementation is as follows:
For example, the factory implementation in the asynctask class is as follows:

// Factory implementation class Private Static final threadfactory sthreadfactory = new threadfactory () {private final atomicinteger mcount = new atomicinteger (1); public thread newthread (runnable R) {return New thread (R, "asynctask #" + mcount. getandincrement () ;}}; // Where is the product category? // As the parameter runnable R, we can create thousands of product categories in this series. // similarly, we can create another similar factory to produce a special thread, which is very easy to expand.

We can see that, on the one hand, we lament the convenience of its production, and on the other hand, it is complicated to create a factory for the creation of a certain type of products, so we will introduce the simple factory below. Its structure is as follows:


A simple factory removes the abstract factory and you just need to create a special product. It is very practical and convenient to apply this mode in some specific but irresponsible fields.
This class is used in the connection class in Android:


The abstract class "connection" serves both the abstract product class and the specific factory class.
In this case, we often need to produce subclass immediately. The getconnection method is usually static, so a simple factory is also called a static factory method.
The Code is as follows:

Abstract class connection {static connection getconnection (context, httphost host, httphost proxy, requestfeeder) {If (host. getschemename (). equals ("HTTP") {return New httpconnection (context, host, requestfeeder);} // otherwise, default to HTTPS return New httpsconnection (context, host, proxy, requestfeeder );}}

This is a simple factory, a very simple Parameterized Factory.

3. Results
1. creation mode;
2. obtain the corresponding object in the Parameterized Factory method mode;
3. Provide hooks for child classes;
4. Connect parallel class layers.

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.