工厂模式实现了创建者和调用者的分离
分类
- 简单(静态)工厂模式
用来生产同一等级结构中的任意产品。通过接收不同的参数来返回对应的对象实例。其对新增加的产品无能为力,不修改代码,无法扩展。
- 工厂方法模式
用来生产同一等级结构中的固定产品。(支持增加任意产品)
- 抽象工厂模式
用来生产不同产品族的全部产品。(对于增加新的产品,无能为力;支持增加产品族)
简单(静态)工厂模式
类图
实现代码
/**
* Car接口
*/
public interface Car {
void run();
}
/**
* Car接口实现
*/
public class Audi implements Car {
@Override
public void run() {
System.out.println("奥迪行驶中......");
}
}
public class Benz implements Car {
@Override
public void run() {
System.out.println("奔驰行驶中......");
}
}
/**
* 简单工厂模式的两种写法
*/
public class CarFactory1 {
public static Car createCar(String type){
if("Audi".equals(type)){
return new Audi();
}else if("Benz".equals(type)){
return new Benz();
}else{
return null;
}
}
}
public class CarFactory2 {
public static Car createAudi(){
return new Audi();
}
public static Car createBenz(){
return new Benz();
}
}
/**
* 使用
*/
public class Client {
public static void main(String[] args) {
Car c1 =CarFactory.createCar("Audi");
Car c2 = CarFactory.createCar("Benz");
c1.run();
c2.run();
}
}
工厂方法模式
类图
实现代码
/**
* Car接口与实现同上
*/
public interface CarFactory {
Car createCar();
}
public class AudiFactory implements CarFactory {
@Override
public Car createCar() {
return new Audi();
}
}
public class BenzFactory implements CarFactory {
@Override
public Car createCar() {
return new Benz();
}
}
public class Client {
public static void main(String[] args) {
Car c1 = new AudiFactory().createCar();
Car c2 = new BenzFactory().createCar();
c1.run();
c2.run();
}
}
抽象工厂模式
类图
实现代码
public interface Engine {
void run();
void start();
}
class LuxuryEngine implements Engine{
@Override
public void run() {
System.out.println("转的快!");
}
@Override
public void start() {
System.out.println("启动快!可以自动启停!");
}
}
class LowEngine implements Engine{
@Override
public void run() {
System.out.println("转的慢!");
}
@Override
public void start() {
System.out.println("启动慢!");
}
}
public interface Seat {
void massage();
}
class LuxurySeat implements Seat {
@Override
public void massage() {
System.out.println("可以自动按摩!");
}
}
class LowSeat implements Seat {
@Override
public void massage() {
System.out.println("不能按摩!");
}
}
public interface Tyre {
void revolve();
}
class LuxuryTyre implements Tyre {
@Override
public void revolve() {
System.out.println("旋转不磨损!");
}
}
class LowTyre implements Tyre {
@Override
public void revolve() {
System.out.println("旋转磨损快!");
}
}
public interface CarFactory {
Engine createEngine();
Seat createSeat();
Tyre createTyre();
}
public class LowCarFactory implements CarFactory {
@Override
public Engine createEngine() {
return new LowEngine();
}
@Override
public Seat createSeat() {
return new LowSeat();
}
@Override
public Tyre createTyre() {
return new LowTyre();
}
}
public class LuxuryCarFactory implements CarFactory {
@Override
public Engine createEngine() {
return new LuxuryEngine();
}
@Override
public Seat createSeat() {
return new LuxurySeat();
}
@Override
public Tyre createTyre() {
return new LuxuryTyre();
}
}
public class Client {
public static void main(String[] args) {
CarFactory factory = new LuxuryCarFactory();
Engine e = factory.createEngine();
e.run();
e.start();
}
}