SoFunction
Updated on 2025-04-05

Detailed explanation of the factory method model example of Android programming design mode

This article describes the factory method mode of Android programming design pattern. Share it for your reference, as follows:

1. Introduction

Factory Pattern is one of the creative design patterns. The factory method model is a simple structure model. It is widely used in our daily development. Maybe you don’t know it, but you have used this model countless times, such as the life cycle methods in the Activity in Android. Taking the onCreate method as an example, it can be regarded as a factory method. In it, we can construct our View and return it to the framework through the setContentView. Let’s talk about the related content below. Let’s take a look at the factory method model definition first.

2. Definition

Define an interface for creating objects, letting the subclass decide which class to instantiate.

3. Use scenarios

Wherever complex objects are needed, factory method patterns can be used. Complex objects are suitable for using factory mode. You can use new to complete the created objects without using factory mode.

4. Simple implementation of the mode

Abstract product category:

public abstract class Product {
  /**
    * Abstract method of product categories
    * Implemented by specific product categories
    * */
  public abstract void method();
}

Specific product category A:

public class ConcreteProductA extends Product {
  @Override
  public void method() {
    ("I'm the specific product A");
  }
}

Specific product category B:

public class ConcreteProductB extends Product {
  @Override
  public void method() {
    ("I'm the specific product B");
  }
}

Abstract factory class:

public abstract class Factory {
  /**
    * Abstract factory method
    * It is implemented specifically by subclasses
    *
    * @return Specific product objects
    * */
  public abstract Product createProduct();
}

Specific factory categories:

public class ConcreteFactory extends Factory {
  /**
    * Specific factory category
    * */
  @Override
  public Product createProduct() {
    return new ConcreteProductA();
  }
}

Client category:

public class Client {
  public static void main(String[] args) {
    Factory factory = new ConcreteFactory();
    Product product = ();
    ();
  }
}

result:

I'm a specific productA

The roles here are very simple, mainly divided into four major modules. One is the abstract factory, which is the core of the factory method model; the second is the specific factory, which realizes the specific business logic; the third is the abstract product, which is the parent class of the products created by the factory method model; the fourth is the concrete product, which is the object of a specific product that realizes the abstract product.

In the above code, we construct a factory object in the Client class and produce a product object through it. The product object we get here is an instance of ConcreteProductA. If you want to get an instance of ConcreteProductB, you can change the logic in ConcreteFactory:

public class ConcreteFactory extends Factory {
  /**
    * Specific factory category
    * */
  @Override
  public Product createProduct() {
    //return new ConcreteProductA();
    return new ConcreteProductB();
  }
}

This method is more common. You can produce which product you need. Sometimes you can use reflection to produce specific product objects more concisely. At this time, you need to pass a Class class into the parameter list of the factory method to decide which product class it is:

public abstract class Factory {
  /**
    * Abstract factory method
    * It is implemented specifically by subclasses
    *
    * @param clz Product object class type
    *
    * @return Specific product objects
    * */
  public abstract <T extends Product> T createProduct(Class<T> clz);
}

For specific factory classes, you can get the class example by reflection:

public class ConcreteFactory extends Factory {
  /**
    * Specific factory category
    * */
  @SuppressWarnings("unchecked")
  @Override
  public <T extends Product> T createProduct(Class<T> clz) {
    Product product = null;
    try {
      product = (Product) (()).newInstance();
    } catch (Exception e) {
      ();
    }
    return (T)product;
  }
}

Finally, let’s take a look at the implementation in Client:

public class Client {
  public static void main(String[] args) {
    Factory factory = new ConcreteFactory();
    Product product = ();
    ();
  }
}

You can just pass in which type of object you need. This method is simple and dynamic. If you don’t like this method, you can also try to define a specific factory for each product and perform your own duties.

public class ConcreteFactoryA extends Factory {
  /**
    * Specific factory category
    **/
  @Override
  public Product createProduct() {
    return new ConcreteProductA();
  }
}
public class ConcreteFactoryB extends Factory {
  /**
    * Specific factory category
    **/
  @Override
  public Product createProduct() {
    return new ConcreteProductB();
  }
}
public class Client {
  public static void main(String[] args) {
    Factory factoryA = new ConcreteFactoryA();
    Product productA = ();
    ();
    Factory factoryB = new ConcreteFactoryB();
    Product productB = ();
    ();
  }
}

The way to have multiple factories like this is called the multi-factory method model. Similarly, back to our original factory method model, when we only had one factory, we still provided an abstract class for the factory. So, can we simplify it? If you are sure that there is only one factory class, then it is definitely no problem to simplify the abstract class. We just need to change the corresponding factory method to a static method:

public class Factory {
  /**
    * Specific factory category
    **/
  @Override
  public static Product createProduct() {
    return new ConcreteProductA();
  }
}

A method like this is also called the Simple Factory Mode or the Static Factory Mode, which is a weakened version of the Factory Method Mode.

In fact, at this point, you should be able to find that the factory method pattern is completely in line with the design principles, which reduces the coupling between objects. Moreover, the factory method pattern relies on an abstract architecture, which hand over the instantiated tasks to subclasses, which has very good scalability.

5. Factory method mode in Android source code

Various life cycles of activities

ArrayList and HashSet

6. Summary

advantage:

The factory method pattern fully complies with the design principles and reduces coupling between objects. High-level modules only need to know the product's abstract classes, and no other implementations need to be cared about.

Good encapsulation and clear code structure. Good scalability.

shortcoming:

Every time we add a new product to the factory method model, we need to write a new product category. At the same time, an abstraction layer must be introduced, which will inevitably lead to the complexity of the class structure. Therefore, in some cases, whether to use the factory pattern needs to be weighed by the pros and cons.

For more information about Android related content, please check out the topic of this site:Android development introduction and advanced tutorial》、《Android debugging skills and solutions to common problems》、《Summary of the usage of basic Android components》、《Android View View Tips Summary》、《Android layout layout tips summary"and"Android control usage summary

I hope this article will be helpful to everyone's Android programming design.