Creational pattern-factory method pattern understanding

Table of contents

1 Introduction:

2. Factory method demo optimization

3. Advantages and disadvantages


1 Introduction:

The previous article wrote about creational mode - simple factory mode demo understanding ,

One disadvantage of the simple factory model is that the if and else if code fragments for creating objects are brought into the factory class. If you add an indicator for analysis later, you need to rewrite and add an else if code in the factory class Fragment, which violates the principle of opening and closing.

How to optimize this shortcoming, you can use the factory method pattern to optimize, the simple point is, add subclasses under the factory class, and different index classes implement different APIs for creating objects and implementing calculations.

2. Factory method demo optimization

It is mainly to optimize the interface layer of the factory class,

//抽象接口:情景分析接口层
interface Analysis {
    public void calculate();
}

//实现层:指标a分析
class AnalysisIndexA implements Analysis {
    public AnalysisIndexA() {
        System.out.println("指标a分析!");
    }

    public void calculate() {
        System.out.println("指标a计算完成!");
    }
}

//实现层:指标b分析
class AnalysisIndexB implements Analysis {
    public AnalysisIndexB() {
        System.out.println("指标b分析!");
    }

    public void calculate() {
        System.out.println("指标b计算完成!");
    }
}

//工厂类
interface analysisFactory {
    //工厂类只负责抽象一个接口
    public Analysis factoryMethod();

}
//指标a工厂类
class AnalysisIndexAFactory implements analysisFactory {
    public Analysis factoryMethod() {
        return new AnalysisIndexA();
    }
}

//指标a工厂类
class AnalysisIndexBFactory implements analysisFactory {
    public Analysis factoryMethod() {
        return new AnalysisIndexB();
    }
}


class Client {
    public static void main(String args[]) {
        //
        analysisFactory factory;
        factory = new AnalysisIndexAFactory();
        Analysis analysis;
        //通过静态工厂方法引用不同指标对象
        analysis = factory.factoryMethod();
        //开始进行指标计算
        analysis.calculate();
    }
}

3. Advantages and disadvantages

1. Main advantages

       The main advantages of the factory method pattern are as follows:

       (1) Hide creation details from the caller

       (2) When adding new indicator calculations, there is no need to modify the interfaces provided by abstract factories and abstract products, nor to modify the client, nor to modify other specific factories and specific products, but only need to add a specific indicator factory and a specific indicator class. , In this way, the scalability of the system becomes very good, which fully complies with the "open and close principle".

  2. Main disadvantages

     The main disadvantages of the factory method pattern are as follows:

      (1) As the number of new indicators increases, the number of classes also increases, increasing resource overhead


 

Guess you like

Origin blog.csdn.net/qq_44691484/article/details/130527670