简单工厂模式

简单工厂模式

简单工厂模式不是 23 种里的一种,简而言之,就是有一个专门生产某个产品的类。
比如下图中的鼠标工厂,专业生产鼠标,给参数 0,生产戴尔鼠标,给参数 1,生产惠普鼠标。
简单工厂

简单工厂设计模式UML类图

简单工厂设计模式UML类图

简单工厂设计模式java实现

/**
* Interface representing weapon.
武器
*/
public interface Weapon {
}

/**
* Enumerates {@link Weapon} types.
*/
public enum WeaponType {
SWORD, AXE, BOW, SPEAR
}

/**
* Class representing Axe.
斧头
*/
public class Axe implements Weapon {
@Override
public String toString() {
return "Axe";
}
}

/**
* Class representing Bows.

*/
public class Bow implements Weapon {
@Override
public String toString() {
return "Bow";
}
}

/**
* Class representing Spear.

*/
public class Spear implements Weapon {
@Override
public String toString() {
return "Spear";
}
}

/**
* Class representing Swords.

*/
public class Sword implements Weapon {
@Override
public String toString() {
return "Sword";
}
}

/**
* Functional interface that allows adding builder with name to the factory.
函数接口,允许向工厂添加具有名称的生成器。
*/
public interface Builder {
void add(WeaponType name, Supplier<Weapon> supplier);
}

/**
* Functional interface, an example of the factory-kit design pattern.
* <br>Instance created locally gives an opportunity to strictly define
* which objects types the instance of a factory will be able to create.
* <br>Factory is a placeholder for {@link Builder}s
* with {@link WeaponFactory#create(WeaponType)} method to initialize new objects.
功能接口,工厂套件设计模式的示例。
<br>在本地创建的实例为严格定义提供了机会
工厂实例将能够创建哪些对象类型。
<br> Factory是{@link Builder}的占位符,它使用{@link WeaponFactory#create(WeaponType)}方法初始化新对象。
*/
public interface WeaponFactory {

/**
* Creates an instance of the given type.
*
* @param name representing enum of an object type to be created.
* @return new instance of a requested class implementing {@link Weapon} interface.
*/
Weapon create(WeaponType name);

/**
* Creates factory - placeholder for specified {@link Builder}s.
*
* @param consumer for the new builder to the factory.
* @return factory with specified {@link Builder}s
*/
static WeaponFactory factory(Consumer<Builder> consumer) {
var map = new HashMap<WeaponType, Supplier<Weapon>>();
consumer.accept(map::put);
return name -> map.get(name).get();
}
}

/**
* Factory-kit is a creational pattern which defines a factory of immutable content with separated
* builder and factory interfaces to deal with the problem of creating one of the objects specified
* directly in the factory-kit instance.
*
* <p>In the given example {@link WeaponFactory} represents the factory-kit, that contains four
* {@link Builder}s for creating new objects of the classes implementing {@link Weapon} interface.
*
* <p>Each of them can be called with {@link WeaponFactory#create(WeaponType)} method, with
* an input representing an instance of {@link WeaponType} that needs to be mapped explicitly with
* desired class type in the factory instance.
Factory-kit是一种创建模式,该模式定义了具有不可变内容的工厂,并使用单独的生成器和工厂接口来处理创建直接在factory-kit实例中指定的对象之一的问题。
<p>在给定的示例中,{@ link WeaponFactory}代表工厂工具包,其中包含四个{@link Builder},用于创建实现{@link Weapon}接口的类的新对象。
<p>可以使用{@link WeaponFactory#create(WeaponType)}方法来调用每个方法,其中输入代表{@link WeaponType}的实例,该实例需要在工厂实例中显式映射为所需的类类型。
*/
public class App {

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

/**
* Program entry point.
*
* @param args command line args
*/
public static void main(String[] args) {
var factory = WeaponFactory.factory(builder -> {
builder.add(WeaponType.SWORD, Sword::new);
builder.add(WeaponType.AXE, Axe::new);
builder.add(WeaponType.SPEAR, Spear::new);
builder.add(WeaponType.BOW, Bow::new);
});
var axe = factory.create(WeaponType.AXE);
LOGGER.info(axe.toString());
}
}