- 简单工厂
- 静态工厂
- 工厂方法 FactoryMethod
- 产品维度扩展
- 抽象工厂
- 产品一族进行扩展
- Spring IOC
- 任何可以产生对象的方法或类,都可以称之为工厂
- 单例也是一种工厂
- 不可咬文嚼字,死扣概念
- 为什么有了new之后,还要有工厂?
- 灵活控制生产过程
- 权限、日志、修饰...
- 任意定制交通工具
- 继承Movable接口
- 任意定制生产过程
- Movable XXXFactory.create()
- 任意定义产品一族
/**
* @description: 可移动的交通工具接口
* @author: flygo
* @time: 2022/7/5 10:00
*/
public interface Movable {
void go();
}
/**
* @description: 汽车对象
* @author: flygo
* @time: 2022/7/5 10:00
*/
public class Car implements Movable {
@Override
public void go() {
System.out.println("Car go...");
}
}
/**
* @description: 飞机对象
* @author: flygo
* @time: 2022/7/5 10:01
*/
public class Plane implements Movable {
@Override
public void go() {
System.out.println("plane go...");
}
}
/**
* @description: 汽车工厂类
* @author: flygo
* @time: 2022/7/5 10:03
*/
public class CarFactory {
Movable create() {
return new Car();
}
}
/**
* @description: 飞机工程类
* @author: flygo
* @time: 2022/7/5 10:04
*/
public class PlaneFactory {
Movable create() {
return new Plane();
}
}
/**
* @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();
}
}
例子描述:假如魔法人 和 现代人两种类型的人,两种人都有食物、武器、交通工具三种产品
- 三种产品描述
- 食物:输出食物名称方法(printName())
- 武器:射击方法(shoot())
- 交通工具:驾驶方法(go())
- 魔法人三种具体产品是:
- 食物:毒蘑菇
- 武器:魔法棒
- 交通工具:魔法扫帚
- 现代人三种具体的产品是:
- 食物:面包
- 武器:AK47
- 交通工具:汽车
通过不同的工厂抽象方法实现不同人的所拥有的不同产品族
/**
* @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();
}
/**
* @description: 抽象的食物
* @author: flygo
* @time: 2022/7/5 11:03
*/
public abstract class AbstractFood {
public abstract void printName();
}
/**
* @description: 抽象的交通工具
* @author: flygo
* @time: 2022/7/5 11:04
*/
public abstract class AbstractVehicle {
public abstract void go();
}
/**
* @description: 抽象的武器
* @author: flygo
* @time: 2022/7/5 11:06
*/
public abstract class AbstractWeapon {
public abstract void shoot();
}
MagicFactory 魔法工厂类 继承AbstractFactory 抽象工厂,同时实现抽象工厂方法
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();
}
}
MashRoom 魔法蘑菇食物 继承AbstractFood 抽象类型,同时实现抽象方法
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...");
}
}
MagicStick 魔法棒 继承AbstractWeapon 抽象类型,同时实现抽象方法
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...");
}
}
Broom 魔法扫帚 继承AbstractVehicle 抽象类型,同时实现抽象方法
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...");
}
}
ModernFactory 现代工厂类 继承AbstractFactory 抽象工厂,同时实现抽象工厂方法
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();
}
}
Bread 面包食物食物 继承AbstractFood 抽象类型,同时实现抽象方法
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...");
}
}
AK47 武器AK47 继承AbstractWeapon 抽象类型,同时实现抽象方法
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...");
}
}
Car 汽车类 继承AbstractVehicle 抽象类型,同时实现抽象方法
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...");
}
}
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();
}
}
输出结果
GitHub - jxaufang168/Design-Patterns: 设计模式学习设计模式学习. Contribute to jxaufang168/Design-Patterns development by creating an account on GitHub.https://github.com/jxaufang168/Design-Patterns