Java 23 design patterns -- sharing element pattern (structural design pattern)

Sharing element mode introduce The meta shari...
Sharing element mode

Sharing element mode

introduce

The meta sharing mode is mainly to share common objects, reduce the use of memory and improve the access efficiency of the system. These shared objects usually consume memory or need to query a large number of interfaces or use database resources, so they are uniformly separated and used as shared objects.

In addition, the meta sharing mode can be divided into server and client. Generally, in Internet H5 and Web scenarios, most data needs to be processed by the server, such as the use of database connection pool and multi-threaded thread pool. In addition to these functions, some also need to be packaged by the server and distributed to the client, because the server needs to do meta sharing processing. However, in some game scenes, many clients need to render map effects, such as; Trees, flowers, fish and insects use shared element common objects by setting different element descriptions, so as to reduce the occupation of memory and make the game on the client more smooth.

In the implementation of the meta model, you need to use the meta factory to manage this part of independent objects and shared objects, so as to avoid the problem of thread safety.

String type in Java

In JAVA language, string type uses meta pattern. String object is of type final. Once an object is created, it cannot be changed. In JAVA, string constants are stored in the constant pool. JAVA will ensure that there is only one copy of a string constant in the constant pool. String a = "abc", where "abc" is a string constant.

String a = "abc"; String b = "abc"; System.out.println(a==b);

In the above example, the result is: true, which means that both references a and b point to the same string constant "abc" in the constant pool. This design avoids unnecessary large resource consumption when creating N same objects.

Structure of meta model

The sharing mode uses a share to avoid the overhead of having a large number of the same content objects. The most common and intuitive cost is memory loss. The key to sharing meta objects is to distinguish between internal state and external state.

An intrinsic state is stored inside the meta object and will not change with the change of the environment. Therefore, a sharing element can have an intrinsic state and can be shared.

An intrinsic state changes with the change of environment and cannot be shared. The intrinsic state of the shared meta object must be saved by the client and passed into the shared meta object when it needs to be used after the shared meta object is created. The intrinsic state cannot affect the intrinsic state of the meta object. They are independent of each other.

example


As like as two peas and a white boy, the size and shape of the go board are identical. If each chess piece is stored in memory as an independent object, it will lead to a large memory space required by the go software. How to reduce the running cost and improve the system performance is a problem to be solved. In order to solve this problem, we decided to use the shared element mode to design the chess sub object of the go software

Internal state
//Go sub class: Abstract meta class public abstract class IgoChessman { public abstract String getColor(); public void display() { System.out.println("Chess piece color:" + this.getColor()); } } //White chess pieces: specific categories public class WhiteIgoChessman extends IgoChessman { public String getColor() { return "white"; } } //Black chess pieces: specific categories public class BlackIgoChessman extends IgoChessman { public String getColor() { return "black"; } }
//Go sub factory class: Xiangyuan factory class, designed using single instance mode public class IgoChessmanFactory { private static IgoChessmanFactory instance = new IgoChessmanFactory(); private static Hashtable ht; //Hashtable is used to store the shared element objects and act as the shared element pool private IgoChessmanFactory() { ht = new Hashtable(); IgoChessman black,white; black = new BlackIgoChessman(); ht.put("b",black); white = new WhiteIgoChessman(); ht.put("w",white); } //Returns a unique instance of the meta factory class public static IgoChessmanFactory getInstance() { return instance; } //Get the meta object stored in the Hashtable through the key public static IgoChessman getIgoChessman(String color) { return (IgoChessman)ht.get(color); } }
External state

Through further analysis of go pieces, it is found that although black pieces and white pieces can be shared, they will be displayed in different positions on the chessboard. How can the same sunspots or whites be repeatedly displayed and located in different places on a chessboard? The solution is to define the position of the chess piece as an external state of the chess piece and set it when necessary. Therefore, we added a new class Coordinates (coordinate class) to store the position of each chess piece

