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
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.
Concreteflyweight: The specific object of the realization of the share, must be shared, need to encapsulate the internal state of flyweight.
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.
Flyweightfactoty: Enjoy meta-factory, which is used primarily to create and manage shared-share objects, and to provide external access to shared-sharing elements.
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
- Reduce the number of objects, saving memory space.
Disadvantages of the pattern
- Maintaining shared objects requires additional overhead. (e.g., a thread to recycle rubbish)
Thinking
Model Nature: Separation and sharing
Application Scenarios in development:
Enjoy meta mode because of its shared characteristics, you can operate in any "pool", such as: thread pool, database connection pool.
The design of the string class is also the enjoy meta mode.
Java design mode---Enjoy meta mode