Android design mode Series-factory method mode

Source: Internet
Author: User

The factory method pattern is often the model for beginners of design patterns, and indeed, some call it the most typical and inspiring model.
There are too many factory classes in Android, which are useful for factory methods, and of course many factories do not use factory method patterns, just tools management classes.
The simple factory model and the factory method model are illustrated today with Threadfactory.
Factory method Mode, Factory methods, simple way, not simple application.

1. Intentions
Defines an interface that is used to create an object so that the subclass decides which class to instantiate. The Factory mode pattern defers the instantiation of a class to its subclasses.
Popular words: fiction maker delay creating object subclasses

2. Structure diagram and code
Let's take a look at the standard factory method structure diagram:


First abstract product class, abstract factory class, and then use the client-specific factory to produce the corresponding specific products, but the client does not know how the specific product is produced, the process of production encapsulated in the factory. So, to some extent, the factory approach mode has changed the way we create objects directly with new, a good start and a big point.
Take Threadfactory as an example:

This picture is actually slightly different from the original structure, that is, the parameterization of the factory, but also in terms of business sense, but the idea is the same.
Let's look at the specific code:

    1. Abstract Products
    2. Public interface Runnable {
    3. public abstract Void run ();
    4. }
    5. Abstract Factory
    6. Public interface Threadfactory {
    7. Thread Newthread (Runnable R);
    8. }

Here are the specific implementations:
For example, the specific implementation of the factory in the Asynctask class is as follows:

  1. Factory Implementation Class
  2. Private static final threadfactory sthreadfactory = new Threadfactory () {
  3. Private final Atomicinteger mCount = new Atomicinteger (1);
  4. Public Thread Newthread (Runnable r) {
  5. return New Thread (R, "Asynctask #" + mcount.getandincrement ());
  6. }
  7. };
  8. So where is the product class?
  9. As a parameter runnable R, we can create tens of thousands of this series of product classes
  10. Similarly, we can create other similar factories, produce some kind of specialized thread, very easy to expand

See here, we on the one hand for its production convenience, on the one hand and do not create a certain kind of products are to create a factory and feel cumbersome, so we introduce the simple factory, its structure diagram is as follows:


Simple factory Remove the abstract factory, you create a specialized production of a certain kind of product is good. It is very practical and convenient to apply this mode in some specific but not responsible areas.
This class is used in the connection class in Android:


Connection this abstract class, which serves both as an abstract product class and as a specific factory class.
Because of this situation, what we often need is to produce subclasses immediately, the Getconnection method is often static, so a simple factory, also called a static factory method.
Let's look at the code as follows:

  1. Abstract class connection{
  2. static Connection getconnection (
  3. Context context, Httphost host, Httphost proxy,
  4. Requestfeeder Requestfeeder) {
  5. if (Host.getschemename (). Equals ("http")) {
  6. return New Httpconnection (context, host, requestfeeder);
  7. }
  8. //Otherwise, default to HTTPS
  9. return New Httpsconnection (context, host, Proxy, requestfeeder);
  10. }
  11. }

This is the simple factory, a very simple parameter chemical factory, really simple.

3. Effects
1. Create model;
2. Parametric chemical Plant method model to get the corresponding object;
3. Provide hooks for subclasses;
4. Connect the parallel class hierarchy.

Android design mode Series-factory method mode

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.