抽象工厂模式

抽象工厂模式

抽象工厂模式也就是不仅生产鼠标,同时生产键盘。
也就是 PC 厂商是个父类,有生产鼠标,生产键盘两个接口。
戴尔工厂,惠普工厂继承它,可以分别生产戴尔鼠标+戴尔键盘,和惠普鼠标+惠普键盘。
创建工厂时,由戴尔工厂创建。
后续工厂.生产鼠标()则生产戴尔鼠标,工厂.生产键盘()则生产戴尔键盘。
抽象工厂

在抽象工厂模式中,假设我们需要增加一个工厂

假设我们增加华硕工厂,则我们需要增加华硕工厂,和戴尔工厂一样,继承 PC 厂商。
之后创建华硕鼠标,继承鼠标类。创建华硕键盘,继承键盘类即可。
增加工厂

在抽象工厂模式中,假设我们需要增加一个产品

假设我们增加耳麦这个产品,则首先我们需要增加耳麦这个父类,再加上戴尔耳麦,惠普耳麦这两个子类。
之后在PC厂商这个父类中,增加生产耳麦的接口。最后在戴尔工厂,惠普工厂这两个类中,分别实现生产戴尔耳麦,惠普耳麦的功能。 以上。
增加产品

抽象工厂设计模式UML类图

抽象工厂设计模式UML类图

抽象工厂设计模式java实现

/**
* KingdomFactory factory interface.
*/
public interface KingdomFactory {

Castle createCastle();

King createKing();

Army createArmy();

}

/**
* OrcKingdomFactory concrete factory.
*/
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();
}
}

/**
* ElfKingdomFactory concrete factory.
*/
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();
}

}

/**
* King interface.
*/
public interface King {

String getDescription();
}

/**
* OrcKing.
*/
public class OrcKing implements King {

static final String DESCRIPTION = "This is the Orc king!";

@Override
public String getDescription() {
return DESCRIPTION;
}
}

/**
* ElfKing.
*/
public class ElfKing implements King {

static final String DESCRIPTION = "This is the Elven king!";

@Override
public String getDescription() {
return DESCRIPTION;
}
}

/**
* Castle interface.
*/
public interface Castle {

String getDescription();
}

/**
* OrcCastle.
*/
public class OrcCastle implements Castle {

static final String DESCRIPTION = "This is the Orc castle!";

@Override
public String getDescription() {
return DESCRIPTION;
}
}

/**
* ElfCastle.
*/
public class ElfCastle implements Castle {

static final String DESCRIPTION = "This is the Elven castle!";

@Override
public String getDescription() {
return DESCRIPTION;
}
}

/**
* Army interface.
*/
public interface Army {

String getDescription();
}

/**
* OrcArmy.
*/
public class OrcArmy implements Army {

static final String DESCRIPTION = "This is the Orc Army!";

@Override
public String getDescription() {
return DESCRIPTION;
}
}

/**
* ElfArmy.
*/
public class ElfArmy implements Army {

static final String DESCRIPTION = "This is the Elven Army!";

@Override
public String getDescription() {
return DESCRIPTION;
}
}

/**
* The Abstract Factory pattern provides a way to encapsulate a group of individual factories that
* have a common theme without specifying their concrete classes. In normal usage, the client
* software creates a concrete implementation of the abstract factory and then uses the generic
* interface of the factory to create the concrete objects that are part of the theme. The client
* does not know (or care) which concrete objects it gets from each of these internal factories,
* since it uses only the generic interfaces of their products. This pattern separates the details
* of implementation of a set of objects from their general usage and relies on object composition,
* as object creation is implemented in methods exposed in the factory interface.
*
* <p>The essence of the Abstract Factory pattern is a factory interface ({@link KingdomFactory})
* and its implementations ( {@link ElfKingdomFactory}, {@link OrcKingdomFactory}). The example uses
* both concrete implementations to create a king, a castle and an army.
抽象工厂模式提供了一种封装一组具有共同主题的单个工厂而无需指定其具体类的方法。在正常使用中,客户端软件会创建抽象工厂的具体实现,然后使用工厂的通用接口来创建作为主题一部分的具体对象。客户端不知道(或不在乎)它从这些内部工厂中获得了哪些具体对象,因为客户端仅使用其产品的通用接口。这种模式将一组对象的实现细节与它们的一般用法分开,并依赖于对象组成,因为对象创建是在工厂接口中公开的方法中实现的。
<p>抽象工厂模式的本质是工厂接口({@link KingdomFactory})及其实现({@link ElfKingdomFactory},{@ link OrcKingdomFactory})。
该示例使用两种具体实现来创建国王,城堡和军队。
*/
public class App {

private static final Logger LOGGER = LoggerFactory.getLogger(App.class);

private King king;
private Castle castle;
private Army army;

/**
* Creates kingdom.
*/
public void createKingdom(final KingdomFactory factory) {
setKing(factory.createKing());
setCastle(factory.createCastle());
setArmy(factory.createArmy());
}

King getKing(final KingdomFactory factory) {
return factory.createKing();
}

public King getKing() {
return king;
}

private void setKing(final King king) {
this.king = king;
}

Castle getCastle(final KingdomFactory factory) {
return factory.createCastle();
}

public Castle getCastle() {
return castle;
}

private void setCastle(final Castle castle) {
this.castle = castle;
}

Army getArmy(final KingdomFactory factory) {
return factory.createArmy();
}

public Army getArmy() {
return army;
}

private void setArmy(final Army army) {
this.army = army;
}

/**
* The factory of kingdom factories.
*/
public static class FactoryMaker {

/**
* Enumeration for the different types of Kingdoms.
*/
public enum KingdomType {
ELF, ORC
}

/**
* The factory method to create KingdomFactory concrete objects.
*/
public static KingdomFactory makeFactory(KingdomType type) {
switch (type) {
case ELF:
return new ElfKingdomFactory();
case ORC:
return new OrcKingdomFactory();
default:
throw new IllegalArgumentException("KingdomType not supported.");
}
}
}

/**
* Program entry point.
*
* @param args command line args
*/
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));
LOGGER.info(app.getArmy().getDescription());
LOGGER.info(app.getCastle().getDescription());
LOGGER.info(app.getKing().getDescription());
}
}