Enjoy meta-mode (structured)

Source: Internet
Author: User

Thinking question: Design a go game, simulate a chess action, how to design?
Answer: Very straightforward, we will design a chessboard class chessboard, a pawn class chesspiece, each time the next piece is a new pawn object (incoming color, position), and then put these pieces into a container.

This simple and rude way does solve the problem, but you will find that the pawn is always only black and white, the function of the pawn object is the same, the main change is only (x, y) position, it can be independent of (x, y) to the caller management (rather than record to the inside of the pawn), Let the pawn object become a reusable object? This avoids the creation of a large number of pawn objects, which is the enjoyment of meta-mode (the share of the pieces of the state can not be shared to the caller management, mode only manage (reuse) pieces to enjoy the yuan).


enjoy meta-interface role : This role abstracts the class of the privilege, and an operation that requires an external state can be passed in as a parameter to the enjoyment meta object.
specific shared meta-object role : This role implements the business of sharing the meta-interface definition, noting that the internal state of the metadata object is independent of the environment, thus allowing the enjoyment meta-object to share within the system.
enjoy meta-factory role : The role is to construct a pool container, responsible for creating and managing the privilege role, and providing a way to get objects from the pool container, to ensure that the sharing meta role will check if there is a qualifying object in the system, if it already exists, The existing meta-object is provided by the Mövenpick factory, otherwise a suitable object of the privilege is created.
Client role : The role needs to store the external state of all the objects on its own.

Two concepts:

    1. Internal state: Information that can be shared, such as pawn color and size attributes, stored inside the object, and does not change as the environment changes (such as the size of a piece does not change with the coordinates);

    2. External state: is a state (such as a coordinate) that changes and cannot be shared as the environment changes, and the external state of the enjoyment meta object must be saved by the client (if All black pieces refer to the same object after the pawn is set to the element mode, they cannot save their position), and after the object is created, In the use of the time to pass to the inside of the object to do operation.

Enjoy meta mode usage scenarios:
1. There are a large number of similar objects (such as chess pieces) in the system that consume a lot of memory.
2. Fine-grained objects have close external states (such as the coordinates of a pawn are external states), and the internal state (such as the color and size of a piece) is not related to the environment, i.e. the object has no specific identity.
3. Scenarios where buffer pools are required.

The class diagram of a pawn:

Instance code:

The factory of the pawn to enjoy the yuan:

 PackageCom.shusheng.flyweight;ImportJava.util.concurrent.ConcurrentHashMap;/** pieces of the factory of the yuan * * Public  class chesspiecefactory {    Private StaticConcurrenthashmap<string,chesspieceflyweight> Conhashmap =NewConcurrenthashmap<> ();//This is a thread-safe and highly efficient container    The /** factory method is used to get the pawn to enjoy the Meta object * /     Public StaticChesspieceflyweightGetchesspieceflyweight(String color) {Chesspieceflyweight flyweight = conhashmap.get (color);if(flyweight==NULL) {flyweight =NewChesspieceflyweight (color);        Conhashmap.put (Color,flyweight); } System.out.println ("Capacity of the container"+conhashmap.size ());returnFlyweight }}

The abstract interface of a pawn's element:

package com.shusheng.flyweight;/**抽象棋子应该有的动作*/publicinterface ChesspieceI {    /**下棋动作*/    publicvoidput(int x,int y); }

The specific realization of the pieces of the element:

 PackageCom.shusheng.flyweight;/** Chess piece * / Public  class chesspieceflyweight implements chesspiecei{    PrivateString color;//chess piece colorChesspieceflyweight (String color) {//The object construction method is not public, so that the external user is not directly new to the object.          This. color = color; }/** Chess Action * *    @Override     Public void put(intXintY) {//Here simple processing, the actual system, should be in the interface to draw that piece. System.out.println ("I Am"+color+"The color piece, I was under the position is x="+x+", y="+y+"; \ n"); }}

The client is used to simulate the external invocation of the enjoy meta mode:

 PackageCom.shusheng.flyweight;/** Client role, you need to save all the external states of the access meta object yourself * / Public  class Client {     Public Static void Main(string[] args) {Chesspieceflyweight Chesspiece1 = Chesspiecefactory.getchesspieceflyweight ("BLACK"); Chesspiece1.put (Ten,Ten);//Black Chess down to (10,10)Chesspieceflyweight Chesspiece3 = Chesspiecefactory.getchesspieceflyweight ("BLACK"); Chesspiece3.put ( -,Ten);//Black Chess down to (20,10)Chesspieceflyweight Chesspiece2 = Chesspiecefactory.getchesspieceflyweight ("White"); Chesspiece2.put (Ten, -);//White down to (10,20)Chesspieceflyweight Chesspiece4 = Chesspiecefactory.getchesspieceflyweight ("White"); Chesspiece4.put ( -, -);//White down to (20,20)}}


From the running results can be seen, no matter how many pieces, the final pieces of the object are only two, is not a lot less than the beginning of the design of the object, save a lot of memory it?

Benefits of the Enjoy meta-mode:
1. Significantly reduce the number of objects in memory, reduce the memory consumption of the program, improve performance.
Disadvantages:
1. Enjoy meta mode adds complexity to the system (from the above design, a good new process is quite complicated);
2. It is necessary to divide the external state and the internal state, and the internal state has the curing characteristic, should not change with the external state change (such as the chess piece color size and so on does not change with the position change), this makes the procedure logic complication.
3. The enjoy meta-mode will allow the state of the meta-object to be externally instantiated (referring to the external state), while reading the external state makes the program run for a long time (such as selecting a Pawn object and can no longer know where it is located by the object).

Enjoy meta-mode (structured)

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.