1. 简单定义
工厂模式又称工厂方法模式,是一种创建型设计模式,其在父类提供了一种创建对象的方法,允许子类决定实例化对象的类型。 抽象工厂,意味着工厂的泛化,也就是说对多个工厂共通行为的抽取及概括。
2. 场景模式
如果想打造一个大型的汽车工厂,汽车工厂可以生产高端车,中端车,低端车。不管是高端车,中端车,还是低端车 都有共同的属性,比如车子重量,车子价格,车子座位数等等,因此我们可以对其进行抽象如下。
3. 代码结构
└─go
│ Test.java
│
└─DreamFactory
│ AbstractCarFactory.java
│ CarModel.java
│
└─factory
├─lx
│ │ LxFactory.java
│ │
│ └─store
│ LxHighCar.java
│ LxLowCar.java
│ LxMidCar.java
│
├─nio
│ │ NioFactory.java
│ │
│ └─store
│ NioHightCar.java
│ NioLowCar.java
│ NioMidCar.java
│
└─xp
│ XpFactory.java
│
└─store
XpHighCar.java
XpLowCar.java
XpMidCar.java
4. 代码DEMO
4.1 定义抽象产品
public abstract class CarModel {
//座位数
public abstract int getSeats();
//重量
public abstract int getWeight();
// 价格
public abstract int getPrice();
//播放音乐
public abstract String play();
}
4.2 定义抽象工厂
public interface AbstractCarFactory {
public CarModel createHighCar();
public CarModel createMidCar();
public CarModel createLowCar();
}
4.3 定义具体工厂
小鹏工厂:
public class XpFactory implements AbstractCarFactory {
@Override
public CarModel createHighCar() {
return new XpHighCar();
}
@Override
public CarModel createMidCar() {
return new XpMidCar();
}
@Override
public CarModel createLowCar() {
return new XpMidCar();
}
}
小鹏系列车:
高端车:
public class XpHighCar extends CarModel {
@Override
public int getSeats() {
return 7;
}
@Override
public int getWeight() {
return 1000;
}
@Override
public int getPrice() {
return 1111110;
}
@Override
public String play() {
return "稻香";
}
}
中端车:
public class XpMidCar extends CarModel {
@Override
public int getSeats() {
return 6;
}
@Override
public int getWeight() {
return 800;
}
@Override
public int getPrice() {
return 80000;
}
@Override
public String play() {
return "小苹果";
}
}
低端车:
public class XpLowCar extends CarModel {
@Override
public int getSeats() {
return 5;
}
@Override
public int getWeight() {
return 700;
}
@Override
public int getPrice() {
return 70000;
}
@Override
public String play() {
return "*飞翔";
}
}
蔚来工厂:
public class NioFactory implements AbstractCarFactory {
@Override
public CarModel createHighCar() {
return new NioHightCar();
}
@Override
public CarModel createMidCar() {
return new NioMidCar();
}
@Override
public CarModel createLowCar() {
return new NioLowCar();
}
}
蔚来系列车:
高端车:
public class NioHightCar extends CarModel {
@Override
public int getSeats() {
return 7;
}
@Override
public int getWeight() {
return 1000;
}
@Override
public int getPrice() {
return 1000000;
}
@Override
public String play() {
return "挪威的森林";
}
}
中端车:
public class NioMidCar extends CarModel {
@Override
public int getSeats() {
return 5;
}
@Override
public int getWeight() {
return 500;
}
@Override
public int getPrice() {
return 500000;
}
@Override
public String play() {
return "稻香";
}
}
低端车:
public class NioLowCar extends CarModel {
@Override
public int getSeats() {
return 3;
}
@Override
public int getWeight() {
return 300;
}
@Override
public int getPrice() {
return 300000;
}
@Override
public String play() {
return "小苹果";
}
}
4.4 业务应用
public class Test {
public static void main(String[] args) {
AbstractCarFactory nioFactory = new NioFactory();
AbstractCarFactory xpFactory = new XpFactory();
CarModel nioHighcar = nioFactory.createHighCar();
CarModel xpHighCar = xpFactory.createHighCar();
}
}