/** * Class representing Axe. 斧头 */ publicclassAxeimplementsWeapon{ @Override public String toString(){ return"Axe"; } }
/** * Class representing Bows. 弓 */ publicclassBowimplementsWeapon{ @Override public String toString(){ return"Bow"; } }
/** * Class representing Spear. 矛 */ publicclassSpearimplementsWeapon{ @Override public String toString(){ return"Spear"; } }
/** * Class representing Swords. 剑 */ publicclassSwordimplementsWeapon{ @Override public String toString(){ return"Sword"; } }
/** * Functional interface that allows adding builder with name to the factory. 函数接口,允许向工厂添加具有名称的生成器。 */ publicinterfaceBuilder{ voidadd(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)}方法初始化新对象。 */ publicinterfaceWeaponFactory{
/** * 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}的实例,该实例需要在工厂实例中显式映射为所需的类类型。 */ publicclassApp{