Java Design Pattern modeling and implementation of cainiao series (21) enjoy metadata Pattern

Source: Internet
Author: User

Java Design Pattern modeling and implementation of cainiao series (21) enjoy metadata Pattern

 

 

Flyweight: uses the sharing technology to effectively support a large number of fine-grained objects. The main purpose is to share objects, that is, the shared pool. When there are many objects in the system, the memory overhead can be reduced. To some extent, You can regard a singleton as a special case of Yuan-sharing.

I. uml modeling:

 

Ii. Code Implementation

 

/*** Flyweight: uses the sharing technology to effectively support a large number of fine-grained objects. ** The main purpose is to share objects, that is, the shared pool. When there are many objects in the system, the memory overhead can be reduced. */Abstract class FlyWeight {public abstract void method ();}/*** create a subclass holding the key */class SubFlyWeight extends FlyWeight {private String key; public SubFlyWeight (String key) {this. key = key ;}@ Overridepublic void method () {System. out. println (this is the sub method, and the key is + this. key) ;}}/*** metadata Factory: Creates and manages metadata objects */class FlyweightFactory {private Map
 
  
Map = new HashMap
  
   
();/*** Get the object */public FlyWeight getFlyWeight (String key) {FlyWeight flyWeight = map. get (key); if (flyWeight = null) {flyWeight = new SubFlyWeight (key); map. put (key, flyWeight);} return flyWeight;}/*** get the number of object objects */public int getCount () {return map. size () ;}/ *** client Test class ** @ author Leo */public class Test {public static void main (String [] args) {/*** create a metadata factory */FlyweightFactory factory = new FlyweightFactory ();/***** first case: key is the same ***************/FlyWeight flyWeightA = factory. getFlyWeight (aaa); FlyWeight flyWeightB = factory. getFlyWeight (aaa);/*** you can know that the keys are aaa, so flyWeightA and flyWeightB point to the same memory address */System. out. println (flyWeightA = flyWeightB); flyWeightA. method (); flyWeightB. method ();/*** number of objects to be shared: 1 */System. out. println (factory. getCount ();/******* Case 2: Keys are inconsistent ****************/System. out. println (============================================ ); flyWeight flyWeightC = factory. getFlyWeight (ccc);/*** the print result is false */System. out. println (flyWeightA = flyWeightC); flyWeightC. method ();/*** number of objects to be shared: 2 */System. out. println (factory. getCount ());}}
  
 
Print result:

 

True
This is the sub method, and the key is aaa
This is the sub method, and the key is aaa
1
==============================================
False
This is the sub method, and the key is ccc
2

Iii. Summary

 

The differences between the shared object and the single instance: 1. Unlike the single instance mode, the shared object mode is a class that can have many objects (share a set of objects ), while Singleton is a class with only one object. 2. They have different purposes. The metadata mode is used to save memory space and improve program performance (avoid a large number of new operations ), the Singleton mode shares the status and features of a single object.

 

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.