The development structure of flyweight pattern in Java design pattern programming _java

Source: Internet
Author: User

(Flyweight) mode: The sharing of technology to effectively support a large number of fine-grained objects.
In Shanhong's "Java and Mode", the model is divided into simple and complex modes, the compound-mode is not to be shared, and the key to share is to distinguish the intrinsic state (the Internal state) and the outer Yun (External). These two "Yun" translation is too difficult to understand, I am not saying that translation is not good, may be my ability to understand poor, or "design patterns Elements of reusable object-oriented Software" of the translation version of the Design pattern Reusable Object-oriented software The book is always translated into internal objects and external objects, relatively straightforward, the concept of things that the literary smell is too strong to feel very awkward. The role here also uses the "design pattern reusable object-oriented software," the argument, does not distinguish between pure and composite mode, but there is a unsharedconcreteflyweight (in the "Java and mode," said the composite element, indicating that the composite element can not be shared), We say here that it is not allowed to share the role of the element, so the role of the element pattern is:

    • Abstract (Flyweight) role: Is the interface provided to the implementation element.
    • Specific (concreteflyweight) role: Implements an abstract role that must be shared and must contain a state that is internal.
    • Do not share the Unsharedconcreteflyweight role: This object is not shared, not all objects that implement the abstract interface are shared, and this object usually concreteflyweight as a constituent element.
    • The Flyweightfactory role: responsible for creating and managing the role of the element to ensure proper sharing.
    • Client role: Maintains a reference to a flyweight object, calculating or storing one (multiple) external storage state.

The organization diagram for the class of the mode of entitlement is as follows:

The use of the java.lang.String pattern in the design of the system, we know that the strings in Java are always shared in one copy, such as the following code fragment:

String m = "a";
String n = "a";
System.out.println (m==n);

This will output true, stating that M and N point to the same instance and that there is only one "a" in memory. This is the use of the meta pattern on string.

The use of the meta mode in the process of editing and storing text, this assumes that the article consists of a row object that consists of several character objects, but if each character holds its own object, then a story of thousands of character objects, which consumes the system memory severely, resulting in unacceptable run-time overhead, A good way is to use the pattern, only to save the ASCII character encoding value, as the internal invariant state, when the character objects are shared, while the relative character color, size, such as the format of the data as an external state, maintained by the client, runtime by the external pass. Each row acts as an Unshared object, which is a combination of the element objects (character objects).

Let's look at a simple example of the structure of the meta schema:

/** 
 * Letter 
{ 
 
 private String name; 
 
 Public letter (String name) { 
  this.name = name; 
 } 
 
 Public String GetName () {return 
  name; 
 } 
} 
/** * An exclusive factory that generates alphabetic objects (single factory)/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 ' {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 alphabetic object 
 static void Addletterbyname (Letterfactory factory, String Word) {for 
  (char C:word.tochararray ()) {
   factory.add (New letter (C + "")); 
  } 
 Output alphabetic object 
 static void Getletter (Letterfactory factory, String Word) {for 
  (char C:word.tochararray ()) { 
   S Ystem.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 

Related Article

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.