Java design mode---Enjoy meta mode

Source: Internet
Author: User

Enjoy meta mode

As the name implies: Shared meta-objects. If there are multiple identical objects in a system, then only one copy of the object needs to be shared, without
Create a new object for each use.

The enjoy meta-mode is one of the few design patterns that are only designed to improve system performance. Its main function is to reuse large objects (heavyweight objects) to save memory space and object creation time.

Pattern structure
Pattern structure
    1. Flyweight: Enjoy the meta interface, through which Flyweight can accept and act on the external state. It hurts. This interface can be passed in to an external state, and these external data may be used in the method processing of the enjoy Meta object.

    2. Concreteflyweight: The specific object of the realization of the share, must be shared, need to encapsulate the internal state of flyweight.

    3. Unshareconcreteflyweight: Non-shared sharing of the implementation object, not all flyweight implementation objects need to be shared. A non-shared privilege object is typically a composite object that is used for the object.

    4. Flyweightfactoty: Enjoy meta-factory, which is used primarily to create and manage shared-share objects, and to provide external access to shared-sharing elements.

    5. Client: The main job is to maintain a reference to the flyweight, calculate or store the external state of the meta, and of course, access to shared and unshared flyweight objects.

Code implementation UML diagram
Source
PublicInterfaceChessflyweight {voidSetColor (String c);StringGetColor ();voidDisplaycoordinate c);} @DataClassConcretechessImplementsChessflyweight {private String color;PublicConcretechess (String color) {super ();This.color = color; }PublicvoidDisplayCoordinate c) {System.Out.println ("Pawn Color:" + color); System.Out.println ("Pawn Position:" + c.getx () +"----" + c.gety ()); }}PublicClasschessflyweightfactory {Enjoy meta poolPrivatestatic Map<string,chessflyweight> Map =New hashmap<string, chessflyweight> ();Public ChessflyweightGetchess (String color) {if (map.Get (color)! =NULL) {return map.Get (color); }else{chessflyweight CFW =New concretechess (color); Map.put (color, CFW);return CFW; }}} @Data @allargsconstructorPublicClasscoordinate {Privateint x, y;}PublicClassClient {public static void main (string[] args) {Chessflyweightfactory factory = new chessflyweightfactory (); Chessflyweight Chess1 = factory.getchess ( "Black"); Chessflyweight chess2 = factory.getchess ( "Black"); System. out.println (CHESS1); System. out.println (CHESS2); System. out.println ( "increase the processing of external states ==========="); Chess1.display (new coordinate (10, 10)); Chess2.display ( new coordinate (20, 20));}}   
Advantages of the model's pros and cons model
    1. Reduce the number of objects, saving memory space.
Disadvantages of the pattern
    1. Maintaining shared objects requires additional overhead. (e.g., a thread to recycle rubbish)
Thinking

Model Nature: Separation and sharing

Application Scenarios in development:
    1. Enjoy meta mode because of its shared characteristics, you can operate in any "pool", such as: thread pool, database connection pool.

    2. The design of the string class is also the enjoy meta mode.

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.