Design Pattern---Factory Method Pattern

1 Definition

The factory method pattern defines an interface for user-created objects, and lets subclasses that inherit the interface decide to instantiate a class. A factory method delays the instantiation of a class to its subclasses.

2 Factory Method Pattern VS Simple Factory Pattern

First put a link to a simple factory pattern I wrote . The factory class in the simple factory pattern contains the necessary logic, and the factory class generates different class instances according to the logic judgment. But to create a class instance, we need to modify the code of the simple factory class and add logical judgment, which is equivalent to opening up the extension of the function and the modification of the code, which violates the open and closed principle.

The factory method pattern transfers the logical judgment to the client, and selects different factories in the client to obtain the instantiated object. When we need to add the creation of an instantiated object, we only need to add a class that inherits the factory interface to create the instantiated object.

The disadvantage of the factory method pattern is that many classes will be added. If there are many instantiated objects, each object needs a factory class to specially create the corresponding object, resulting in a large number of classes and increasing the difficulty of management.

3 Factory Method Pattern Structure Diagram

The following figure is the structure diagram of the factory method pattern (from "Dahua Design Patterns").

4 Code Examples

The code display business is the same as the business in the simple factory mode . Please click the link to view the specific business introduction.

4.1 Mapper of the object interface that the factory method needs to create

/**
 * @program: design-pattern-learning
 * @author: zgr
 * @create: 2021-09-01 16:24
 **/
public interface Mapper<T> {

    /**
     * 存储实体到数据库
     *
     * @param entity 存储的实体
     */
    void saveEntity(T entity);
}

4.2 Merchant Review mapper

/**
 * @program: design-pattern-learning
 * @author: zgr
 * @create: 2021-09-01 16:26
 **/
public class MerchantCommentMapper implements Mapper<MerchantComment> {

    @Override
    public void saveEntity(MerchantComment merchantComment) {
        System.out.println("存储的是审核商户回复内容," + merchantComment.getMerchantName());
    }
}

4.3 User comment mapper

/**
 * @program: design-pattern-learning
 * @author: zgr
 * @create: 2021-09-01 16:26
 **/
public class UserCommentMapper implements Mapper<UserComment> {

    @Override
    public void saveEntity(UserComment userComment) {
        System.out.println("存储的是审核用户评论内容," + userComment.getUserName());
    }
}

 4.4 CommonComment of the object instance that the factory method needs to create

/**
 * @program: design-pattern-learning
 * @author: zgr
 * @create: 2021-09-01 16:28
 **/
@Data
public class CommonComment {

    private Integer id;

    private String commentContent;

    private String commentResult;
}

4.5 Merchant Comment Model MerchantComment

@Data
public class MerchantComment extends CommonComment {

    private String merchantName;
}

4.6 User Comment Model UserComment

/**
 * @program: design-pattern-learning
 * @author: zgr
 * @create: 2021-09-01 16:29
 **/
@Data
public class UserComment extends CommonComment {

    private String userName;
}

4.7 Create Mapper's factory interface MapperFactory

/**
 * @program: design-pattern-learning
 * @author: zgr
 * @create: 2021-09-10 10:31
 **/
public interface MapperFactory {

    /**
     * 创建mapper实例
     * @return mapper实例
     */
    Mapper createMapper();
}

4.8 Create a factory UserCommentMapperFactory for user comment mapper

/**
 * @program: design-pattern-learning
 * @author: zgr
 * @create: 2021-09-10 10:36
 **/
public class UserCommentMapperFactory implements MapperFactory {
    @Override
    public Mapper createMapper() {
        return new UserCommentMapper();
    }
}

4.9 Create MerchantCommentMapperFactory for MerchantCommentMapperFactory

/**
 * @program: design-pattern-learning
 * @author: zgr
 * @create: 2021-09-10 10:34
 **/
public class MerchantCommentMapperFactory implements MapperFactory {

    @Override
    public Mapper createMapper() {
        return new MerchantCommentMapper();
    }
}

4.10 The factory interface CommentFactory that creates a comment model instance

/**
 * @program: design-pattern-learning
 * @author: zgr
 * @create: 2021-09-10 10:40
 **/
public interface CommentFactory {

    /**
     * 创建评论对象实例
     * @return 评论对象实例
     */
    CommonComment createComment();
}

4.11 UserCommentFactory, the factory that creates the user comment model

/**
 * @program: design-pattern-learning
 * @author: zgr
 * @create: 2021-09-10 10:42
 **/
public class UserCommentFactory implements CommentFactory{
    @Override
    public CommonComment createComment() {
        UserComment userComment = new UserComment();
        userComment.setId(1);
        userComment.setCommentContent("用户评论类容");
        userComment.setCommentResult("用户评论审核结果");
        userComment.setUserName("hello,我是用户评论");
        return userComment;
    }
}

4.12 The MerchantCommentFactory that creates the merchant comment model

/**
 * @program: design-pattern-learning
 * @author: zgr
 * @create: 2021-09-10 10:42
 **/
public class MerchantCommentFactory implements CommentFactory{
    @Override
    public CommonComment createComment() {
        MerchantComment merchantComment = new MerchantComment();
        merchantComment.setId(1);
        merchantComment.setCommentContent("商户评论类容");
        merchantComment.setCommentResult("商户评论审核结果");
        merchantComment.setMerchantName("hello,我是商户评论");
        return merchantComment;
    }
}

4.13 Main program

/**
 * @program: design-pattern-learning
 * @author: zgr
 * @create: 2021-09-10 10:29
 **/
public class MainClass {
    public static void main(String[] args) {
        boolean flag = true;
        while (flag) {
            //模拟接口参数
            Scanner sc = new Scanner(System.in);
            String identity = sc.nextLine();
            if (identity.equals("exit")) {
                System.out.println("退出系统");
                flag = false;
            } else {
                //在客户端代码添加逻辑判断
                if (identity.equals("user")){
                    Mapper mapper = new UserCommentMapperFactory().createMapper();
                    mapper.saveEntity(new UserCommentFactory().createComment());
                }else if (identity.equals("merchant")){
                    Mapper mapper = new MerchantCommentMapperFactory().createMapper();
                    mapper.saveEntity(new MerchantCommentFactory().createComment());
                }else {
                    System.out.println("身份标识参数输入错误");
                    continue;
                }
            }
        }
    }
}

4.14 Running Results

5 Summary

The definition and code examples of the factory method pattern were introduced earlier. As can be seen from the code example in Section 4, the factory method has many more factory classes that create class object instances, which increases the difficulty of management.

6 Quotes

"Big Talk Design Patterns"

7 Source code

design-pattern-learning/src/com/hz/design/pattern/factory/method at main · airhonor/design-pattern-learning · GitHub

Guess you like

Origin blog.csdn.net/honor_zhang/article/details/120217725