Android Design Patterns - Simple Factory Patterns

Preface

Android Design Patterns series articles, welcome attention, ongoing updates:

1. definition

Define an interface for creating objects so that subclasses decide which class to instantiate.

2. introduction

  • Simple factory mode belongs to creation mode.
  • Simple factory mode is also called static factory method mode.

3.UML class diagram

3.1 Role Description:

  • Product (abstract product class): To create complex objects, define the common interface of objects.
  • ConcreteProduct (Specific Product Class): Implement the Product interface.
  • Factory (factory class): Returns the ConcreteProduct instance.

4. implementation

4.1 Create abstract product classes and define common interfaces:

    //Abstract product class 
    public abstract class Product {
        public abstract void show();
    }

4.2 Create specific product classes and inherit Product classes:

    //Specific Product Category A 
    public class ProductA extends Product {
        @Override
        public void show() {
            System.out.println("product A");
        }
    }
    //Specific Product Category B
    public class ProductB extends Product {
        @Override
        public void show() {
            System.out.println("product B");
        }
    }

4.3 Create factory classes and specific products:

public class Factory {

    public static Product create(String productName) {
        Product product = null;
        //Control which commodity to produce by switch statement
        switch (productName) {
            case "A":
                product = new ProductA();
                break;
            case "B":
                product = new ProductB();
                break;
        }
        return product;
    }
}

4.4 Test methods:

   public void test() {
        Factory.create("A").show();//Production of ProductA
        Factory.create("B").show();//Production of Product B
        try {
            Factory.create("C").show();//Production of Product C
        } catch (NullPointerException e) {
            System.out.println("No, ProductC");//Without ProductC, errors will be reported.
        }
    }

5. Application scenarios

  • When generating complex objects, determine that there is only one factory class, and you can use the simple factory pattern. Otherwise, if there are more than one factory class, use Factory Method Model.

6. advantages

  • Code decoupling, the creation of instances is separated from the use of instances, users do not need to care about how to create class objects.

7. disadvantages

  • In violation of the open and closed principle, if new products need to be added, the factory logic must be modified, which will cause the factory logic to be too complex.
  • Simple factory mode uses static factory method, so static method can not be inherited and rewritten.
  • The factory class contains the creation logic of all instances (products). If the factory class fails, the whole system will be affected.

8. Comparisons between Factory Method Model and Simple Factory Model

  • The factory method pattern has an abstract factory class, while the simple factory pattern has no abstract factory class, and the factory method of the factory class is static.
  • The factory method mode only needs to build a new factory class when adding new products, which conforms to the principle of open and closed, while the simple factory mode needs to modify the factory class directly, which violates the principle of open and closed.

9. Optimizing Simple Factory Model

It violates the principle of openness and closure because of the need to modify the factory class directly when adding new products in the simple factory model. Therefore, reflection can be used to create instance objects to ensure that the principle of openness and closure is followed.

9.1 Reflective Implementation Factory Class

public class Factory {

    public static <T extends Product> T create(Class<T> clz) {
        Product product = null;
        try {
            product = (Product) Class.forName(clz.getName()).newInstance();//Reflect examples
        } catch (Exception e) {
            e.printStackTrace();
        }
        return (T) product;
    }
}

9.2 Test Method

    public void test() {
        Factory.create(ProductA.class).show();//Production of ProductA
        Factory.create(ProductB.class).show();//Production of Product B
    }

9.3 summary
Reflections are used to implement factory classes. There is no need to modify factory classes when adding new products, but creating instance objects using reflection is slower than using new normally.

Keywords: Android

Added by zyrolasting on Fri, 17 May 2019 23:05:06 +0300