当前位置:网站首页>Thorough and thorough analysis of factory method mode

Thorough and thorough analysis of factory method mode

2022-06-24 01:48:00 Tom bomb architecture

This article is excerpted from 《 This is how design patterns should be learned 》

1 Application scenarios of the factory method pattern

The factory method mode is mainly applicable to the following application scenarios .

(1) Creating objects requires a lot of repetitive code .

(2) client ( application layer ) Does not depend on how product class instances are created 、 Implementation details .

(3) A class uses its subclasses to specify which object to create .

2 Factory method mode UML Class diagram

Factory method mode UML The class diagram is shown in the figure below .

file

As can be seen from the picture above , Abstract factory pattern mainly includes 4 A character .

(1) Abstract factory (Factory): It's the core of the factory approach pattern , It's not about the application . Any factory class of an object created in a pattern must implement this interface .

(2) Specific factory (Concrete Factory): Is a concrete factory class that implements the abstract factory interface , Contains logic that is closely related to the application , And is called by the application to create the product object .

(3) Abstract product (Product): Is the supertype of the object created by the factory method pattern , That is, the common parent class or the common interface of the product object .

(4) Specific products (Concrete Product): This role implements the interface defined by the abstract product role . A specific product has a specific factory , They often correspond to each other .

3 A common way to write factory method patterns

The following is a general description of the factory method pattern .

public class Client {
    public static void main(String[] args) {
        IFactory factory = new FactoryA();
        factory.makeProduct().doSomething();

        factory = new FactoryB();
        factory.makeProduct().doSomething();

        factory = new FactoryC();
        factory.makeProduct().doSomething();
    }

    // Abstract factory 
    public interface IFactory {
        IProduct makeProduct();
    }
    // production ProductA Specific factory class 
    static class FactoryA implements IFactory {
        public IProduct makeProduct() {
            return new ProductA();
        }
    }
    // production ProductB Specific factory class 
    static class FactoryB implements IFactory {
        public IProduct makeProduct() {
            return new ProductB();
        }
    }

    // production ProductC Specific factory class 
    static class FactoryC implements IFactory {
        public IProduct makeProduct() {
            return new ProductC();
        }
    }


    // Abstract product 
    public interface IProduct {
        void doSomething();
    }

    // Specific products :ProductA
    static class ProductA implements IProduct {
        public void doSomething() {
            System.out.println("I am Product A");
        }
    }

    // Specific products :ProductB
    static class ProductB extends FactoryB implements IProduct {
        public void doSomething() {
            System.out.println("I am Product B");
        }
    }

    // Specific products :ProductC
    static class ProductC implements IProduct {
        public void doSomething() {
            System.out.println("I am Product C");
        }
    }
}

4 Implement product extension using factory method pattern

The factory method mode mainly solves the problem of product expansion , In a simple factory , 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 single responsibility principle , 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 , Abstract the factory itself . First create ICourseFactory Interface .

public interface ICourseFactory {
    ICourse create();
}

Then create sub factories ,JavaCourseFactory The code for the class is as follows .

import com.tom.pattern.factory.ICourse;
import com.tom.pattern.factory.JavaCourse;

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

PythonCourseFactory The code for the class is as follows .

import com.tom.pattern.factory.ICourse;
import com.tom.pattern.factory.PythonCourse;

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

The client test code is as follows .

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

    factory = new JavaCourseFactory();
    course = factory.create();
    course.record();
}

Finally, look at the class diagram shown in the figure below .

file

5 The factory approach pattern is in Logback Application in source code

Look at Logback The application of factory method pattern in , The class diagram is shown in the following figure .

file

As can be seen from the picture above , Different factories have been separated to create different logging frameworks , Such as Substitute Log framework 、NOP Log framework 、Log4J Log framework , So the corresponding Logger So is the product system , As shown in the figure below .

file

Pay attention to WeChat public number 『 Tom Bomb architecture 』 reply “ Design patterns ” The complete source code is available .

【 recommend 】Tom Bomb architecture :30 A real case of design pattern ( Source code attached ), Challenge annual salary 60W Is not a dream

This paper is about “Tom Bomb architecture ” original , Reprint please indicate the source . Technology is about sharing , I share my happiness !undefined If this article helps you , Welcome to pay attention and like ; If you have any suggestions, you can also leave comments or private letters , Your support is the driving force for me to adhere to my creation . Pay attention to WeChat public number 『 Tom Bomb architecture 』 More technical dry goods are available !

原网站

版权声明
本文为[Tom bomb architecture]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/11/20211111135503541l.html