简要描述

一、工厂的定义

二、FactoryMethod 工厂方法

1、定义Movable接口

/**
 * @description: 可移动的交通工具接口
 * @author: flygo
 * @time: 2022/7/5 10:00
 */
public interface Movable {

  void go();
}

2、定义Car汽车对象

/**
 * @description: 汽车对象
 * @author: flygo
 * @time: 2022/7/5 10:00
 */
public class Car implements Movable {

  @Override
  public void go() {
    System.out.println("Car go...");
  }
}

3、定义Plane飞机对象

/**
 * @description: 飞机对象
 * @author: flygo
 * @time: 2022/7/5 10:01
 */
public class Plane implements Movable {

  @Override
  public void go() {
    System.out.println("plane go...");
  }
}

4、定义汽车工厂类

/**
 * @description: 汽车工厂类
 * @author: flygo
 * @time: 2022/7/5 10:03
 */
public class CarFactory {

  Movable create() {
    return new Car();
  }
}

5、定义飞机工厂类

/**
 * @description: 飞机工程类
 * @author: flygo
 * @time: 2022/7/5 10:04
 */
public class PlaneFactory {

  Movable create() {
    return new Plane();
  }
}

6、定义主方法类

/**
 * @description: 主方法
 * @author: flygo
 * @time: 2022/7/5 10:01
 */
public class FactoryMain {

  public static void main(String[] args) {
    Movable carMovable = new CarFactory().create();
    carMovable.go();

    Movable planeMovable = new PlaneFactory().create();
    planeMovable.go();
  }
}

三、简单工厂方法

/**
 * @description: 简单工厂方法
 * @author: flygo
 * @time: 2022/7/5 10:11
 */
public class SimpleVehicleFactory {

  public Car createCar() {
    return new Car();
  }

  public Plane createPlane() {
    return new Plane();
  }
}

四、AbstractFactory抽象方法

【设计模式-04】Factory工厂模式-LMLPHP

1、定义抽象工厂、抽象产品类和方法

1.1、AbstractFactory 抽象工厂

/**
 * @description: 抽象工程方法
 * @author: flygo
 * @time: 2022/7/5 11:02
 */
public abstract class AbstractFactory {

  public abstract AbstractFood createFood();

  public abstract AbstractVehicle createVehicle();

  public abstract AbstractWeapon createWeapon();
}

1.2、AbstractFood 抽象的食物

/**
 * @description: 抽象的食物
 * @author: flygo
 * @time: 2022/7/5 11:03
 */
public abstract class AbstractFood {

  public abstract void printName();
}

1.3、AbstractVehicle抽象的交通工具

/**
 * @description: 抽象的交通工具
 * @author: flygo
 * @time: 2022/7/5 11:04
 */
public abstract class AbstractVehicle {
  public abstract void go();
}

1.4、AbstractVehicle抽象的武器

/**
 * @description: 抽象的武器
 * @author: flygo
 * @time: 2022/7/5 11:06
 */
public abstract class AbstractWeapon {
  public abstract void shoot();
}

2、定义魔法人具体工厂、产品类和方法

2.1、MagicFactory 魔法工厂类

import tech.flygo.factory.abstractfactory.abstractf.AbstractFactory;
import tech.flygo.factory.abstractfactory.abstractf.AbstractFood;
import tech.flygo.factory.abstractfactory.abstractf.AbstractVehicle;
import tech.flygo.factory.abstractfactory.abstractf.AbstractWeapon;

/**
 * @description: 魔法工厂类
 * @author: flygo
 * @time: 2022/7/5 11:07
 */
public class MagicFactory extends AbstractFactory {

  @Override
  public AbstractFood createFood() {
    return new MashRoom();
  }

  @Override
  public AbstractVehicle createVehicle() {
    return new Broom();
  }

  @Override
  public AbstractWeapon createWeapon() {
    return new MagicStick();
  }
}

2.2、MashRoom 魔法蘑菇食物

import tech.flygo.factory.abstractfactory.abstractf.AbstractFood;

/**
 * @description: 魔法蘑菇食物
 * @author: flygo
 * @time: 2022/7/5 11:13
 */
public class MashRoom extends AbstractFood {

  @Override
  public void printName() {
    System.out.println("MashRoom food...");
  }
}

2.3、MagicStick 魔法棒定义

import tech.flygo.factory.abstractfactory.abstractf.AbstractWeapon;

