The Java design mode of "Enjoy meta mode" and its application scenario

Source: Internet
Author: User

Original works, can be reproduced, but please mark the source address: http://www.cnblogs.com/V1haoge/p/6542449.html

Enjoy meta mode: "Enjoy" is the meaning of sharing, refers to a thing is shared by the people, and this is the final purpose of the model.

The enjoy meta-mode is somewhat similar to a singleton pattern , which is to generate only one object for shared use. There is a problem here, that is the modification of the shared object, in order to avoid this situation, we extract the public parts of these objects, or the non-changing parts to form an object. This object can be avoided to the problem of modification.

The purpose of the privilege is to reduce memory consumption by reducing the amount of memory consumed, and by centralizing multiple accesses to the same object without creating a separate object for each visitor.

Let's look at a simple example:

Building interface: Jianzhu

1  Public Interface Jianzhu {2     void Use (); 3 }

Gymnasium Implementation class: Tiyuguan

1  Public classTiyuguanImplementsJianzhu {2     PrivateString name;3     PrivateString shape;4     PrivateString Yundong;5      PublicTiyuguan (String yundong) {6          This. Setyundong (Yundong);7     }8      PublicString GetName () {9         returnname;Ten     } One      Public voidsetName (String name) { A          This. Name =name; -     } -      PublicString Getshape () { the         returnshape; -     } -      Public voidsetshape (String shape) { -          This. Shape =shape; +     } -      PublicString Getyundong () { +         returnYundong; A     } at      Public voidSetyundong (String yundong) { -          This. Yundong =Yundong; -     } - @Override -      Public voidUse () { -System.out.println ("The stadium was used to hold the Olympic Games" + "Movement for:" + yundong+ "shape:" +shape+ "name is:" +name); in     } -}

Construction Factory class: Jianzhufactory

1 Importjava.util.*;2 3  Public classJianzhufactory {4     Private Static FinalMap<string,tiyuguan> Tygs =NewHashmap<string,tiyuguan>();5      Public StaticTiyuguan Gettyg (String yundong) {6Tiyuguan Tyg =Tygs.get (yundong);7         if(Tyg = =NULL){8Tyg =NewTiyuguan (yundong);9 Tygs.put (YUNDONG,TYG);Ten         } One         returnTyg; A     } -      Public Static intGetSize () { -         returntygs.size (); the     } -}

Test class: Clienter

1  Public classClienter {2      Public Static voidMain (string[] args) {3String Yundong = "Football";4          for(inti = 1;i <= 5;i++){5Tiyuguan Tyg =Jianzhufactory.gettyg (yundong);6Tyg.setname ("China Gymnasium");7Tyg.setshape ("Round");8 tyg.use ();9System.out.println ("The number of objects in the object pool is:" +jianzhufactory.getsize ());Ten         } One     } A}

Execution Result:

the stadium was used to hold the Olympic Games as follows: The shape of the football is: the  Circle  name is: China Stadium the number of objects in the pool is:1 The stadium was used to hold the Olympic Games  as: Football  The shape is: The circle  name is: China Stadium the number of objects in the pool is:1 The stadium was used to hold the Olympic Games as follows: The shape of the football is: the  circle name is:  China Stadium The number of objects in the pool is: 1 The stadium was used to hold the Olympic Games as follows: The shape of the football is: the  Circle  name is: China Gymnasium object Pool in the number of objects is:1 The stadium was used to hold the Olympic Games  The sport is: the shape of the football is: the  circle  name is: China Gymnasium Object Pool object number is:1

As in the example above, using Factory mode for mates, creating object pools, testing the loops in the class, you can imagine being held in 5 games, each game is the stadium

By executing the results, it can be seen that in this object pool (HASHMAP), there is always only one object, the first time the object is created, and then each call is using that object, no re-creation.

In fact, there are instances of this type in Java: String.

In Java, the string class is defined as final (immutable), and the strings in the JVM are generally stored in the string constant pool, which is in the constant pool before JDK 6.0, in the permanent generation, and in JDK 7.0, the JVM takes it from the permanent generation to the heap.

The two strings that we define with the following code are actually string values in the same string constant pool.

1 String s1 = "abc"; 2 String s2 = "abc";

If we compare with S1==S2, the result is: true, because S1 and S2 hold the same string address in the string constant pool. This is similar to the one we are talking about today, where strings can be shared once they are defined, because they are immutable, and there is no hidden danger of being called in multiple places.

Scenarios used by the enjoy meta mode:

When we create a lot of objects in our project and there are many identical modules, we can extract the same modules to generate a single object using the enjoy meta-mode, and then use this object to use with many previous objects, which will undoubtedly save a lot of space.

  

The Java design mode of "Enjoy meta mode" and its application scenario

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.