Flyweight mode: Through sharing technology, it can effectively support a large number of fine-grained objects.
The Enjoy Yuan mode is divided into simple Enjoy Yuan mode and composite Enjoy Yuan mode in Yan Hong's "Java and Mode". The composite Enjoy Yuan in the composite mode cannot be shared. The key to sharing the Enjoy Yuan object is to distinguish between internal and external states. These two "congruents" are too difficult to understand. I am not saying that the translation is not good, but maybe my understanding ability is poor, or the translation version of "Design Pattern Elements of Reusable Object-Oriented Software" is translated into internal and external objects. It is relatively straightforward and feels awkward to the literary smell of conceptual things that are too strong. The characters here also use the statement "Basics of Design Pattern Reusable Object-Oriented Software", which does not distinguish between simple modes and composite modes, but has an UnSharedConcreteFlyweight (in "Java and Mode", it indicates that composite enjoyment cannot be shared). We here say that it cannot share enjoyment roles. In this way, the characters in the enjoyment mode include:
The institutional diagram of the class of the Xiangyuan model is as follows:
When using the Encyclopedia pattern in java.lang.String design, we know that strings in java are always shared, such as the following code snippet:
String m = "a";String n = "a";System.out.println(m==n);
This will output true, indicating that m and n point to the same instance, and there is only one "a" in memory. This is how the Enjoy Yuan mode is used on String.
The use of the Xiangyuan mode in the text editing and storage process. Here, it is assumed that the article is composed of line objects and the line objects are composed of several character objects. However, if each character saves its own object, then a post has thousands of character objects, which seriously consumes system memory and causes unacceptable runtime overhead. The good method is to use the Xiangyuan mode to only save ASCII character encoding values, as an internal unchanging state, share the character objects, and formatted data such as relative character color and size are maintained by the client, and can be passed in from the outside during runtime. Each row is a non-shared universal object, which is composed of a universal object (character object).
Let's take a look at an example of a simple structure of the Encyclopedia pattern:
/** * Letter*/ public class Letter { private String name; public Letter(String name) { this.name = name; } public String getName() { return name; } } /** * A Xiangyuan factory (singleton factory) that generates letter objects */ public class LetterFactory { private Map<String, Letter> map; private static LetterFactory instance = new LetterFactory(); private LetterFactory() { map = new HashMap<String, Letter>(); } public static LetterFactory getInstance() { return instance; } public void add(Letter letter) { if (letter != null && !map.containsKey(letter.getName())) { map.put(letter.getName(), letter); } System.out.println("map.size====" + map.size()); } public Letter get(String name) { return map.get(name); } } public class Test { public static void main(String[] args) { LetterFactory factory = LetterFactory.getInstance(); String word = "easiness"; addLetterByName(factory, word); getLetter(factory, word); } //Add letter object static void addLetterByName(LetterFactory factory, String word) { for (char c : word.toCharArray()) { factory.add(new Letter(c + "")); } } //Output letter object static void getLetter(LetterFactory factory, String word) { for (char c : word.toCharArray()) { System.out.println(factory.get(c + "")); } } }Print:
map.size====1 map.size====2 map.size====2 map.size=====3 map.size====4 map.size====5 map.size=====5 flyweight.Letter@3343c8b3 flyweight.Letter@272d7a10 flyweight.Letter@3343c8b3 flyweight.Letter@1aa8c488 flyweight.Letter@3dfeca64 flyweight.Letter@22998b08 flyweight.Letter@1aa8c488