Java Simple Factory mode, polymorphic factory, Extraction factory explanation, code example

Source: Internet
Author: User

Package Org.rui.pattern2;import Java.util.*;import junit.framework.*;/** * (implement Factory mode) The common method is to put factory Static methods declared as base classes (static method) * * @author Administrator * */abstract class shape{public abstract void Draw ();p Ublic Abstract void Erase ();p ublic static Shape factory (String type) {if (Type.equals ("Circle")) return new Circle (); if (Type.equals (" Square ") return new Square (); throw new RuntimeException (" Bad shape creation: "+ type);}} Class Circle extends Shape{circle () {}//package-access constructorpublic void Draw () {System.out.println ("Circle.draw" );} public void Erase () {System.out.println ("Circle.erase");}} Class Square extends Shape{square () {}//package-access constructorpublic void Draw () {System.out.println ("Square.draw" );} public void Erase () {System.out.println ("Square.erase");}} public class ShapeFactory1 extends testcase{string shlist[] = {"Circle", "Square", "Square", "Circle", "Circle", "Square" }; List shapes = new ArrayList ();p ublic void Test () {Iterator it = arrays.aslist (shlist). ITerator (), while (It.hasnext ()) Shapes.add (Shape.factory (String) It.next ()), it = Shapes.iterator (); while ( It.hasnext ()) {shape s = (shape) it.next (); S.draw (); S.erase ();}} public static void Main (String args[]) {junit.textui.TestRunner.run (Shapefactory1.class);}} The/:~/** * Factory () method needs to pass in a parameter to determine the specific type of Shape to be created; in the above * Example (parameter) happens to be a string (string), it can also be any other type. When adding a new Shape * type (we assume that the initialization code of the object being created is from outside the system, rather than using an array of hard-coded (hard-coded) as in the example above), the only code that needs to be changed is the factory () method. In order for the code that creates the object to be included only in the factory () method, the constructors for a particular type of Shape class are declared as * package permissions, so that only the factory () method can call these constructors, and the part of the code that is outside the package There are not enough permissions (these constructors are used for tuning). */


Package Org.rui.pattern2;import Java.util.*;import junit.framework.*;/** * In the above example, the static factory () method causes all operations that create objects to be concentrated in one place , which is the only place where you need to change the code. This is certainly a good solution, which encapsulates the process of creating objects. However, the design pattern emphasizes the use of Factory Method * To enable different types of factories to be derived from the basic type of factory (subclass) (The example above is a special case). * However, the book does not give a specific example, just repeating the example used to illustrate the abstract Factory (you will see an abstract * Factory example in the next section of this book). In the following example, we have modified Shapefactory1.java to make the factory method a virtual function of a separate class. Note that a specific type of * Shape class is dynamically loaded as needed. * * @author Administrator * */interface shape2{void Draw (); void erase ();} Abstract class shapefactory{protected abstract Shape2 Create ();p rivate static MAP factories = new HashMap ();p ublic static void Addfactory (String ID, shapefactory f) {factories.put (ID, f);} public static final Shape2 Createshape (String ID) {if (!factories.containskey (ID)) {try{//dynamically loaded Class.forName (" Org.rui.pattern2. "+ ID);} catch (ClassNotFoundException e) {e.printstacktrace (); throw new RuntimeException ("Bad shape creation:" + ID);} Look at this is: if (!factories.containskey (ID)) throw new RuntimeException ("Bad shApe creation: "+ ID);} Gets the object inner class inherits from the add-in to the collection shapefactory create returns the Outer class object Shape2return ((shapefactory) factories.get (ID)). create ();}} -------------------------------------------------------------------class Circle2 implements Shape2{private Circle2 () {}public void Draw () {System.out.println ("Circle.draw");} public void Erase () {System.out.println ("Circle.erase");} Inner class private static class Factory extends shapefactory{//return external class object protected Shape2 Create () {return new Circle2 ();}} Static{shapefactory.addfactory ("Circle", New Factory ()) to add the object to the factory map collection during initialization;}} ---------------------------------------------------------------------------class Square2 implements shape2{ Private Square2 () {}public void Draw () {System.out.println ("Square.draw");} public void Erase () {System.out.println ("Square.erase");} private static class Factory extends shapefactory{protected Shape2 Create () {return new Square2 ();}} Static{shapefactory.addfactory ("Square2", New Factory ());}} // --------------------------------------------------public class ShapeFactory2 extends testcase{string shlist[] = {"Circle2", "Square2", "Square2", "Circle2", "Circle2" , "Square2"}; List shapes = new ArrayList ();p ublic void Test () {//t This just ensures that it will complete//not throw an exception iterator it = Arrays.aslist (shlist). iterator (); whi Le (It.hasnext ()) Shapes.add (Shapefactory.createshape (String) It.next ()); it = Shapes.iterator (); while (It.hasnext ( ) {Shape2 s = (Shape2) it.next (); S.draw (); S.erase ();}} public static void Main (String args[]) {junit.textui.TestRunner.run (Shapefactory2.class);}} /:~/** * Now the Factory Method (Factory) appears in its own class shapefactory, and the name is changed to the Create (*) method. It is a protected (protected) method, which means it cannot be called directly, but can be overloaded. The subclass of the Shape class must create a subclass of the corresponding * shapefactory, and create its own instance by overloading the Create () function. The creation of a series of Shape objects is actually done by calling * Shapefactory.createshape (). Createshape () is a static method that finds the corresponding factory object (factory obejct) by locating the MAP member variable of the * shapefactory, based on the incoming label. The factory object that is found is then used to create the * Shape object, but you can imagine a more tricky problem: The Factory object (which corresponds to a shape type) is used to create objects in a more complex way. However, in most cases you do not seem to need to use complex polymorphic factory methods (polymorphic FactOry * method), adding a static method (like Shapefactory1.java) in the base class is enough to solve the problem. * * Note that the initialization of shapefactory must be done by loading the map data members (map elements are factory objects), and these initialization codes are in static initialization statements of the * Shape implementation class. In this way, you have to inherit the original type for each new type you are adding (meaning Shape?). ), create a * factory, and then add static initialization statements to load the Map object. Again, these additional complexities imply that if you do not need to create a separate factory * object, it is best to use the static factory method. */

---------------------------------------------------------------

Package org.rui.pattern2;/** * Abstract Factory (factories) */interface obstacle{void action (); Interface player{void Interactwith (Obstacle o);} Class Kitty implements Player{public void Interactwith (obstacle ob) {System.out.println ("Kitty has encountered a"); O B.action ();}} Class Kungfuguy implements Player{public void Interactwith (obstacle ob) {System.out.println ("Kungfuguy now battles a"); O B.action ();}} Class Puzzle implements obstacle{public void action () {System.out.println ("Puzzle");}} Class Nastyweapon implements obstacle{public void action () {System.out.println ("Nastyweapon");}} The Abstract factory:interface gameelementfactory{player makeplayer (); obstacle makeobstacle ();} Concrete Factories:class Kittiesandpuzzles Implements gameelementfactory{//Real Example class returns the parent like public Player Makeplayer () { return new Kitty ();} The real example class returns the parent similar to public obstacle makeobstacle () {return new Puzzle ();}} Class Killanddismember implements Gameelementfactory{public Player Makeplayer () {return new Kungfuguy ();} Public Obstacle MaKeobstacle () {return new Nastyweapon ();}} Game Environment Class Gameenvironment{private gameelementfactory gef;private Player p;private Obstacle ob;/** * Get the required object instance from the factory * @pa Ram Factory */public Gameenvironment (Gameelementfactory Factory) {GEF = Factory;p = Factory.makeplayer (); OB = Factory.makeobstacle ();//system.out.println ("OB:" +ob); public void Play () {p.interactwith (OB);}}

The package Org.rui.pattern2;import junit.framework.testcase;/** abstract Factory mode looks much like the factory objects we saw earlier, It's just that it has multiple, not a factory method. Each factory method creates an object of a different type. The basic idea is that where you create a factory object, you decide how to use those objects created by the factory object. The example given in design mode enables portability between different user graphical interfaces (GUIs): You create a corresponding factory object based on the GUI you use, and after that, when you need to use menus, buttons, and scrollbars, it will be based on the GUI you use. Automatically create the appropriate objects. In this way, you can separate the code that implements the switch between the different GUIs, so that it is concentrated in one place. As another example, suppose you want to create a common game environment, and you want to support different types of games. The following example uses an abstract factory to give a possible implementation of it. * @author Administrator * */public class Games extends testcase{gameelementfactory KP = new Kittiesandpuzzles (), kd = new K Illanddismember (); Gameenvironment G1 = new Gameenvironment (KP), g2 = new Gameenvironment (kd);//These just ensure no exceptions is thrown:p ublic void Test1 () {G1.play ();} public void Test2 () {G2.play ();} public static void Main (String args[]) {junit.textui.TestRunner.run (Games.class);}} /:~/** in the above game environment, the player object interacts with the Obstale object, depending on the type of game you choose, the respective types of player and obstacle will be different. You decide the type of game by selecting a particular gameelementfactory, and then gameelementfactory controls the initialization and gameplay. In the example above, the initialAnd gameplay are very simple, but those activities (initial conditions and state changes) can largely determine the outcome of the game. Here, Gameenvironment is not used to inherit from other classes, although it is likely that it will also make sense. The above code also contains two modes of double dispatch (double dispatching) and factory method (FactoryMethod), which we will explain later. */


Java Simple Factory mode, polymorphic factory, Extraction factory explanation, code example

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.