This article describes the implementation of Gozi chess game functions based on the Xiangyuan mode of Java. Share it for your reference, as follows:
1. Pattern definition
Enjoy the world mode efficiently supports a large number of fine-grained objects in a shared way. Reduce the performance consumption of system creating object instances by multiplexing existing objects in memory. Xiangyuan's English is Flyweight, which means a particularly small object, that is, a fine-grained object.
2. Examples of the model
1. Pattern analysis
We borrow Gozi game to illustrate this pattern.
2. Static class diagram of Enjoy Yuan mode
3. Code example
3.1 Creating Abstract Chessman
package com.demo.flyweight.object;public abstract class AbstractChessman { // Chesspaper coordinates protected int x; protected int y; // Chesspaper category (black|white) protected String chess; public AbstractChessman(String chess) { this.chess = chess; } // Point coordinates set public abstract void point(int x, int y); // Show chesspaper information public void show() { System.out.println(this.chess + "(" + this.x + "," + this.y + ")"); }}3.2 Creating BlackChessman
package com.demo.flyweight.object;public class BlackChessman extends AbstractChessman { /** * Constructor initializes black chessman */ public BlackChessman() { super("●"); System.out.println("--BlackChessman Construction Exec!!!"); } // Point coordinate setting @Override public void point(int x, int y) { this.x = x; this.y = y; // Show chess contents show(); }}3.3 Create WhiteChessman
package com.demo.flyweight.object;public class WhiteChessman extends AbstractChessman { /** * Constructor initializes white chessman */ public WhiteChessman() { super("○"); System.out.println("--WhiteChessman Construction Exec!!!"); } // Point coordinate setting @Override public void point(int x, int y) { this.x = x; this.y = y; // Show chess piece contents show(); }}3.4 Create a Chess Man Factory
package com.demo.flyweight.factory;import java.util.Hashtable;import com.demo.flyweight.object.AbstractChessman;import com.demo.flyweight.object.BlackChessman;import com.demo.flyweight.object.WhiteChessman;public class FiveChessmanFactory { // Singleton pattern factory private static FiveChessmanFactory fiveChessmanFactory = new FiveChessmanFactory(); // Cache to store shared objects private final Hashtable<Character, AbstractChessman> cache = new Hashtable<Character, AbstractChessman>(); // Privatization constructor private FiveChessmanFactory() { } // Get singleton factory public static FiveChessmanFactory getInstance() { return fiveChessmanFactory; } /** * Get chess pieces based on characters* * @param c * (B: Black ChessW: White Chess) * @return */ public AbstractChessman getChessmanObject(char c) { // Obtain the instance of the pawn object from the cache AbstractChessman abstractChessman = this.cache.get(c); if (abstractChessman == null) { // If there is no pawn object instance information in the cache, create the pawn object instance and put it in the cache switch (c) { case 'B': abstractChessman = new BlackChessman(); break; case 'W': abstractChessman = new WhiteChessman(); break; default: break; } // To prevent illegal characters from entering null if (abstractChessman != null) { // Put this.cache.put(c, abstractChessman); } } // If there is a chess object in the cache, return directly abstractChessman; }}3.5 Client implementation
package com.demo;import java.util.Random;import com.demo.flyweight.factory.FiveChessmanFactory;import com.demo.flyweight.object.AbstractChessman;/** * Main application* * @author */public class Client { /** * @param args */ public static void main(String[] args) { // Create Goziqi Factory FiveChessmanFactory fiveChessmanFactory = FiveChessmanFactory .getInstance(); Random random = new Random(); int radom = 0; AbstractChessman abstractChessman = null; // Get the chess piece for (int i = 0; i < 10; i++) { radom = random.nextInt(2); switch (radom) { // Get the black chess case 0: abstractChessman = fiveChessmanFactory.getChessmanObject('B'); break; // Get the white chess case 1: abstractChessman = fiveChessmanFactory.getChessmanObject('W'); break; } if (abstractChessman != null) { abstractChessman.point(i, random.nextInt(15)); } } }}4. Operation results
--WhiteChessman Construction Exec!!!
○(0,2)
○(1,6)
--BlackChessman Construction Exec!!!
●(2,3)
○(3,14)
○(4,13)
○(5,8)
●(6,14)
●(7,0)
●(8,3)
○(9,8)
3. Two states of the Enjoyment Mode
Intrinsic state : It will not change with the change of the environment, it is stored in the internal state information of the Entrinsic object. This Intrinsic state can be shared. For any Entrinsic object, its value is exactly the same. Just like the "black pieces" and "white pieces" in Goji, the state it represents is the internal state.
External state : It will change with the change of the environment, so it cannot share the state. For different objects of the genus, its values may be different. The external state of the Xiangyuan object must be saved by the client. After the Xiangyuan object is created and needs to be used, it will be passed into the Xiangyuan object. Just like the position information of Gozi, the state represented is the external state of the object of the ecology.
Therefore, the external and internal states of Xiangyuan are two independent states and have no connection with each other.
IV. Principles of design of this model
1. Share fine-grained objects to reduce memory space.
2. Effectively isolate the changing parts and the unchanged parts of the system.
5. Use occasions
1. When there are many instances of a certain object type in the system.
2. In system design, after classifying object instances, it is time to find that there are few real distinct categories.
6. Static class diagram of Enjoy Yuan mode
For more information about Java algorithms, readers who are interested in this site can view the topics: "Java Data Structure and Algorithm Tutorial", "Summary of Java Operation DOM Node Tips", "Summary of Java File and Directory Operation Tips" and "Summary of Java Cache Operation Tips"
I hope this article will be helpful to everyone's Java programming.