抽象工厂模式
是一种创建型设计模式,它能让你封装一系列相关的工厂方法,它们能产出相互依赖的对象家族。这种模式提供了一个接口,允许创建一系列相关或相互依赖的对象,而无需指定它们具体的类。
抽象工厂模式的主要优点包括:
封装变化:通过将产品族的创建细节封装起来,使得系统更容易适应新的产品族。
易于交换产品族:可以在运行时根据条件选择不同的产品族。
遵循开闭原则:可以在不修改现有代码的情况下添加新的产品族。
关键概念
封装多个工厂:通过一个共同的主题将一组工厂封装起来,不指定具体实现。
客户端使用:客户端创建抽象工厂的具体实现,并通过通过接口创建主题相关的具体对象。
对象组合:依赖对象组合而非继承来实现扩展和变化。
一.标准案例结构
1.创建一个王国接口和他的实现类(作为王国类型)
public interface KingdomFactory {
//城堡
Castle createCastle();
//国王
King createKing();
//军队
Army createArmy();
}
ElfKingdomFactory
public class ElfKingdomFactory implements KingdomFactory {
@Override
public Castle createCastle() {
return new ElfCastle();
}
@Override
public King createKing() {
return new ElfKing();
}
@Override
public Army createArmy() {
return new ElfArmy();
}
}
OrcKingdomFactory
public class OrcKingdomFactory implements KingdomFactory {
@Override
public Castle createCastle() {
return new OrcCastle();
}
@Override
public King createKing() {
return new OrcKing();
}
@Override
public Army createArmy() {
return new OrcArmy();
}
}
2. 城堡.国王.军队.接口
public interface Castle {
String getDescription();
}
public interface King {
String getDescription();
}
public interface Army {
String getDescription();
}
3.王国制造者
@Getter
@Setter
public class Kingdom {
private King king;
private Castle castle;
private Army army;
public static class FactoryMaker {
//类型
public enum KingdomType {
ELF, ORC
}
/**
* 创建KingdomFactory具体对象的工厂方法。
*/
public static KingdomFactory makeFactory(KingdomType type) {
return switch (type) {
case ELF -> new ElfKingdomFactory();
case ORC -> new OrcKingdomFactory();
//这里的->不是用于lambda表达式,而是在switch表达式中指示case块的内容。
};
}
}
}
ELF王国
public class ElfCastle implements Castle {
static final String DESCRIPTION = "This is the elven castle!";
@Override
public String getDescription() {
return DESCRIPTION;
}
}
public class ElfKing implements King {
static final String DESCRIPTION = "This is the elven king!";
@Override
public String getDescription() {
return DESCRIPTION;
}
}
public class ElfArmy implements Army {
static final String DESCRIPTION = "This is the elven army!";
@Override
public String getDescription() {
return DESCRIPTION;
}
}
ORC王国
public class OrcCastle implements Castle {
static final String DESCRIPTION = "This is the orc castle!";
@Override
public String getDescription() {
return DESCRIPTION;
}
}
public class OrcKing implements King {
static final String DESCRIPTION = "This is the orc king!";
@Override
public String getDescription() {
return DESCRIPTION;
}
}
public class OrcArmy implements Army {
static final String DESCRIPTION = "This is the orc army!";
@Override
public String getDescription() {
return DESCRIPTION;
}
}
3主程序
public class App implements Runnable {
private final Kingdom kingdom = new Kingdom();
public static void main(String[] args) {
var app = new App();
app.run();
}
@Override
public void run() {
LOGGER.info("elf kingdom");
createKingdom(Kingdom.FactoryMaker.KingdomType.ELF);
LOGGER.info(kingdom.getArmy().getDescription());
LOGGER.info(kingdom.getCastle().getDescription());
LOGGER.info(kingdom.getKing().getDescription());
LOGGER.info("orc kingdom");
createKingdom(Kingdom.FactoryMaker.KingdomType.ORC);
LOGGER.info(kingdom.getArmy().getDescription());
LOGGER.info(kingdom.getCastle().getDescription());
LOGGER.info(kingdom.getKing().getDescription());
}
public void createKingdom(final Kingdom.FactoryMaker.KingdomType kingdomType) {
final KingdomFactory kingdomFactory = Kingdom.FactoryMaker.makeFactory(kingdomType);
kingdom.setKing(kingdomFactory.createKing());
kingdom.setCastle(kingdomFactory.createCastle());
kingdom.setArmy(kingdomFactory.createArmy());
}
}
运行结果
二.简单练习
假设我们有一个简单的产品族例子,包括两个产品族:品牌BrandA 和 品牌BrandB,每个产品族都有两种产品:Chair 和 Table。
1.定义抽象产品接口:
public interface Chair {
void sitOn();
}
public interface Table {
void putObjectOn();
}
2.实现具体产品:
public class BrandAChair implements Chair {
@Override
public void sitOn() {
System.out.println("Sitting on a Brand A chair.");
}
}
public class BrandATable implements Table {
@Override
public void putObjectOn() {
System.out.println("Putting object on a Brand A table.");
}
}
public class BrandBChair implements Chair {
@Override
public void sitOn() {
System.out.println("Sitting on a Brand B chair.");
}
}
public class BrandBTable implements Table {
@Override
public void putObjectOn() {
System.out.println("Putting object on a Brand B table.");
}
}
3.定义抽象工厂:
public interface FurnitureFactory {
Chair createChair();
Table createTable();
}
4.实现具体工厂:
public class BrandAFurnitureFactory implements FurnitureFactory {
@Override
public Chair createChair() {
return new BrandAChair();
}
@Override
public Table createTable() {
return new BrandATable();
}
}
public class BrandBFurnitureFactory implements FurnitureFactory {
@Override
public Chair createChair() {
return new BrandBChair();
}
@Override
public Table createTable() {
return new BrandBTable();
}
}
5.使用抽象工厂:
public class Client {
public static void main(String[] args) {
FurnitureFactory factory = new BrandAFurnitureFactory(); // 或者 BrandBFurnitureFactory
Chair chair = factory.createChair();
Table table = factory.createTable();
chair.sitOn();
table.putObjectOn();
}
}