//Go sub class: Abstract meta class public abstract class ExternalIgoChessman { public abstract String getColor(); public void display(Coordinates coord){ System.out.println("Chess piece color:" + this.getColor() + ",Chess piece position:" + coord.getX() + "," + coord.getY() ); } } //White chess pieces: specific categories public class ExternalWhiteIgoChessman extends ExternalIgoChessman { public String getColor() { return "white"; } } //Black chess pieces: specific categories public class ExternalBlackIgoChessman extends ExternalIgoChessman { public String getColor() { return "black"; } }
public class Coordinates { private int x; private int y; public Coordinates(int x,int y) { this.x = x; this.y = y; } public int getX() { return this.x; } public void setX(int x) { this.x = x; } public int getY() { return this.y; } public void setY(int y) { this.y = y; } }
//Go sub factory class: Xiangyuan factory class, designed using single instance mode public class ExternalIgoChessmanFactory { private static ExternalIgoChessmanFactory instance = new ExternalIgoChessmanFactory(); private static Hashtable ht; //Hashtable is used to store the shared element objects and act as the shared element pool private ExternalIgoChessmanFactory() { ht = new Hashtable(); ExternalIgoChessman black, white; black = new ExternalBlackIgoChessman(); ht.put("b", black); white = new ExternalWhiteIgoChessman(); ht.put("w", white); } //Returns a unique instance of the meta factory class public static ExternalIgoChessmanFactory getInstance() { return instance; } //Get the meta object stored in the Hashtable through the key public static ExternalIgoChessman getIgoChessman(String color) { return (ExternalIgoChessman) ht.get(color); } }
Test class
@Slf4j public class ApiTest { @Test public void testCommand() { log.info("--------------------- Internal state sharing mode ---------------------"); IgoChessman black1, black2, black3, white1, white2; IgoChessmanFactory factory; //Get meta factory object factory = IgoChessmanFactory.getInstance(); //Get three sunspots through Xiangyuan factory black1 = factory.getIgoChessman("b"); black2 = factory.getIgoChessman("b"); black3 = factory.getIgoChessman("b"); System.out.println("Judge whether the two sunspots are the same:" + (black1 == black2)); //Obtain two white sons through Xiangyuan factory white1 = factory.getIgoChessman("w"); white2 = factory.getIgoChessman("w"); System.out.println("Judge whether the two Baizi are the same:" + (white1 == white2)); //Show pieces black1.display(); black2.display(); black3.display(); white1.display(); white2.display(); log.info("--------------------- External state sharing mode ---------------------"); ExternalIgoChessman externalBlack1,externalBlack2,externalBlack3,externalWhite1,externalWhite2; ExternalIgoChessmanFactory externalFactory; //Get meta factory object externalFactory = ExternalIgoChessmanFactory.getInstance(); //Get three sunspots through Xiangyuan factory externalBlack1 = externalFactory.getIgoChessman("b"); externalBlack2 = externalFactory.getIgoChessman("b"); externalBlack3 = externalFactory.getIgoChessman("b"); System.out.println("Judge whether the two sunspots are the same:" + (externalBlack1==externalBlack2)); //Obtain two white sons through Xiangyuan factory externalWhite1 = externalFactory.getIgoChessman("w"); externalWhite2 = externalFactory.getIgoChessman("w"); System.out.println("Judge whether the two Baizi are the same:" + (externalWhite1==externalWhite2)); //Display the chessmen and set the coordinate position of the chessmen externalBlack1.display(new Coordinates(1,2)); externalBlack2.display(new Coordinates(3,4)); externalBlack3.display(new Coordinates(1,3)); externalWhite1.display(new Coordinates(2,5)); externalWhite2.display(new Coordinates(2,4)); } }

Summary

The advantage of meta mode is that it greatly reduces the number of objects in memory. However, the cost of doing so is also high:

  • The meta model makes the system more complex. In order to share objects, some states need to be externalized, which complicates the logic of the program.
  • The sharing mode externalizes the state of the sharing object, and reading the external state makes the running time slightly longer.

25 October 2021, 02:27 | Views: 9475

Add new comment

For adding a comment, please log in
or create account

0 comments