当前位置:网站首页>[detailed explanation of factory mode] factory method mode

[detailed explanation of factory mode] factory method mode

2022-04-23 14:35:00 Vivien_ oO0

Factory mode

I've already introduced Simple factory model
This paper mainly introduces

  • Factory method model

Factory method model

The disadvantage of the simple factory model is that the responsibilities of the factory class are relatively heavy , It is not easy to expand complex product structure .

Factory method model (Fatory Method Pattern) It refers to defining an interface for creating objects , But let the class that implements this interface decide which class to instantiate , The factory method pattern postpones the instantiation of a class to a subclass . In the factory method mode, users only need to care about the factory corresponding to the required product , Don't care about the details of the creation , And the principle of compound opening and closing when adding careful products .

The factory method mode mainly solves the problem of product expansion . In simple factory mode , With the enrichment of product chain , If the creation logic of each course is different , Then the responsibilities of the factory will become more and more , It's kind of like a universal factory , Not easy to maintain . According to the principle of single responsibility, we will continue to split our functions , Someone to do special work .Java The course consists of Java The factory to create ,Python The course consists of Python The factory to create , Make an abstraction of the factory itself . Look at code :
ICourse Code :

public interface ICourse {
    
     void record();// Recording lessons 
}

JavaCourse Code :

public class JavaCourse implements ICourse {
    
    @Override
    public void record() {
    
        System.out.println(" Recording java Course ");
    }
}

PythonCourse Code :

public class PythonCourse implements ICourse {
    
    @Override
    public void record() {
    
        System.out.println(" Recording python Course ");
    }
}

ICourseFactory Interface :

public interface ICourseFactory {
    
    ICourse create();
}

Then create factories separately ,JavaCourseFactory

public class JavaCourseFactory implements ICourseFactory {
    
    @Override
    public ICourse create() {
    
        return new JavaCourse();
    }
}

PythonCourseFactory Code :

public class PythonCourseFactory implements ICourseFactory {
    
    @Override
    public ICourse create() {
    
        return new PythonCourse();
    }
}

The test code is as follows :

public class Test {
    
    public static void main(String[] args) {
    
        ICourseFactory factory = new PythonCourseFactory();
        ICourse course = factory.create();
        course.record();
        
        factory = new JavaCourseFactory();
        course = factory.create();
        course.record();
    }
}

result :
 Insert picture description here
Let's look at the class diagram :

 Insert picture description here
You can compare simple communism to see the difference , The beginning of the connection has given .

The factory method pattern applies to the following scenarios :

  1. Creating objects requires a lot of repetitive code
  2. The application layer does not depend on how product class instances are created 、 How to implement details
  3. A class specifies which object to create through its subclasses

Disadvantages of factory method pattern :

  1. It's easy to have too many classes , Added complexity
  2. It increases the abstraction and understanding difficulty of the system

版权声明
本文为[Vivien_ oO0]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231432380186.html

随机推荐