🔍目的
提供一个用于创建相关对象家族的接口,而无需指定其具体类
🔍解释
真实世界例子
要创建一个王国,我们需要具有共同主题的对象。精灵王国需要精灵国王、精灵城堡和精灵军队,而兽人王国需要兽人国王、兽人城堡和兽人军队。王国中的对象之间存在依赖关系。
通俗的说
一个将单个但相关/从属的工厂分组在一起而没有指定其具体类别的工厂。
维基百科
抽象工厂模式提供了一种封装一组具有共同主题的单个工厂而无需指定其具体类的方法
程序实例
按照上述的真实世界案例。首先,我们为王国中的对象提供了一些接口和实现。
public interface Castle {String getDescription();
}public interface King {String getDescription();
}public interface Army {String getDescription();
}// Elven implementations ->
public class ElfCastle implements Castle {static final String DESCRIPTION = "This is the Elven castle!";@Overridepublic String getDescription() {return DESCRIPTION;}
}
public class ElfKing implements King {static final String DESCRIPTION = "This is the Elven king!";@Overridepublic String getDescription() {return DESCRIPTION;}
}
public class ElfArmy implements Army {static final String DESCRIPTION = "This is the Elven Army!";@Overridepublic String getDescription() {return DESCRIPTION;}
}// Orcish implementations similarly -> ...
然后我们有了王国工厂的抽象和实现
public interface KingdomFactory {Castle createCastle();King createKing();Army createArmy();
}public class ElfKingdomFactory implements KingdomFactory {public Castle createCastle() {return new ElfCastle();}public King createKing() {return new ElfKing();}public Army createArmy() {return new ElfArmy();}
}public class OrcKingdomFactory implements KingdomFactory {public Castle createCastle() {return new OrcCastle();}public King createKing() {return new OrcKing();}public Army createArmy() {return new OrcArmy();}
}
我们可以制作相关对象的系列,即精灵王国工厂创建了精灵城堡,国王和军队等。
var factory = new ElfKingdomFactory();
var castle = factory.createCastle();
var king = factory.createKing();
var army = factory.createArmy();castle.getDescription();
king.getDescription();
army.getDescription();
程序输出
This is the Elven castle!
This is the Elven king!
This is the Elven Army!
我们可以为不同的王国工厂设计工厂。 在此示例中,我们创建了FactoryMaker,负责返回ElfKingdomFactory或OrcKingdomFactory的实例。 客户可以使用FactoryMaker来创建所需的具体工厂,该工厂随后将生产不同的具体对象(军队,国王,城堡)。 在此示例中,我们还使用了一个枚举来参数化客户要求的王国工厂类型。
public static class FactoryMaker {public enum KingdomType {ELF, ORC}public static KingdomFactory makeFactory(KingdomType type) {return switch (type) {case ELF -> new ElfKingdomFactory();case ORC -> new OrcKingdomFactory();default -> throw new IllegalArgumentException("KingdomType not supported.");};}
}public static void main(String[] args) {var app = new App();LOGGER.info("Elf Kingdom");app.createKingdom(FactoryMaker.makeFactory(KingdomType.ELF));LOGGER.info(app.getArmy().getDescription());LOGGER.info(app.getCastle().getDescription());LOGGER.info(app.getKing().getDescription());LOGGER.info("Orc Kingdom");app.createKingdom(FactoryMaker.makeFactory(KingdomType.ORC));--similar use of the orc factory}
🔍类图
Abstract Factory class diagram
🔍适用场景
1.该系统应独立于其产品的创建,组成和表示方式
2.系统应配置有多个产品系列之一
3.相关产品对象系列旨在一起使用,你需要强制执行此约束
4.你想提供产品的类库,并且只想暴露它们的接口,而不是它们的实现。
5.从概念上讲,依赖项的生存期比使用者的生存期短。
6.你需要一个运行时值来构建特定的依赖关系
7.你想决定在运行时从系列中调用哪种产品。
8.你需要提供一个或更多仅在运行时才知道的参数,然后才能解决依赖关系。
9.当你需要产品之间的一致性时
10.在向程序添加新产品或产品系列时,您不想更改现有代码。
🔍后果
1.Java中的依赖注入会隐藏服务类的依赖关系,这些依赖关系可能导致运行时错误,而这些错误在编译时会被捕获。
2.虽然在创建预定义对象时模式很好,但是添加新对象可能会很困难。
3.由于引入了许多新的接口和类,因此代码变得比应有的复杂。