1. Introduction

1) Abstract factory model: Define an interface for creating related or dependent objects cluster, without naming specific class 2) the abstract factory pattern can be a simple factory pattern and factory method pattern to integrate 3) from the design level, the abstract factory pattern is the improvement of simple factory pattern (or called further abstraction) 4) abstract factory into two layers, AbstractFactory and factory subclasses of concrete implementations. You can subclass a factory based on the type of object you create, turning a single simple factory class into a factory cluster that is easier to maintain and extend

Example 2.

Instance demand

  • Added female robot and male robot factory production in simple Factory mode example

Abstract factory pattern code

  • AbstractRobotFactory Defines an abstract factory

    public interface AbstractRobotFactory {
    
        Robot createRobot(String type);
    }
    Copy the code
  • GirlRobotFactory is used to produce female robots

    public class GirlRobotFactory implements AbstractRobotFactory {
        @Override
        public Robot createRobot(String type) {
            Robot robot = null;
            if (type.equalsIgnoreCase("one")) {
                robot = new RobotOneGirl("First generation");
            } else if (type.equalsIgnoreCase("two")) {
                robot = new RobotTwoGirl("Second generation");
            }
            returnrobot; }}Copy the code
  • ManRobotFactory is used to produce male robots

    public class ManRobotFactory implements AbstractRobotFactory {
        @Override
        public Robot createRobot(String type) {
            Robot robot = null;
            if (type.equalsIgnoreCase("one")) {
                robot = new RobotOneMan("First generation");
            } else if (type.equalsIgnoreCase("two")) {
                robot = new RobotTwoMan("Second generation");
            }
            returnrobot; }}Copy the code
  • Client

    public class Client {
    
        public static void main(String[] args) {
            AbstractRobotFactory robotFactory = null;
            String input = getInput("Enter to create girl or man robot :");
            switch (input) {
                case "girl":
                    robotFactory = new GirlRobotFactory();
                    break;
                case "man":
                    robotFactory = new ManRobotFactory();
                    break;
                default:
                    return;
            }
            while (true) {
                String type = getInput("Enter robot type :");
                Robot robot = robotFactory.createRobot(type);
                if(robot ! =null) {
                    robot.create();
                } else {
                    System.exit(0); }}}public static String getInput(String msg) {
            try {
                BufferedReader strin = new BufferedReader(new InputStreamReader(System.in));
                System.out.println(msg);
                String str = strin.readLine();
                return str;
            } catch (IOException e) {
                e.printStackTrace();
                return ""; }}}Copy the code
  • The results

    Do you need to create girl or Man robot: Man Input robot Category: One Male robot Generation Production first generation head first generation body first generation hand first generation foot input robot category: Two male robots second generation production second generation head second generation body second generation hand second generation foot input robot category: EXCopy the code
    Input to create girl or man robot: girl Input robot Category: female robot first generation production first generation head first generation body first generation hand first generation foot input robot category: Two female robots second generation production second generation head second generation body second generation hand second generation foot input robot category: EXCopy the code