工厂方法模式

工厂方法模式

工厂方法模式也就是鼠标工厂是个父类,有生产鼠标这个接口。
戴尔鼠标工厂,惠普鼠标工厂继承它,可以分别生产戴尔鼠标,惠普鼠标。
生产哪种鼠标不再由参数决定,而是创建鼠标工厂时,由戴尔鼠标工厂创建。
后续直接调用鼠标工厂.生产鼠标()即可
工厂方法

工厂方法设计模式UML类图

工厂方法设计模式UML类图

工厂方法设计模式java实现

/**
* Weapon interface.
*/
public interface Weapon {

WeaponType getWeaponType();

}

/**
* WeaponType enumeration.
*/
public enum WeaponType {

SHORT_SWORD("short sword"), SPEAR("spear"), AXE("axe"), UNDEFINED("");

private String title;

WeaponType(String title) {
this.title = title;
}

@Override
public String toString() {
return title;
}
}

/**
* OrcWeapon.
*/
public class OrcWeapon implements Weapon {

private WeaponType weaponType;

public OrcWeapon(WeaponType weaponType) {
this.weaponType = weaponType;
}

@Override
public String toString() {
return "Orcish " + weaponType;
}

@Override
public WeaponType getWeaponType() {
return weaponType;
}
}

/**
* ElfWeapon.
*/
public class ElfWeapon implements Weapon {

private WeaponType weaponType;

public ElfWeapon(WeaponType weaponType) {
this.weaponType = weaponType;
}

@Override
public String toString() {
return "Elven " + weaponType;
}

@Override
public WeaponType getWeaponType() {
return weaponType;
}
}

/**
* The interface containing method for producing objects.
*/
public interface Blacksmith {

Weapon manufactureWeapon(WeaponType weaponType);

}

/**
* Concrete subclass for creating new objects.
*/
public class OrcBlacksmith implements Blacksmith {

private static Map<WeaponType, OrcWeapon> ORCARSENAL;

static {
ORCARSENAL = new HashMap<>(WeaponType.values().length);
Arrays.stream(WeaponType.values()).forEach(type -> ORCARSENAL.put(type, new OrcWeapon(type)));
}

@Override
public Weapon manufactureWeapon(WeaponType weaponType) {
return ORCARSENAL.get(weaponType);
}
}

/**
* Concrete subclass for creating new objects.
*/
public class ElfBlacksmith implements Blacksmith {

private static Map<WeaponType, ElfWeapon> ELFARSENAL;

static {
ELFARSENAL = new HashMap<>(WeaponType.values().length);
Arrays.stream(WeaponType.values()).forEach(type -> ELFARSENAL.put(type, new ElfWeapon(type)));
}

@Override
public Weapon manufactureWeapon(WeaponType weaponType) {
return ELFARSENAL.get(weaponType);
}

}

/**
* The Factory Method is a creational design pattern which uses factory methods to deal with the
* problem of creating objects without specifying the exact class of object that will be created.
* This is done by creating objects via calling a factory method either specified in an interface
* and implemented by child classes, or implemented in a base class and optionally overridden by
* derived classes—rather than by calling a constructor.
*
* <p>In this Factory Method example we have an interface ({@link Blacksmith}) with a method for
* creating objects ({@link Blacksmith#manufactureWeapon}). The concrete subclasses (
* {@link OrcBlacksmith}, {@link ElfBlacksmith}) then override the method to produce objects of
* their liking.
工厂方法是一种创新性的设计模式,它使用工厂方法来处理创建对象的问题,而无需指定要创建的对象的确切类。
这是通过调用工厂方法创建的对象来完成的,该方法可以在接口中指定并由子类实现,也可以在基类中实现,并有选择地覆盖
派生类-而不是通过调用构造函数。
<p>在此工厂方法示例中,我们具有一个接口({@link Blacksmith}),其中包含用于创建对象的方法({@link Blacksmith#manufactureWeapon})。 具体的子类(
{@link OrcBlacksmith},{@ link ElfBlacksmith}),然后重写该方法以生成自己喜欢的对象。
*
*/
public class App {

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

private final Blacksmith blacksmith;

/**
* Creates an instance of <code>App</code> which will use <code>blacksmith</code> to manufacture
* the weapons for war.
* <code>App</code> is unaware which concrete implementation of {@link Blacksmith} it is using.
* The decision of which blacksmith implementation to use may depend on configuration, or
* the type of rival in war.
* @param blacksmith a non-null implementation of blacksmith
*/
public App(Blacksmith blacksmith) {
this.blacksmith = blacksmith;
}

/**
* Program entry point.
*
* @param args command line args
*/
public static void main(String[] args) {
// Lets go to war with Orc weapons
var app = new App(new OrcBlacksmith());
app.manufactureWeapons();

// Lets go to war with Elf weapons
app = new App(new ElfBlacksmith());
app.manufactureWeapons();
}

private void manufactureWeapons() {
var weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR);
LOGGER.info(weapon.toString());
weapon = blacksmith.manufactureWeapon(WeaponType.AXE);
LOGGER.info(weapon.toString());
}
}