Java design mode of the sharing meta-mode

Source: Internet
Author: User

Explain the concept: that is, if there are multiple identical objects in a system, it is possible to share only one copy, without having to instantiate an object each. For example, a text system, each letter set an object, then the big lowercase letters are altogether 52, then you need to define 52 objects. If there is a 1M of text, then the letter is much more, if each letter is defined an object then the memory is already exploded. So if you share an object with each letter, you save a lot of resources.
In flyweight mode, Factory mode is often present in the flyweight (enjoy meta) mode because of the variety of objects to be produced. The internal state of the Flyweight is used for sharing, and Flyweight factory is responsible for maintaining an object storage pool (Flyweight pool) to hold the internal state of the object. Flyweight mode is a mode to improve the efficiency and performance of the program, which will greatly speed up the running of the program. There are a lot of applications, here's an example:
First, define an abstract flyweight class:

[Java]View PlainCopyprint?
    1. Package Flyweight;
    2. Public abstract class flyweight{
    3.  Public abstract void operation ();
    4. }

Implement a specific class:

[Java]View PlainCopyprint?
    1. Package Flyweight;
    2. Public class Concreteflyweight extends flyweight{
    3.  private string string;
    4.  Public concreteflyweight (String str) {
    5. string = str;
    6. }
    7.  Public void operation ()
    8. {
    9. System.out.println ("Concrete---Flyweight:" + string);
    10. }
    11. }

Implement a factory method class:

[Java]View PlainCopyprint?
  1. Package Flyweight;
  2. Import java.util.Hashtable;
  3. Public class flyweightfactory{
  4.  Private Hashtable flyweights = new Hashtable (); ----------------------------1
  5.  Public flyweightfactory () {}
  6.  Public Flyweight getflyweight (Object obj) {
  7. Flyweight Flyweight = (Flyweight) flyweights.get (obj); //----------------2
  8.   if (flyweight = = null) {//---------------------------------------------------3
  9.    Create a new Concreteflyweight
  10. Flyweight = new Concreteflyweight ((String) obj);
  11. Flyweights.put (obj, flyweight); //--------------------------------------5
  12. }
  13.   return flyweight; //---------------------------------------------------------6
  14. }
  15.  public int Getflyweightsize () {
  16.   return Flyweights.size ();
  17. }
  18. }

This factory method class is very critical, here is a detailed explanation:
A hashtable is defined at 1 to store each object, the object to instantiate at 2, the object is returned at 6, and if there are no objects to select in Hashtable, the variable flyweight is null. Produces a new flyweight stored in the Hashtable and returns the object.
Finally look at the invocation of flyweight:

[Java]View PlainCopy print?
  1. Package Flyweight;
  2. Import java.util.Hashtable;
  3. Public class flyweightpattern{
  4. Flyweightfactory factory = new Flyweightfactory ();
  5. Flyweight fly1;
  6. Flyweight Fly2;
  7. Flyweight Fly3;
  8. Flyweight Fly4;
  9. Flyweight fly5;
  10. Flyweight Fly6;
  11.  /** *//** Creates a new instance of Flyweightpattern * /
  12.  Public Flyweightpattern () {
  13. Fly1 = Factory.getflyweight ("Google");
  14. Fly2 = Factory.getflyweight ("Qutr");
  15. Fly3 = Factory.getflyweight ("Google");
  16. Fly4 = Factory.getflyweight ("Google");
  17. Fly5 = Factory.getflyweight ("Google");
  18. Fly6 = Factory.getflyweight ("Google");
  19. }
  20.  Public void Showflyweight () {
  21. Fly1.operation ();
  22. Fly2.operation ();
  23. Fly3.operation ();
  24. Fly4.operation ();
  25. Fly5.operation ();
  26. Fly6.operation ();
  27.   int objSize = Factory.getflyweightsize ();
  28. System.out.println ("objSize =" + objSize);
  29. }
  30.  Public static void Main (string[] args) {
  31. System.out.println ("The FlyWeight pattern!");
  32. Flyweightpattern fp = new Flyweightpattern ();
  33. Fp.showflyweight ();
  34. }
  35. }


Here is the result of the operation:

[Java]View PlainCopyprint?
    1. Concrete---Flyweight:google
    2. Concrete---FLYWEIGHT:QUTR
    3. Concrete---Flyweight:google
    4. Concrete---Flyweight:google
    5. Concrete---Flyweight:google
    6. Concrete---Flyweight:google
    7. ObjSize = 2


We have defined 6 objects, 5 of which are the same, according to the definition of flyweight mode "Google" should share an object, in the actual number of objects we can see that the actual object is only 2.

Summarize:
Flyweight (enjoy meta) mode is so important because it helps you to save a lot of memory space in a complex system. In the Java language, the string type is the use of the enjoy meta pattern. The string object is the final type, and the object cannot be changed once it is created. In Java, where string constants are present in a constant pool, Java ensures that a string constant has only one copy in the constant pool. String a= "abc", where "abc" is a string constant.

Familiar with Java should know the following example:

[Java]View PlainCopyprint?
    1. String a = "Hello";
    2. String B = "Hello";
    3. if (a = = b)
    4. System.out.println ("OK");
    5. Else
    6. System.out.println ("Error");

The output is: OK. You can see that the IF condition compares the addresses of two A and B, or memory space.
The core summary is that the objects that can be shared, that is, the same type of objects returned are actually the same instance, when the client asks to generate an object, the factory detects if there is an instance of this object, and if it exists then returns this object instance directly, and if it does not exist, creates one and saves it, which means some singleton patterns. Usually the factory class will have a member variable of the collection type to hold the object, such as Hashtable,vector. In Java, the database connection pool, the thread pool, and so on are the applications that use the enjoy meta-mode.

jason0539

Weibo: http://weibo.com/2553717707

Blog: http://blog.csdn.net/jason0539 (reprint please indicate the source)

Java design mode of the sharing meta-mode

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.