introduce

Factory Pattern is one of the creative design patterns. The factory method pattern is a simple pattern that is widely used in development. You may not know it, but you have used it countless times. For example, the Activity lifecycle method in Android, the onCreate method, can be considered a factory method. Where we can construct our View and return it to the Framework via setContentView.

define

Define an interface for creating objects and let subclasses decide which class to instantiate.

Usage scenarios

The factory method pattern can be used anywhere complex objects need to be generated. Complex objects are suitable for the Factory pattern, and objects that can be created with new do not need to use Factory.

Factory pattern code sample

Requirements: Match different service modules based on user permissions.

Define an abstract business module class

public abstract class IModule {

    /** * Get function modules according to different permissions *@param permission
     * @return* /
    public abstract List<IFunctionModule> getMoudleFun(String permission);
}

Copy the code

An implementation class for a specific function

public class PlayMusicImpl implements IFunctionModule {
    private String TAG = getClass().getSimpleName();
    @Override
    public void function(a) {
        Log.i(TAG,"Play the music"); }}Copy the code
public class PlayVideoImpl implements IFunctionModule {


    public  String TAG = getClass().getSimpleName();

    @Override
    public void function(a) {
        Log.i(TAG,"Play video"); }}Copy the code
public class NewsImpl implements IFunctionModule {

    private String TAG = getClass().getSimpleName();

    @Override
    public void function(a) {
        Log.i(TAG, "Watch the news"); }}Copy the code

The specific functional module factory starts to produce the required module

public class FunModuleFactor extends IModule {


    @Override
public List<IFunctionModule> getMoudleFun(String permission) {
    switch (permission) {
        case "VIP":
            List<IFunctionModule> vipLists = new ArrayList<>();
            vipLists.add(new PlayMusicImpl());
            vipLists.add(new PlayVideoImpl());
            vipLists.add(new NewsImpl());
          return vipLists;
         default:
           List<IFunctionModule> kipLists = new ArrayList<>();
           kipLists.add(new PlayMusicImpl());
           kipLists.add(new NewsImpl());
           returnkipLists; }}}Copy the code

Use:

    @Test
    public void test7(a) {
        FunModuleFactor funModuleFactor = new FunModuleFactor();
        List<IFunctionModule> vip = funModuleFactor.getMoudleFun("VIP");
        System.out.println("VIP:" + vip.size());

        List<IFunctionModule> kip = funModuleFactor.getMoudleFun("KIP");
        System.out.println("KIP:" + kip.size());

    }
Copy the code

Output:

VIP:3
KIP:2
Copy the code

According to the code above, we define an abstract module class IMoudle. FunModuleFactory implements IModule, and external services can be assembled with different permissions. And that’s how it works.

conclusion

In general, factory method pattern is a good design patterns, but also has faults, every time we for the factory method pattern to add a new module to write a new module class, abstraction, introduced at the same time, it will inevitably lead to class structure complicated, so, in some cases is simpler, whether to use the factory pattern, the need to weigh the pros and cons.

Article code address

Special thanks to

Android source code design pattern analysis and combat