An overview,

  • Flyweight Pattern: also known as flylight Pattern, sharing technology effectively supports a large number of fine-grained objects
  • It is often used for low-level system development to solve system performance problems. Like a database connection pool, which is filled with created connection objects. If there is something we need in these connection objects, we can use it directly to avoid repeated creation.
  • The share mode can solve the memory waste problem of duplicate objects. When there are a large number of similar objects in the system and the buffer pool is needed, there is no need to always create new objects, you can take them from the buffer pool. This reduces system memory and improves efficiency.
  • Classic application scenario: pool technology. String constant pools, database connection pools, buffer pools, and so on are all examples of meta-schema applications. Share mode is an important way to realize pool technology
  • Internal state and external state
    • Internal state refers to the information shared by the object, stored inside the share object and not changing with the environment
    • An external state is a token that an object can depend on. It is a state that changes with the environment and is not shareable

Two, enjoy yuan mode in the use of chess

  • In a game hall, there are multiple rooms playing chess, each room has a different chess layout, but the style of the pieces are the same. Thus, the style of chess is the internal state and the layout of positions is the external state. All game halls can share a single chess set, but the placement of pieces in each room is different.
  • Single chess piece style, all rooms share one
// The style of a single piece
public class ChessPieceUnit {
  private int id;
  private String text;
  private Color color;

  public ChessPieceUnit(int id, String text, Color color) {
    this.id = id;
    this.text = text;
    this.color = color;
  }

  public static enum Color { RED, BLACK; }}Copy the code
  • A class that contains the style of the chess piece and its position coordinates
// a class that contains the chess pieces and their positions
public class ChessPiece {
  private ChessPieceUnit unit;
  private int positionX;
  private int positionY;

  public ChessPiece(ChessPieceUnit unit, int positionX, int positionY) {
    this.unit = unit;
    this.positionX = positionX;
    this.positionY = positionY; }}Copy the code
  • Chess factory, one piece of chess can provide all rooms
public class ChessPieceUnitFactory {
  private static final Map<Integer, ChessPieceUnit> pieces = new HashMap<>(); // A warehouse where all the pieces are stored

  static {
    pieces.put(1.new ChessPieceUnit(1."Car", ChessPieceUnit.Color.BLACK));
    pieces.put(2.new ChessPieceUnit(2."Horse", ChessPieceUnit.Color.BLACK));
    / /... Omit the code for placing other pieces...
  }

  /** * Get the chessman * by ID@param chessPieceId
   * @return* /
  public static ChessPieceUnit getChessPiece(int chessPieceId) {
    returnpieces.get(chessPieceId); }}Copy the code
  • The board
public class ChessBoard {
  private Map<Integer, ChessPiece> chessPieces = new HashMap<>();

  public ChessBoard(a) {
    init();  // Initialize the position coordinates of the pieces
  }

  private void init(a) {
    chessPieces.put(1.new ChessPiece(ChessPieceUnitFactory.getChessPiece(1), 0.0));
    chessPieces.put(1.new ChessPiece(ChessPieceUnitFactory.getChessPiece(2), 1.0));
    / /... Omit the code for placing other pieces...
  }

  public void move(int chessPieceId, int toPositionX, int toPositionY) {
    / /... Omit...}}Copy the code