The factory mode is created by encapsulating objects, including simple factory mode, Factory mode, and abstract Factory mode.
Simple factory mode:
Dependency principle: separate the changed and unchanged processes with a single responsibility.
Implementation method: it is not a factory in the true sense. It just extracts the methods for creating objects in the target and puts them in a class for centralized processing.
Mode advantages: 1. Separate the parts of the objects created in the program and put them into a class for centralized management to facilitate maintenance; 2. You can use the configuration method for maintenance of the separation part.
Disadvantages of the mode: 1. Retaining a large number of object creation judgment logic; 2. Separating does not improve efficiency; 3. Concentrating all object creation logic, violating the high cohesion responsibility allocation principle
Applicable mode: 1. there are many logic created in the program and need to be maintained frequently; 2. the same class object needs to be returned, but only the running class knows that the specific returned object is required; 3. customers do not care about how objects are created
Factory method mode:
Dependency principle: Dependency Inversion, for interface programming, separating the changed and unchanged parts of the program.
Implementation Method: delays the processing of object details to the subclass. Using abstract classes, you can set the methods to be processed to be abstract.
Mode advantages: delayed instantiation; specific processing logic is determined by sub-classes;
Mode disadvantage: refactoring the Factory method will destroy the existing code
Mode: creating an object requires a lot of repeated code. Creating an object requires some information, which should not be included in the composite class. Creating an object requires centralized management to ensure program consistency.
Abstract Factory mode:
Dependency principle: hides detailed implementation details for interface programming.
Implementation Method: provides an interface to create a dependent object family without specifying a specific class.
Mode advantages: separate specific products from the customer code and integrate the same type of products
Disadvantages: difficult to expand new features
Applicable mode: provides a product Library
Simple example
Make a simple analysis based on the production skills of the role in the game.
The game has three roles: Warrior, mage, and priest. Each role corresponds to one skill. Each skill can be subdivided into two branches (see figure ), materials required for creating items (not shown in the figure)
In the sample code, take the warrior-foundry-weapon-sword class as an example, use the simple factory mode to assist in product and model acquisition, and use the factory method mode to define the weapon factory and its subclass sword factory, define weapons using abstract factory models-swords and materials used
The directory structure is as follows:
The code is as follows:
Package factorypattern. roles; import factorypattern. skills. makeException; import factorypattern. skills. noSuchProductionException; import factorypattern. skills. production; /*** Role interface */public interface Role {/*** Production ** @ param type * @ param pName * @ return * @ throws MakeException * @ throws NoSuchProductionException */Production make (String type, string pName) throws MakeException, NoSuchProductionException ;}
Package factorypattern. roles; import factorypattern. skills. makeException; import factorypattern. skills. noSuchProductionException; import factorypattern. skills. production; import factorypattern. skills. skill;/*** Role: Warrior */public class Warrior implements Role {private final Skill skill; public Warrior (Skill skill) {this. skill = skill;} @ Overridepublic Production make (String type, String pName) throws MakeException, NoSuchProductionException {return skill. product (type, pName );}}
Package factorypattern. skills;/*** Skill interface */public interface Skill {Production product (String type, String pName) throws MakeException, NoSuchProductionException ;}
Package factorypattern. skills;/*** abstract class factory: product class */public interface Production {String getName ();}
Package factorypattern. skills;/*** production skills not yet learned to throw an Exception */public class NoSuchProductionException extends Exception {private static final long serialVersionUID = 1705270808656089664L; public NoSuchProductionException (String skill) {super ("not yet learned" + skill + "production skills! ");}}
Package factorypattern. skills;/*** no technical expertise Exception */public class MakeException extends Exception {private static final long serialVersionUID =-8191272257834156731L; public MakeException () {super ("unable to make without this skill ");}}
Package factorypattern. skills. cast; import factorypattern. skills. makeException; import factorypattern. skills. noSuchProductionException; import factorypattern. skills. production; import factorypattern. skills. skill;/*** simple factory mode: casting skills */public class Cast implements Skill {@ Overridepublic Production product (String type, String pName) throws MakeException, NoSuchProductionException {return ContrlCenter. getProduction (type, pName );}}
Package factorypattern. skills. cast; import factorypattern. skills. noSuchProductionException; import factorypattern. skills. production; import factorypattern. skills. cast. sword. sword;/*** factory model: the weapon factory ** is determined by the sub-class weapon model. * In this example, only the Sword factory and the dagger factory are written, changbing factory .... */public abstract class WarponFactory {public Production getCasting (String pName) throws NoSuchProductionException {Sword p = getModel (pName); p. setOre (); p. setFur (); p. melt (); p. casting (); p. quench (); p. hilt (); return p;} protected abstract Sword getModel (String pName) throws NoSuchProductionException ;}
Package factorypattern. skills. cast; import factorypattern. skills. makeException; import factorypattern. skills. noSuchProductionException; import factorypattern. skills. production; import factorypattern. skills. cast. sword. blueWaveSword; import factorypattern. skills. cast. sword. crystalSword; import factorypattern. skills. cast. sword. ironSword; import factorypattern. skills. cast. sword. sword; import factorypattern. skills. cast. sword. swordFactory;/*** specific processing class in simple factory mode ** handle various details in a centralized manner, you can use xml for centralized configuration */public class ContrlCenter {/*** to obtain the product ** @ param type * @ param pName * @ return * @ throws NoSuchProductionException * @ throws MakeException * /public static Production getProduction (String type, string pName) throws NoSuchProductionException, MakeException {Production p = null; if ("warpon ". equals (type) {WarponFactory f = new SwordFactory (); p = f. getCasting (pName);} else if ("jewelry ". equals (type) {// This branch is not implemented for the moment} else {throw new MakeException ();} return p ;} /*** get model ** @ param pName * @ return * @ throws NoSuchProductionException */public static Sword getModel (String pName) throws NoSuchProductionException {Sword p = null; if ("ironSword ". equals (pName) {p = new IronSword ();} else if ("blueWaveSword ". equals (pName) {p = new BlueWaveSword ();} else if ("crystalSword ". equals (pName) {p = new CrystalSword () ;}else {throw new NoSuchProductionException (pName) ;}return p ;}}
Package factorypattern. skills. cast. sword; import factorypattern. skills. noSuchProductionException; import factorypattern. skills. cast. contrlCenter; import factorypattern. skills. cast. warponFactory;/*** simple factory mode: Sword Factory */public class SwordFactory extends WarponFactory {@ Overrideprotected Sword getModel (String pName) throws NoSuchProductionException {return ContrlCenter. getModel (pName );}}
Package factorypattern. skills. cast. sword; import factorypattern. skills. production; import factorypattern. skills. meterial. fur. fur; import factorypattern. skills. meterial. ore. ore;/*** Abstract Factory: The Sword Class ** is determined by the specific subclass to cast materials for use. [the subclass does not have the right to decide what subclass is actually used, the material has been set.] */public abstract class Sword implements Production {protected String name; // The Sword name protected Ore; // The synthetic ore protected Fur fur; // auxiliary leather @ Overridepublic String getName () {return name;}/*** prepare ore */public abstract void setOre (); /*** prepare leather */public abstract void setFur ();/*** melting */public void melt () {System. out. println ("melting ore:" + ore. getName ();}/*** cast */public void casting () {System. out. println ("casting");}/*** quenching */public void quench () {System. out. println ("quenching");}/*** decorative molding */public void hilt () {System. out. println ("use" + fur. getName () + "");}}
Package factorypattern. skills. cast. sword; import factorypattern. skills. meterial. fur. pigFur; import factorypattern. skills. meterial. ore. ironOre;/*** Sword type: Iron Sword */public class IronSword extends Sword {public IronSword () {name = "tie Jian" ;}@ Overridepublic void setOre () {this. ore = new IronOre () ;}@ Overridepublic void setFur () {this. fur = new PigFur ();}}
Package factorypattern. skills. cast. sword; import factorypattern. skills. meterial. fur. silveryFoxFur; import factorypattern. skills. meterial. ore. crystalOre;/*** Sword class: crystal Sword */public class CrystalSword extends Sword {public CrystalSword () {this. name = "" ;}@ Overridepublic void setOre () {this. ore = new CrystalOre () ;}@ Overridepublic void setFur () {this. fur = new SilveryFoxFur ();}}
Package factorypattern. skills. cast. sword; import factorypattern. skills. meterial. fur. deerFur; import factorypattern. skills. meterial. ore. blueStoneOre;/*** Sword class: Bibo Sword */public class BlueWaveSword extends Sword {public BlueWaveSword () {this. name = "Bibo ";}@ Overridepublic void setOre () {this. ore = new BlueStoneOre () ;}@ Overridepublic void setFur () {this. fur = new DeerFur ();}}
Package factorypattern. skills. meterial. fur;/*** leather material interface */public interface Fur {String getName ();}
Package factorypattern. skills. meterial. fur;/*** leather class: Lupi */public class DeerFur implements Fur {@ Overridepublic String getName () {return "Lupi ";}}
Package factorypattern. skills. meterial. fur;/*** leather class: pig */public class PigFur implements Fur {@ Overridepublic String getName () {return "pig ";}}
Package factorypattern. skills. meterial. fur;/*** leather class: silver body skin */public class SilveryFoxFur implements Fur {@ Overridepublic String getName () {return "silver body skin ";}}
Package factorypattern. skills. meterial. ore;/*** mineral class interface */public interface Ore {String getName ();}
Package factorypattern. skills. meterial. ore;/*** Ore class: Iron ore */public class IronOre implements Ore {@ Overridepublic String getName () {return "iron ore ";}}
Package factorypattern. skills. meterial. ore;/*** ore: Crystal ore * @ author Tony. sun **/public class CrystalOre implements Ore {@ Overridepublic String getName () {return "Crystal Ore ";}}
Package factorypattern. skills. meterial. ore;/*** ore class: turquoise * @ author Tony. sun **/public class BlueStoneOre implements Ore {@ Overridepublic String getName () {return "turquoise ";}}
Package factorypattern. test; import factorypattern. roles. role; import factorypattern. roles. warrior; import factorypattern. skills. makeException; import factorypattern. skills. noSuchProductionException; import factorypattern. skills. production; import factorypattern. skills. cast. cast; public class Test {public static void main (String [] args) {Role warrier = new Warrior (new Cast (); try {Production p1 = warrier. make ("warpon", "ironSword"); infos ("get new item:" + p1.getName (); Production p2 = warrier. make ("warpon", "blueWaveSword"); infos ("get new item:" + p2.getName (); Production p3 = warrier. make ("warpon", "crystalSword"); infos ("get new item:" + p3.getName ();} catch (MakeException e) {e. printStackTrace ();} catch (NoSuchProductionException e) {e. printStackTrace () ;}} public static void infos (String s) {System. out. println (s); System. out. println ("------------------------------------------------");}}
Factory Model Summary