1. Introduction

1) The simple factory mode belongs to the creation mode, which is a kind of factory mode. The simple factory pattern is one in which a factory object decides which instance of a product class to create. Simple factory pattern is the most simple and practical model in the factory model family 2) simple factory pattern: defines a class for creating an object, by the behavior of the objects to encapsulate instantiate this class (code) 3) in software development, when we will be using a lot of creating some, some, or a group of objects, will use the factory pattern

Example 2.

Instance demand

  • Robot production projects: to facilitate the expansion of robot types, to facilitate maintenance
  • The types of robots are divided into: robot generation, robot generation and so on
  • Fulfill robot production requirements

Simple factory mode code

  • Robot abstract class, all Robot production steps

    public abstract class Robot {
    
        private String name;
    
        public Robot(String name) {
            this.name = name;
        }
    
        /** * Prepare materials */
        public abstract void ready(a);
    
        public void create(a) {
            ready();
            head();
            body();
            hand();
            foot();
        }
    
        protected void head(a) {
            System.out.println(name + "Head");
        }
    
        protected void body(a) {
            System.out.println(name + "Body");
        }
    
        protected void hand(a) {
            System.out.println(name + "Hand");
        }
    
        protected void foot(a) {
            System.out.println(name + "Foot"); }}Copy the code
  • RobotOne first generation robot production

    public class RobotOne extends Robot {
        public RobotOne(String name) {
            super(name);
        }
    
        @Override
        public void ready(a) {
            System.out.println("Robot Generation Production"); }}Copy the code
  • RobotTwo second generation robot production

    public class RobotTwo extends Robot {
        public RobotTwo(String name) {
            super(name);
        }
    
        @Override
        public void ready(a) {
            System.out.println("Robot Ii Production"); }}Copy the code
  • RobotFactory is a robot manufacturing factory

    public class RobotFactory {
    
        public RobotFactory(a) {}public Robot createRobot(String type) {
            Robot robot = null;
            if (type.equalsIgnoreCase("one")) {
                robot = new RobotOne("First generation");
            } else if (type.equalsIgnoreCase("two")) {
                robot = new RobotTwo("Second generation");
            }
            returnrobot; }}Copy the code
  • Client produces different robots according to different types

    public class Client {
    
        public static void main(String[] args) {
            RobotFactory robotFactory = new RobotFactory();
            while (true) {
                String type = getType();
                Robot robot = robotFactory.createRobot(type);
                if(robot ! =null) {
                    robot.create();
                } else {
                    System.exit(0); }}}public static String getType(a) {
            try {
                BufferedReader strin = new BufferedReader(new InputStreamReader(System.in));
                System.out.println("input type:");
                String str = strin.readLine();
                return str;
            } catch (IOException e) {
                e.printStackTrace();
                return ""; }}}Copy the code
  • The execution result

    Input type: one Robot generation produces the first generation head the first generation body the first generation hands the first generation feet The second generation robots produce the second generation head the second generation body the second generation hands the second generation feet Input type: threeCopy the code

3. Summary

1) Significance of factory mode: extract the code of instantiated object and put it into a class for unified management and maintenance, so as to decouple the dependency relationship with the main project and improve the expansion and maintenance of the project

2) Three factory modes simple factory mode (static factory mode) Factory method mode Abstract factory mode

3) The dependency abstraction principle of design pattern when creating an object instance, do not directly new the class, but put the actions of the new class in a factory method and return do not let the class inherit from the concrete class, but inherit from the abstract class or implement an interface that does not override the methods already implemented in the base class