Java设计模式【工厂模式】
前言
三种工厂模式:简单工厂模式、工厂方法模式、抽象工厂模式;
- 创建型设计模式
- 封装对象的创建过程,将对象的创建和使用分离开,从而提高代码的可维护性和可扩展性
简单工厂模式
概述:将对象的创建过程封装在一个工厂类中,客户端通过工厂类来创建对象。
特点:
- 一个工厂类+多个产品类
- 添加新的产品类时,就必须修改工厂类的代码,这违反了开闭原则
package com.qiangesoft.design.creational.factory.simple;/*** 简单工厂模式*/
public class SimpleFactory {public static void main(String[] args) {// 奔驰车Car benz = CarFactory.buildCar("benz");benz.running();// 宝马车Car bmw = CarFactory.buildCar("bmw");bmw.running();// 奥迪车Car audi = CarFactory.buildCar("audi");audi.running();}
}/*** 1.工厂类*/
class CarFactory {public static Car buildCar(String type) {if ("benz".equals(type)) {return new Benz();} else if ("bmw".equals(type)) {return new BMW();} else if ("audi".equals(type)) {return new Audi();} else {throw new IllegalArgumentException("Illegal argument for type: " + type);}}
}/*** 2.抽象产品接口*/
interface Car {void running();
}/*** 3.产品实现类* 奔驰、宝马、奥迪*/
class Benz implements Car {@Overridepublic void running() {System.out.println("奔驰车在路上跑!");}
}class BMW implements Car {@Overridepublic void running() {System.out.println("宝马车在路上跑!");}
}class Audi implements Car {@Overridepublic void running() {System.out.println("奥迪车在路上跑!");}
}
工厂方法模式
概述:定义一个工厂接口和多个具体工厂类来实现对象的创建。每个具体工厂类都实现了工厂接口,并负责创建一类产品对象。
特点:
- 一个工厂接口+多个具体工厂类
- 具体的产品类和工厂类都可以在运行时动态切换,符合开闭原则
package com.qiangesoft.design.creational.factory.method;/*** 工厂方法模式*/
public class FactoryMethod {public static void main(String[] args) {// 奔驰车CarFactory benzFactory = new BenzFactory();Car benzCar = benzFactory.buildCar();benzCar.running();// 宝马车CarFactory bmwFactory = new BMWFactory();Car bmwCar = bmwFactory.buildCar();bmwCar.running();// 奥迪车CarFactory audiFactory = new AudiFactory();Car audiCar = audiFactory.buildCar();audiCar.running();}
}/*** 1.抽象工厂接口*/
interface CarFactory {Car buildCar();
}/*** 2.工厂实现类* 奔驰工厂、宝马工厂、奥迪工厂*/
class BenzFactory implements CarFactory {@Overridepublic Car buildCar() {return new Benz();}
}class BMWFactory implements CarFactory {@Overridepublic Car buildCar() {return new BMW();}
}class AudiFactory implements CarFactory {@Overridepublic Car buildCar() {return new BMW();}
}/*** 3.抽象产品接口*/
interface Car {void running();
}/*** 4.产品实现类* 奔驰、宝马、奥迪*/
class Benz implements Car {@Overridepublic void running() {System.out.println("奔驰车在路上跑!");}
}class BMW implements Car {@Overridepublic void running() {System.out.println("宝马车在路上跑!");}
}class Audi implements Car {@Overridepublic void running() {System.out.println("奥迪车在路上跑!");}
}
抽象工厂模式
概述:工厂方法模式的升级版,它定义了多个工厂接口和多个产品族接口,每个工厂接口负责创建一类产品族对象,每个产品族接口负责定义一类产品对象。具体工厂类实现工厂接口并负责创建一类产品族对象,具体产品类实现产品族接口并负责创建一类产品对象。
特点:
- 多个工厂接口+多个产品族接口
- 具体的产品族和工厂类都可以在运行时动态切换,符合开闭原则
package com.qiangesoft.design.creational.factory.abstractf;/*** 抽象工厂模式*/
public class AbstractFactory {public static void main(String[] args) {// 奔驰车CarFactory benzFactory = new BenzFactory();Engine benzEngine = benzFactory.createEngine();Transmission benzTransmission = benzFactory.createTransmission();System.out.println("奔驰汽车:" + benzEngine.getName() + "+" + benzTransmission.getName());// 宝马车CarFactory bmwFactory = new BMWFactory();Engine bmwEngine = bmwFactory.createEngine();Transmission bmwTransmission = bmwFactory.createTransmission();System.out.println("宝马汽车:" + bmwEngine.getName() + "+" + bmwTransmission.getName());// 奥迪车CarFactory audiFactory = new AudiFactory();Engine audiEngine = audiFactory.createEngine();Transmission audiTransmission = audiFactory.createTransmission();System.out.println("奥迪汽车:" + audiEngine.getName() + "+" + audiTransmission.getName());}
}/*** 1.抽象工厂接口*/
interface CarFactory {Engine createEngine();Transmission createTransmission();
}/*** 2.具体工厂类* 奔驰工厂、宝马工厂、奥迪工厂*/
class BenzFactory implements CarFactory {@Overridepublic Engine createEngine() {return new BenzEngine();}@Overridepublic Transmission createTransmission() {return new BenzTransmission();}
}class BMWFactory implements CarFactory {@Overridepublic Engine createEngine() {return new BMWEngine();}@Overridepublic Transmission createTransmission() {return new BmwTransmission();}
}class AudiFactory implements CarFactory {@Overridepublic Engine createEngine() {return new AudiEngine();}@Overridepublic Transmission createTransmission() {return new AudiTransmission();}
}/*** 3.产品接口1:发动机*/
interface Engine {String getName();
}/*** 4.产品实现类1:发动机*/
class BenzEngine implements Engine {@Overridepublic String getName() {return "奔驰发动机";}
}class BMWEngine implements Engine {@Overridepublic String getName() {return "宝马发动机";}
}class AudiEngine implements Engine {@Overridepublic String getName() {return "奥迪发动机";}
}/*** 5.产品接口2:变速箱*/
interface Transmission {String getName();
}/*** 6.产品实现类2:变速箱*/
class BenzTransmission implements Transmission {@Overridepublic String getName() {return "奔驰变速箱";}
}class BmwTransmission implements Transmission {@Overridepublic String getName() {return "宝马变速箱";}
}class AudiTransmission implements Transmission {@Overridepublic String getName() {return "奥迪变速箱";}
}