Java design mode to enjoy the meta-pattern of a detailed example

Source: Internet
Author: User

This article describes the Java design pattern of the sharing meta-mode. Share to everyone for your reference, as follows:

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:

    1. Package Flyweight;
    2. Public abstract class flyweight{
    3. public abstract void operation ();
    4. }
Copy Code

Implement a specific class:

    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. }
Copy Code

Implement a factory method class:

    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. }
Copy Code

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:

  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. }
Copy Code

Here is the result of the operation:

Concrete---Flyweight:google
Concrete---FLYWEIGHT:QUTR
Concrete---Flyweight:google
Concrete---Flyweight:google
Concrete---Flyweight:google
Concrete---Flyweight:google
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:

    1. String a = "Hello";
    2. String b = "Hello";
    3. if (a = = b)
    4. System.out.println ("OK");
    5. Else
    6. System.out.println ("Error");
Copy Code

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.

More Java-related content readers can view the topic: Java Data structure and algorithm tutorial, Java Operation DOM node tips summary, Java file and directory operations tips Summary and Java Cache Operations Tips Summary

http://www.ljhseo.com/
http://www.xyrjkf.net/
http://www.xyrjkf.cn/
http://www.xyrjkf.com.cn/
http://www.zjdygsi.cn/
http://www.zjdaiyun.cn/
http://www.jsdygsi.cn/
http://www.xyrjkf.top/
http://www.xyrjkf.com/
http://www.daiyunzj.cn/
http://ljhseo.com/
http://xyrjkf.net/
http://xyrjkf.cn/
http://xyrjkf.com.cn/
http://zjdygsi.cn/
http://zjdaiyun.cn/
http://jsdygsi.cn/
http://xyrjkf.top/
http://xyrjkf.com/
http://daiyunzj.cn/

Java design mode to enjoy the meta-pattern of a detailed example

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.