JAVA design mode-Metadata Mode

Source: Internet
Author: User

Enjoy (Flyweight) mode: through the sharing technology to effectively support a large number of fine-grained objects. In java and mode, the metadata-sharing mode is divided into the simple metadata-sharing mode and the compound metadata-sharing mode. The compound metadata-sharing mode cannot be shared, the translation of Design Pattern Elements of Reusable Object-Oriented Software, Design Pattern Reusable basics of object-oriented Software, is always translated into internal and external objects, which is relatively straightforward. The role here also uses the statement "design patterns can reuse the basics of Object-Oriented Software", without distinguishing between simple and compound models, instead, there is an UnSharedConcreteFlyweight (in java and mode), which indicates that the compound metadata cannot be shared. Here we say that it cannot share the metadata role. In this way, the role in the meta mode includes: abstract: The Flyweight role is an interface provided to implement the metadata. ConcreteFlyweight role: implements an abstract role. This object must be shared and its status must be internal. UnSharedConcreteFlyweight: This object cannot be shared. Not all objects that implement the abstract metadata interface must be shared. This object usually uses ConcreteFlyweight as the component element. FlyweightFactory role: Creates and manages the metadata to ensure reasonable sharing. Client role: maintain a reference to a Flyweight object, compute or store one or more external storage states. The structure of the class in the metadata mode is as follows: the metadata mode is in java. lang. we know that the String in java is always shared, as shown in the following code snippet: String m = "a"; String n = "a"; System. out. println (m = n); this outputs true, indicating that m and n point to the same instance and there is only one "a" in the memory ". This is the usage of the Meta mode on the String. The metadata mode is used in the text editing and storage process. It is assumed that the article consists of row objects and row objects consist of several character objects. However, if each character saves its own object, therefore, an article contains thousands of character objects, which seriously consumes system memory and leads to unacceptable runtime overhead. A good way is to use the enjoy meta mode and only save the ASCII character encoding value, as an internal constant state, when a character object is shared, and formatted data such as the color and size of the character is used as an external State, maintained by the client and passed in by the outside during running. Each row can be used as a non-shared object. It is composed of a shared object (character object. Java code collection code package flyWeight;/*** Description: Abstract meta */public interface Glyph {public void draw (Context context);} package flyWeight;/*** description: specific metadata */public class Character implements Glyph {private char c; private int size; @ Override public void draw (Context context) {this. size = context. getSize (); System. out. println (size + "Number" + c + "is drawn! ");} Public char getC () {return c;} public void setC (char c) {this. c = c;} public Character (char c) {super (); this. c = c; System. out. println (c + "created! ") ;}} Package flyWeight; import java. util. arrayList; import java. util. list;/*** Description: The Row cannot be shared */public class Row implements Glyph {private List <Character> list = new ArrayList <> (); @ Override public void draw (Context context) {} public Row () {} public void setCharacter (Glyph r) {list. add (Character) r);} public int getSize () {return list. size ();} public String getRow () {StringBuilder sb = new StringBuilder (); for (Character g: list) {sb. append (g. getC ();} return sb. toString () ;}} package flyWeight; import java. util. hashMap; import java. util. map;/*** Description: */public class GlyphFactory {private Map <String, Glyph> map = new HashMap <> (); public Glyph getGlyph (Context context) {String cStr = context. getC () + ""; Glyph gl = map. get (cStr); if (gl = null) {gl = new Character (context. getC ()); Map. put (cStr, gl);} gl. draw (context); return gl ;}} package flyWeight;/*** Description: Data class */public class Context {private int size; private char c; public int getSize () {return size;} public void setSize (int size) {this. size = size;} public char getC () {return c;} public void setC (char c) {this. c = c;} public Context (int size, char c) {super (); this. size = size; this. c = c ;}} package flyW Eight;/*** Description: The Client directly writes */public class Client {public static void main (String [] args) in the main method for simplicity) {Row r = new Row (); GlyphFactory factory = new GlyphFactory (); Context context1 = new Context (12, 'A'); Glyph gly1 = factory. getGlyph (context1); r. setCharacter (gly1); Context context2 = new Context (13, 'A'); Glyph gly2 = factory. getGlyph (context2); r. setCharacter (gly2); Context context3 = new Context (13, 'B'); Glyph gly3 = factory. getGlyph (context3); r. setCharacter (gly3); System. out. println (r. getRow () ;}} running result: a is created! On the 12th, a was drawn! On the 13th, a was drawn! B is created! Draw B on the 13th! Aab can be seen that a is created once. The size of a is 12, and 13 is the external state, so it is the external input. The external State cannot be saved in the Meta, and the character a is the internal state, row is composed of Character. Although Row implements the abstract metadata interface, it does not reflect sharing in the factory, because it is a non-shareable metadata.

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.