"Onlookers" design pattern (17)--structure-type of the Flyweight pattern

Source: Internet
Author: User

Enjoy meta mode (English: Flyweight pattern) is a software design pattern. It uses shared objects to minimize memory usage and to share information to as many similar objects as possible, and it is suitable for use in large amounts of memory when a large number of objects are simply duplicated and thus result in unacceptable usage. Usually part of the state in an object can be shared. It is common practice to put them in an external data structure and pass them on to the element when it is needed. ----WIKIPEDIA


Personal Understanding

Sharing, memory consumption should consider the sharing of objects, shared objects can reduce the number of objects generated, which can reduce the consumption of memory, when an object and other objects exist in common and the content is consistent, you can extract the common parts to share, so that all of the resulting objects consume memory of the sum will be reduced, This reflects the importance of sharing.


Pattern Definition

The shared mode is to support the reuse of a large number of fine-grained objects, so the enjoy meta-pattern requires that the objects that can be shared must be fine-grained objects. The so-called enjoy meta-mode is to run the sharing technology effectively support the reuse of a large number of fine-grained objects. The system uses a small number of objects, and these are more similar, the state changes small, you can achieve multiple reuse of objects.

Requires fine-grained objects, so inevitably the number of objects and the nature of similar, then we can divide these object information into two parts, internal state and external state.

Internal state: The shared part that is not changed with the external environment within the enjoyment meta object.

External state: As the environment changes, the state that cannot be shared is the external state.


Case analysis


Take a look at this example, the exam registration system, a few basic information, including the ID of the applicant and the location of the test and the exam subjects, and so on, if you do not use the sharing model, then each registration of the staff to come to the registration, will create a test information related to the object, So this object should be the same as the test taker, so if each candidate to create an object, add 1000 candidates, each object 28 bytes, then will produce 28*1000 memory consumption, so that the consumption is not very large, but you have to understand the actual build environment, An object property is more than the above 28 bytes (just assume) large, then may be a lot of M space is occupied, 1000 candidates, each examination room 50 people, then only 20 examination rooms can be, that is, if the object sharing, only need 28b*20, from here you can see how big the gap.


Structure diagram of the class


The sharing of information in the same examination room allows the object to consume significantly less memory.


public class Examroom {private string Roomid;private string Location;private string subject;//the identity of the class private string key;}
public class Signupfactory {private static hashmap<string, examroom> pool = new hashmap<string, examroom> (); /Get the exam room information public static examroom Getexamroominfo (String key) {Examroom room = null;if (!pool.containskey (key)) {room = new Exa Mroom (Key);p Ool.put (key, guest); System.out.println ("New object into Pool" + key);} Else{room = Pool.get (key); System.out.println ("read from pool" + key);} return to the hostel;}}

in this example, the examination room information is extracted, put into the pool of hashmap to share, through the key as a unique identification, to differentiate, here we use the subject and the examination room mixed string concatenation as key to uniquely identify.


Through such a design, it should be said that greatly reduce the production of unnecessary examroom objects, such design, in the actual production environment, for the stability of the whole system is very useful.


Case Analysis 2

If I want to go to school, then generally are to find the bus, I look at the time, eight, good, the door of the community should have 1058 of the car, then such a process down, how to express it? Think of a problem first, when I think of the bus, I need to think of his basic information is right, at least the time should be known, because I know the time I know this time point has this train ah right, next to a simulation.

The main code is as follows:

public class Bus {private string Busnumber;private string Datetime;private string DriverID;}

public class Busfactory {private static hashmap<string, bus> Buspool = new hashmap<string, bus> (); static {Busp Ool.put ("10588:00sh100002", New Bus ("1058", "8:00am", "SH100002")), Buspool.put ("10589:00sh100003", New Bus ("1058", " "SH100003"), Buspool.put ("105810:00sh100002", New Bus ("1058", "10:00am", "SH100002"), Buspool.put (" 105811:00sh100003 ", New Bus (" 1058 "," 11:00am "," SH100003 ")) Buspool.put (" 105812:00sh100002 ", New Bus (" 1058 "," 12:00 "," SH100002 "));} public static Bus Getbus (date date) {bus bus = null; String DriverID = null; String DateTime = null;//The following paragraph to determine the passenger in the acquisition (date) of which the train and its information//assume the 1058 bus, here only simulated, here should be required to query the database if (! Date.after ( New Date (new Date (). GetTime () + 10000)) {DriverID = "SH100002";d atetime = "8:00";} The basic information for merging this train is uniquely identified as String key = "1058" + DateTime + driverid;//If the object pool exists if the IF (!buspool.containskey (key)) {//Actually here can be more simplified, The bus object can also be constructed with key only bus = new Bus ("1058", DateTime, DriverID); Buspool.put (key, bus); System.out.println ("New Train");} If there is no else{system.out.println ("fromAccess "); bus = Buspool.get (key);} return bus;}} <span style= "FONT-SIZE:18PX;" ><strong><span style= "FONT-SIZE:12PX;" ></span></strong></span>

benefits of the enjoy meta model

1 It can greatly reduce the number of objects in the system.

2 uses the external state, the external state is relatively independent, and does not affect the internal state, so the enjoy meta-mode enables the sharing of meta-objects to be shared in different environments.

The disadvantage of the enjoy meta mode

The 1-Share model needs to differentiate between the external state and the internal state, making the application somewhat more complex.

2 in order for objects to be shared, the enjoy meta-mode requires the state of the object to be externally instantiated, while the read external state makes the run time longer.



Source Download

Source

Structural Design Patterns related articles

Combined pattern of structural type






"Onlookers" design pattern (17)--structure-type of the Flyweight pattern

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.