/**
 * @description: 魔法棒
 * @author: flygo
 * @time: 2022/7/5 11:15
 */
public class MagicStick extends AbstractWeapon {

  @Override
  public void shoot() {
    System.out.println("MagicStick shoot...");
  }
}

2.4、Broom 魔法棒定义

import tech.flygo.factory.abstractfactory.abstractf.AbstractVehicle;

/**
 * @description: 魔法扫帚
 * @author: flygo
 * @time: 2022/7/5 11:11
 */
public class Broom extends AbstractVehicle {

  @Override
  public void go() {
    System.out.println("Broom go...");
  }
}

3、定义现代人具体工厂、产品类和方法

3.1、ModernFactory 现代工厂类

import tech.flygo.factory.abstractfactory.abstractf.AbstractFactory;
import tech.flygo.factory.abstractfactory.abstractf.AbstractFood;
import tech.flygo.factory.abstractfactory.abstractf.AbstractVehicle;
import tech.flygo.factory.abstractfactory.abstractf.AbstractWeapon;

/**
 * @description: 现代工厂类
 * @author: flygo
 * @time: 2022/7/5 11:16
 */
public class ModernFactory extends AbstractFactory {

  @Override
  public AbstractFood createFood() {
    return new Bread();
  }

  @Override
  public AbstractVehicle createVehicle() {
    return new Car();
  }

  @Override
  public AbstractWeapon createWeapon() {
    return new AK47();
  }
}

3.2、Bread 面包食物

import tech.flygo.factory.abstractfactory.abstractf.AbstractFood;

/**
 * @description: 面包食物
 * @author: flygo
 * @time: 2022/7/5 11:18
 */
public class Bread extends AbstractFood {

  @Override
  public void printName() {
    System.out.println("Bread...");
  }
}

3.3、AK47 武器AK47

import tech.flygo.factory.abstractfactory.abstractf.AbstractWeapon;

/**
 * @description: 武器AK47
 * @author: flygo
 * @time: 2022/7/5 11:19
 */
public class AK47 extends AbstractWeapon {

  @Override
  public void shoot() {
    System.out.println("AK47 shoot...");
  }
}

3.4、Car汽车类定义

import tech.flygo.factory.abstractfactory.abstractf.AbstractVehicle;

/**
 * @description: 汽车类
 * @author: flygo
 * @time: 2022/7/5 11:17
 */
public class Car extends AbstractVehicle {

  @Override
  public void go() {
    System.out.println("Car go...");
  }
}

4、定义 AbstractFactoryMain 抽象工厂方法主方法类

import tech.flygo.factory.abstractfactory.abstractf.AbstractFactory;
import tech.flygo.factory.abstractfactory.abstractf.AbstractFood;
import tech.flygo.factory.abstractfactory.abstractf.AbstractVehicle;
import tech.flygo.factory.abstractfactory.abstractf.AbstractWeapon;
import tech.flygo.factory.abstractfactory.magic.MagicFactory;

/**
 * @description: 抽象工厂方法主方法类
 * @author: flygo
 * @time: 2022/7/5 11:03
 */
public class AbstractFactoryMain {

  public static void main(String[] args) {

    System.out.println("-------生成一组产品族(魔法)-------");
    // 生成一组产品族(魔法)
    AbstractFactory magicFactory = new MagicFactory();

    AbstractFood food1 = magicFactory.createFood();
    food1.printName();

    AbstractWeapon weapon1 = magicFactory.createWeapon();
    weapon1.shoot();

    AbstractVehicle vehicle1 = magicFactory.createVehicle();
    vehicle1.go();

    System.out.println("-------生成一组产品族(现代)-------");
    // 生成一组产品族(现代)
    AbstractFactory modernFactory = new MagicFactory();

    AbstractFood food2 = modernFactory.createFood();
    food2.printName();

    AbstractWeapon weapon2 = modernFactory.createWeapon();
    weapon2.shoot();

    AbstractVehicle vehicle2 = modernFactory.createVehicle();
    vehicle2.go();
  }
}

输出结果

【设计模式-04】Factory工厂模式-LMLPHP

四、源码地址

GitHub - jxaufang168/Design-Patterns: 设计模式学习设计模式学习. Contribute to jxaufang168/Design-Patterns development by creating an account on GitHub.【设计模式-04】Factory工厂模式-LMLPHPhttps://github.com/jxaufang168/Design-Patterns



 

01-13 01:44