Java design mode--Enjoy meta mode

Source: Internet
Author: User

Overview

When there are a large number of objects, it is possible to cause memory overflow, we abstract the common part, if there is the same business request, directly return in memory existing objects, avoid re-creation.

Copyright notice

Copyright belongs to the author.
Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.
This article Coding-naga
Published: April 25, 2016
This article link: http://blog.csdn.net/lemon_tree12138/article/details/51241598
Source: CSDN
More information: Classification >> Design Patterns

Directory

    • Overview
    • Copyright notice
    • Directory
    • Enjoy meta mode
      • Defined
      • Simply enjoy meta mode
      • No sharing of the enjoy meta mode
      • Compound enjoy meta mode
    • Enjoy meta-mode in JDK
    • Ref

Enjoy meta-mode definition

The enjoy meta-mode is a kind of structural pattern, which uses the sharing technology to effectively support a large number of fine-grained objects.

Simply enjoy meta mode

In the enjoy meta-mode, we can construct a simple-to-enjoy model for its definition. is a class diagram that simply enjoys meta-mode:

In the simple-to-enjoy model, an abstract interface is needed to specify the methods that are required for all the specific privileges. The implementation is as follows:
Flyweight.java

publicinterface Flyweight {    publicvoidoperation(String state);}

For the Concreteflyweight section, the share must be shareable, any state it holds must be internal (intrinsic), and Concreteflyweight must be independent of its application environment. For example, the string "Hello" does not have to care about the occasion of using it, it is an immutable object.
Concreteflyweight.java

 Public  class concreteflyweight implements Flyweight {    PrivateString intrinsicstate =NULL;/** * Constructor intrinsic state passed in as parameter * /     Public Concreteflyweight(String _intrinsicstate) { This. intrinsicstate = _intrinsicstate; }/** * The behavior of the external state as a parameter in the method of changing the method but does not change the intrinsic state of the object * *    @Override     Public void Operation(String extrinsicstate) {System.out.println ("intrinsic state:"+ intrinsicstate); System.out.println ("Outer Yun State:"+ extrinsicstate); }}

In the enjoy meta-mode, one of the most important modules is the factory module. A Flyweight pool is maintained in the Flyweight Factory (the internal state is stored), and the Flyweight Factory is controlled through the Flyweight pool for the entire sharing mode.
Flyweightfactory.java

public  class  flyweightfactory  { Span class= "Hljs-keyword" >private  map<integer, flyweight> labels = new     Hashmap<integer, flyweight> (); public  Flyweight factory  (String intrinsicstate) {int  ha        Shcode = Intrinsicstate.hashcode ();        Flyweight fly = Labels.get (hashcode); if  (Fly = = null )            {fly = new  concreteflyweight (intrinsicstate);        Labels.put (Hashcode, fly);    } return  fly; }}

As you can see in the factory module above, Flyweightfactory is actually a filtering filtering feature that filters duplicate objects and caches new objects.

No sharing of the enjoy meta mode

In contrast to a shared share object, it is an unshared object that is not shared. With respect to unshared objects, you may have some questions about why you might want to include it in the enjoy meta mode, since you don't share it. This may be due to completeness, or if you want to use shared shares in some scenarios, you should also use the non-shared rewards. I have not fully understood this point, and it is possible that you do not need this shared object. The following is an unshared class diagram:

The concreteflyweight of sharing is no different from the same. As follows:
Unsharedconcreteflyweight.java

public  class  unsharedconcreteflyweight  implements  flyweight  { private  String intrinsicstate = null ; public  unsharedconcreteflyweight  (String _intrinsicstate) {this . intrinsicstate = _intrinsicstate; }  @Override  public  void  operation  (String extrinsicstate) {System.out.println ( "intrinsic state:" + intrinsicstate); System.out.println ( "outer Yun State:"  + extrinsicstate); }} 

Here is the code to test for pure and unshared sharing.
Client.java

 Public  class Client {     Public Static void Main(string[] args) {Flyweightfactory factory =NewFlyweightfactory (); Flyweight fly1 = Factory.factory ("Hello"); Fly1.operation ("ExtrinsicState-1"); Flyweight fly2 = Factory.factory ("Designpattern"); Fly2.operation ("ExtrinsicState-2"); Flyweight Fly3 = Factory.factory ("Flyweight"); Fly3.operation ("ExtrinsicState-3"); Flyweight Fly4 = Factory.factory ("Hello"); Fly4.operation ("ExtrinsicState-4"); System.out.println ("fly1 = = fly2?"+ (Fly1 = = fly2)); System.out.println ("fly1 = = fly3?"+ (Fly1 = = Fly3)); System.out.println ("fly1 = = fly4?"+ (Fly1 = = Fly4)); Flyweight fly5 =NewUnsharedconcreteflyweight ("unshared"); Fly5.operation ("ExtrinsicState-5"); }}
内蕴状态:Hello外蕴状态:ExtrinsicState-1内蕴状态:DesignPattern外蕴状态:ExtrinsicState-2内蕴状态:Flyweight外蕴状态:ExtrinsicState-3内蕴状态:Hello外蕴状态:ExtrinsicState-4falsefalsetrue内蕴状态:Unshared外蕴状态:ExtrinsicState-5
Compound enjoy meta mode

In the above simple-to-enjoy meta, all of the elements can be said to be in the same group. Sometimes such a large collection may not be able to play a good application function, so the introduction of a composite of the element of the model. The following is a composite enjoy meta-mode class diagram:

The compound is equivalent to a collection of multiple pure elements. In other words, the compound-sharing element is equivalent to a re-grouping of the pure-sharing element, and it is an independent simple-to-enjoy meta-mode in each grouping of the compound-sharing element. The relevant key codes are as follows:
Concretecompositeflyweight.java

 Public  class concretecompositeflyweight implements Flyweight {    PrivateMap<integer, flyweight> labels =NewHashmap<integer, flyweight> (); Public void Add(intKey, Flyweight Flyweight) {labels.put (key, Flyweight); }@Override     Public void Operation(String extrinsicstate) {Flyweight Flyweight =NULL; for(Object Key:labels.keySet ())            {flyweight = Labels.get (key);        Flyweight.operation (extrinsicstate); }    }}

There is also a need to redesign the plant for its construction, as follows:
Flyweightfactory.java

 Public  class flyweightfactory {    PrivateMap<integer, flyweight> labels =NewHashmap<integer, flyweight> ();/** * Simple to enjoy the yuan factory * *     PublicFlyweightFactory(String intrinsicstate) {         ... ...returnFly }/** * Compound to enjoy the yuan factory * *     PublicFlyweightcompositefactory(list<string> intrinsicstates) {Concretecompositeflyweight flyweight =NewConcretecompositeflyweight (); for(String intrinsicstate:intrinsicstates)        {Flyweight.add (Intrinsicstate.hashcode (), Factory (Intrinsicstate)); }returnFlyweight }}

Test class
Client.java

 Public void Compositeflyweight() {list<string> intrinsicstates =NewArraylist<string> (); Intrinsicstates.add ("Hello"); Intrinsicstates.add ("Java"); Intrinsicstates.add ("Designpattern"); Intrinsicstates.add ("Flyweight"); Flyweightfactory factory =NewFlyweightfactory ();        Flyweight flyweight1 = factory.compositefactory (intrinsicstates);        Flyweight flyweight2 = factory.compositefactory (intrinsicstates); System.out.println ("flyweight1 = = flyweight2?"+ (flyweight1 = = flyweight2)); Flyweight1.operation ("Compound-1"); Flyweight2.operation ("Compound-2"); }
false内蕴状态:Java外蕴状态:复合享元-1内蕴状态:Flyweight外蕴状态:复合享元-1内蕴状态:Hello外蕴状态:复合享元-1内蕴状态:DesignPattern外蕴状态:复合享元-1内蕴状态:Java外蕴状态:复合享元-2内蕴状态:Flyweight外蕴状态:复合享元-2内蕴状态:Hello外蕴状态:复合享元-2内蕴状态:DesignPattern外蕴状态:复合享元-2
Enjoy meta-mode in JDK

In the JDK design, there is also a very good meta-mode. For example, some constant pool design (String constant pool, Integer Chang, etc.);

Ref
    • "23 Java Design Patterns"
    • Http://www.runoob.com/design-pattern/flyweight-pattern.html
    • Http://www.cnblogs.com/java-my-life/archive/2012/04/26/2468499.html

Java design mode--Enjoy 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.