Java design pattern: from the [dotamap] analysis of the Flyweight Pattern

Source: Internet
Author: User

There are several hundred trees in the map of the Dota game. Now we assume that these trees are nothing more than the poplar and maple trees, with a total of 400 trees. When loading this map scenario, should we create objects for these 400 course trees? (For example, MapItem tree1 = new MapItem ("poplar"); MapItem tree2 = new MapItem ("Maple"); MapItem tree3 = new MapItem ("poplar ");......) Even if these statements are written in a loop, the workload of code writing is reduced. For the system, it is a resource-consuming task because the system generates 400 objects. This affects efficiency. So can we use some optimization technologies? Of course you can, that is, use the "meta-mode ". If you still don't understand it, you can see it later.

The intention of the meta-mode is to use the sharing technology to effectively support a large number of fine-grained objects. In the above example, we direct all the trees of the same type to the same object, which can save memory usage and improve program efficiency. Please refer to the following code:

Interface FlyweightItem {void draw ();} class FlyweightMapItem implements FlyweightItem {String name; public FlyweightMapItem (MapItem map) {this. name = map. name;} public void draw () {System. out. printf ("Drawing % s \ n", name) ;}} class MapItem {String name; public MapItem (String name) {this. name = name ;}} class FlyweightMapFactory {private static FlyweightMapFactory flyweightMapFactory = new FlyweightMapFactory (); private HashMap <String, Region> itemMap = new HashMap <String, FlyweightMapItem> (); private int itemCount = 0; public int getItemCount () {return itemCount;} public static FlyweightMapFactory getFlyweightMapFactory () {return flyweightMapFactory;} public FlyweightMapItem getItem (MapItem item) {if (itemMap. containsKey (item. name) {return itemMap. get (item. name);} else {FlyweightMapItem map = new FlyweightMapItem (item); itemCount ++; itemMap. put (item. name, map); return map ;}} class Flyweight {public static void main (String [] args) {FlyweightItem item1 = FlyweightMapFactory. getFlyweightMapFactory (). getItem (new MapItem ("Maple"); FlyweightItem item2 = FlyweightMapFactory. getFlyweightMapFactory (). getItem (new MapItem ("Maple"); FlyweightItem item3 = FlyweightMapFactory. getFlyweightMapFactory (). getItem (new MapItem ("poplar"); FlyweightItem item4 = FlyweightMapFactory. getFlyweightMapFactory (). getItem (new MapItem ("Maple"); item1.draw (); item2.draw (); item3.draw (); item4.draw (); System. out. println ("the number of objects in the element is:"); System. out. println (FlyweightMapFactory. getFlyweightMapFactory (). getItemCount ());}}


Suppose we are working on a map editor. All the map elements are MapItem classes. The name indicates the name of the map element. We use it to determine whether two elements are the same element. The MapItem object can be passed as a parameter to the FlyweightMapItem object. The FlyweightMapItem has a draw method, indicating that this element is drawn. FlyweightMapFactory is a factory class used to determine the uniqueness of an element. It has a Map internally to record whether the element has been produced. When getItem is called, if the object has already been produced, the production object is directly returned. Otherwise, a new object is created, added to the Map, and then returned. The factory must be unique in this example, so it adopts the singleton mode.

Finally, the output result is:

Creating maple tree...
Creating maple tree...
Drawing Poplar
Creating maple tree...
The number of objects in the element is:
2
It can be seen that although we have created item1, item2, item3, and item4, item1, item2, and item4 are all maple trees, they share the same object in the metadata mode, so there are item1 = item2 = item4, so the total number of objects in the system is only two, respectively, maple tree and poplar.

The above is a simple application of the metadata sharing model. The metadata mode increases the complexity of the program, but increases the efficiency of the Program (in the above example, although a MapItem object is created, it is quickly reclaimed by garbage collection ). The applications of the enjoy meta-mode include: String type of Java (such as String a = "123"; String B = "123"; finally, a = B, it indicates that the String type of Java is in the meta mode) and the Character Processing Method in the Text Processing System (this is obvious. Assume that an article contains 0.1 million characters, it is impossible to create 0.1 million objects, and it needs to use the enjoy mode to reduce expenses ). The shared part and the non-shared part are usually used as the parent node of the shared part.

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.