Android Design Pattern series (6)-SDK source code-enjoy the metadata Pattern

Source: Internet
Author: User

The metadata mode gives me the feeling that an object pool is a cache singleton object.
In Java, the most classic example of the enjoy meta mode is the string class, and the most easy to understand is the word document character sharing example, which is also a classic application of the enjoy meta mode.
This article describes sqlitecompiledsql for compiling SQL statements in Android. It is also an easy-to-understand example. In fact, the android SDK must use the metadata mode in many places.
Enjoy the meta mode, flyweight pattern, serious points, someProgramIf you do not use the metadata mode, you cannot implement it using the object-oriented method. The object will overwrite your memory: "applications designed with object-oriented thinking often face the problem of too many object instances ".

1. Intention
Use the sharing technology to effectively support a large number of fine-grained objects.
Popular Words:Shared Pool cache internal status external status object Singleton 

2. Structure


This is a complete structure of the metadata mode.
The client obtains the metadata object through the metadata factory. The creation of the metadata object is controlled based on the metadata pool of the factory. If the metadata pool does not have this object, the client creates this object and saves it to the metadata pool, if this object exists in the metadata pool, use this object directly. Because the metadata object is shared at the same time, it indicates that the attributes of reuse are immutable. Otherwise, they are all changed and there is no sharing. These unchanged attributes are called internal states and are independent from external scenarios. Some other attributes can be changed according to external scenarios, which are called external States. We also see that we can change the external State through operation.
The use of sqlitecompiledsql in Android is a typical implementation of many database systems. Starting from the application, through various database operations, we do not know how many queries are performed, and some of these operations have the same SQL statements, these compiled SQL compilation objects are actually the same and can be shared and shared, but they are actually cache objects. Sqlitecompiledsql is such a metadata object to be shared. The related UML diagram is shown as follows:


Among them, mcompiledquerie in sqlitedatabase is the container that stores the metadata objects.
This method greatly reduces the creation of SQL compilation objects and improves the performance of database operations.

3.Code
Sqlitecompiledsql, a metadata object, is mainly an internal state SQL statement:

 
Class sqlitecompiledsql {private string msqlstmt = NULL; native_compile (SQL); native_finalize ();}

Enjoy the Yuan factory class:

Public class sqlitedatabase {Map <string, sqlitecompiledsql> mcompiledqueries = maps. newhashmap (); sqlitecompiledsql Merge (string SQL) {export compiledstatement = NULL; Boolean cachehit; synchronized (mcompiledqueries) {If (mmaxsqlcachesize = 0) {return NULL ;} cachehit = (compiledstatement = mcompiledqueries. get (SQL ))! = NULL;} If (cachehit) {mnumcachehits ++;} else {mnumcachemisses ++;} return compiledstatement;} private void Merge () {synchronized (mcompiledqueries) {for (sqlitecompiledsql compiledsql: mcompiledqueries. values () {compiledsql. releasesqlstatement ();} mcompiledqueries. clear () ;}} void addtocompiledqueries (string SQL, sqlitecompiledsql compiledstatement) {// omitting the specific code }}

Several other related classes are related to the operation of this set. There is no substantive relationship with the metadata mode, and the code is omitted.

4. Results
(1). structural mode;
(2). Storage Saving Method: Use sharing to reduce the consumption of internal states, and use the computing time in exchange for storage of external States;
(3). buffer.

Related Article

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.