先放上2张类图,直观的感受一下
车的类图:
Car是顶级接口
BmwCar是宝马车,MercedesCar是奔驰车
BMW1是宝马的轿车系列,BMW2是宝马的跑车系列
Mercedes1是奔驰的轿车系列,Mercedes2是奔驰的跑车系列
我们需要的工厂就是需要生产宝马、奔驰的轿车,宝马、奔驰的跑车
我们不按照车的牌子来生产,为了方便理解抽象工厂,我们按照车的类型来生产,分成轿车、跑车2个生产线
给上工厂的类图
CarFactory1是轿车生产线
CarFactory2是跑车生产线
main方法来调用2各生产线来生产车辆
上代码:
先是关于车的类
//车辆的顶级接口
public interface Car {
public void info();
}
public abstract class BmwCar implements Car{
public void info(){
System.out.println("宝马车");
}
}
public abstract class MercedesCar implements Car{
@Override
public void info() {
System.out.println("奔驰车");
}
}
/**宝马轿车
*
* @author: wangmingxin1
* @create: 2018-11-05 15:32
**/
public class BMW1 extends BmwCar{
public void show(){
System.out.println("宝马轿车 尊崇悦享,自由驾驭,智能轻量化高强度车身");
}
}
/**宝马跑车
*
* @author: wangmingxin1
* @create: 2018-11-05 15:28
**/
public class BMW2 extends BmwCar{
public void show(){
System.out.println("宝马跑车 灵动的车顶,高扭矩发动机,底盘技术出色");
}
}
/**
* 奔驰轿车
*
* @author: wangmingxin1
* @create: 2018-11-05 15:12
**/
public class Mercedes1 extends MercedesCar {
public void show(){
System.out.println("奔驰轿车 启动引擎的那一刻,就掳获了沿途的追慕。敞篷轿跑车性能卓越,御风疾驰之間,尽享万千风情。");
}
}
/**
*奔驰跑车
*
* @author: wangmingxin1
* @create: 2018-11-05 15:10
**/
public class Mercedes2 extends MercedesCar {
public void show(){
System.out.println("奔驰跑车 雷霆动力之下是对自我的永不松懈,内外兼修而实力卓越,全新跑车秉承一贯的磅薄野心,助力时代中坚者的全力以赴。");
}
}
关于工厂的类
//顶级接口
public interface CarFactory {
//制作宝马车
public Car createBmWCar();
//制作奔驰车
public Car createMercedesCar();
}
public abstract class AbstractCarFactory implements CarFactory{
protected Car createCar(CarEnum carEnum){
Car car = null;
if (!carEnum.getValue().equals("")) {
try {
car = (Car)Class.forName(carEnum.getValue()).newInstance();
} catch (Exception e) {
e.printStackTrace();
}
}
return car;
}
}
根据指定枚举类来创建指定车辆
public enum CarEnum {
BMW1("pers.wmx.abstractfactory.BMW1"),
BMW2("pers.wmx.abstractfactory.BMW2"),
Mercedes1("pers.wmx.abstractfactory.Mercedes1"),
Mercedes2("pers.wmx.abstractfactory.Mercedes2");
private String value = "";
private CarEnum(String value){
this.value = value;
}
public String getValue(){
return this.value;
}
}
宝马奔驰轿车生产线
/**生产轿车
*
* @author: wangmingxin1
* @create: 2018-11-05 15:53
**/
public class CarFactory1 extends AbstractCarFactory {
@Override
public Car createBmWCar() {
return super.createCar(CarEnum.BMW1);
}
@Override
public Car createMercedesCar() {
return super.createCar(CarEnum.Mercedes1);
}
}
奔驰宝马跑车生产线
/**生产跑车
*
* @author: wangmingxin1
* @create: 2018-11-05 15:57
**/
public class CarFactory2 extends AbstractCarFactory {
@Override
public Car createBmWCar() {
return super.createCar(CarEnum.BMW2);
}
@Override
public Car createMercedesCar() {
return super.createCar(CarEnum.Mercedes2);
}
}
测试使用工厂
public class Main {
public static void main(String[] args){
//第一条生产线,轿车生产线
CarFactory carFactory1 = new CarFactory1();
//第二条生产线,跑车生产线
CarFactory carFactory2 = new CarFactory2();
BMW1 bmWCar = (BMW1) carFactory1.createBmWCar();
bmWCar.info();
bmWCar.show();
Mercedes2 mercedesCar = (Mercedes2) carFactory2.createMercedesCar();
mercedesCar.info();
mercedesCar.show();
}
}
运行效果:
说点什么
您将是第一位评论人!