The 12th chapter of the design pattern-Enjoy meta-mode (Java implementation)

Source: Internet
Author: User

The 12th chapter of the design pattern-Enjoy meta-mode (Java implementation)

  "What's going on, there's a outofmemory error." Fish brother, come and help me see AH. "Is there a reason for tracking errors?" Is it a memory leak? "Not memory leaks Ah, specific reasons do not know AH." Yes, it says new objects don't apply to memory space. "" For this reason, I wrote a blog post: outofmemory simple analysis. But you obviously because of the code problem, the production of too many objects, resulting in memory exhaustion, just a lesson, will be able to solve your problem. (Hey, it's my turn to enjoy the meta-mode)

Self-introduction of the meta-mode of enjoyment

I, the enjoy meta-mode is an important implementation of pool technology, specifically defined as follows: Use sharing to support large numbers of fine-grained objects efficiently. It means that the use of shared objects can effectively support a large number of fine-grained objects. Two requirements are defined by definition: fine-grained objects and shared objects. The problem with the person in front of you is a memory overflow, because allocating too many objects results in a memory overflow and, of course, the performance of the program. So how to fix it, right is the sharing technology I have provided. Before that, let's look at the external state of the object and its internal state.

Fine-grained objects inevitably cause a large number of objects and are similar in nature, so we divide these objects into two parts, the external state and the internal state. The following explains the internal and external states:

    • External state: A token that an object is dependent on, changes with the environment, and cannot be shared, such as a user name and information such as an ID.
    • Internal state: The information that an object can share, stored inside the object of the enjoyment and not changed with the environment, does not have to be stored in a specific object, belonging to a shared part.

  My class diagram is as follows:

The function of each class in the class diagram and the detailed introduction, I will not talk nonsense.

Self-analysis of the meta-mode of enjoyment

Advantages:

    • Reduce the objects created by the application.
    • Reduce program memory consumption.
    • Enhance program performance.

  Disadvantages:

    • Increased system complexity.
    • The external state and internal state need to be detached.

The realization of the mode of enjoying meta

  as the saying goes, there is no silver bullet, but there is a generic implementation of the code, my level is so high, naturally will use the common code to implement my pattern ~ My mode I take the lead, the first is the abstract enjoy meta role:

1  Public Abstract classflyweight{2     //Internal State3     PrivateString intrinsic;4     //External State5     protected FinalString extrinsic;6     //require the enjoy meta role to accept external states7      PublicFlyweight (String extr) {8          This. extrinsic =extr;9     }Ten     //Defining Business Actions One      Public Abstract voidoperate (); A  -     //Getter/setter of the internal state -      PublicString Getintrinsic () { the         returnintrinsic; -     } -  -      Public voidSetintrinsic (String intrinsic) +     { -          This. intrinsic =intrinsic; +     } A}
View Code

  Abstract enjoy meta-role is generally abstract class, in the actual project, is generally an implementation class, it is a way to describe a class of things, in the abstract role, the general need to define the external state and internal state, to avoid arbitrary expansion of subclasses, the following is the specific implementation of the code to enjoy meta-role:

1  Public classConcreteflyweightextendsflyweight{2     //Accept External State3      Publicconcreteflyweight (String extri) {4         Super(Extri);5     }6     //logical processing based on external state7      Public voidoperate () {8         //Business Logic9     }Ten}

  This is the reason for implementing your own business logic and then receiving external states so that the internal business logic relies on external states, and adding the final keyword to the abstract privilege is also a cause, in order to prevent the pool from being confused by no-comment modification.

Here is the Mövenpick factory:

1  Publicflyweightfactory{2     //Defining Containers3     Private StaticHashmap<string, flyweight> pool =NewHashmap<>();4     //enjoy the Yuan factory5      Public StaticFlyweight getflyweight (String extri) {6         //objects that need to be returned7Flyweight Flyweight =NULL;8         //determine if the object is in the pool9         if(Pool.containskey (Extri)) {TenFlyweight =Pool.get (Extri); One         } A         Else{ -             //Create a Privilege object based on an external state -Flyweight =Newconcreteflyweight (Extri); the Pool.put (Extri, flyweight); -         } -         returnFlyweight; -     } +}
View Code

The general code is so much, can according to the specific needs of the inside set ~ specific I will not say more.

Usage scenarios for the enjoy meta-mode

When you encounter the following situations, use me, the effect is the leverage:

    • An application uses a large number of objects.
    • Because of the high storage overhead caused by the use of a large number of objects.
    • Most states of an object are external states.
    • If you delete an object's external state, you can replace many group objects with a relatively small number of shared objects.
    • The application does not depend on the identity of the object.

  Above. That's all for today. Jing Ke Thorn qin King, design mode makes me strong ~

  

  PS: This blog welcome forwarding, but please specify the blog address and author ~

Blog Address: http://www.cnblogs.com/voidy/

Blog: http://voidy.gitcafe.com

<. ))) ≦

 

The 12th chapter of the design pattern-Enjoy meta-mode (Java implementation)

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.