[Original-tutorial-serialization] "Android-based big talk Design Model"-Chapter 9 of the structural model of the design model: text messages in the enjoy mode can be sent in this way

Source: Internet
Author: User
<Big talk design model> description and copyright notice of this tutorial

Guoshi studio is a technical team dedicated to enterprise-level application development on the Android platform. It is committed to the best Android Application in China.ProgramDevelopment institutions provide the best Android enterprise application development training services.

Official contact information for Enterprise Training and Development Cooperation:

Tel: 18610086859

Email: hiheartfirst@gmail.com

QQ: 1740415547

Guoshi studio is better for you!

L this document references and uses free images and content on the Internet, and is released in a free and open manner, hoping to contribute to the mobile Internet and the smart phone age! This document can be reproduced at will, but cannot be used for profit.

L if you have any questions or suggestions about this document, go to the official blog

Http://www.cnblogs.com/guoshiandroid/ (with the following contact information), we will carefully refer to your suggestions and modify this document as needed to benefit more developers!

L The latest and complete content of "big talk design mode" will be regularly updated on the official blog of guoshi studio. Please visit the blog of guoshi studio.

Http://www.cnblogs.com/guoshiandroid/get more updates.

 

Meta Mode Text messages can be sent in this way 

Meta ModeUse Cases: 

Gg sends at least one text message to mm every day, And before going to bed every day, there will be one text message, which is often a trivial matter and a bit of emotional talk. In the first month, GG was still happy with this. Over time, he also felt bored when talking about it many times, what is even more intolerable is the repeated input of these emotional words each time. Gg told his friend K about this problem, K said, "You stupid, why don't you store some of your frequently used words on your mobile phone, you can use it directly when you want to use it. "When you hear it, silly Gg immediately feels that he has been overwhelmed, so he immediately saves it in his cell phone." Baby, good night "," you are my angel "," Baby, I love you forever "and other words.

Explanation of the metadata mode: 

The flyweight pattern mode supports a large number of fine-grained objects efficiently through sharing. Its purpose is to save space and resources occupied, so as to improve the system performance.

Enjoy yuan is flyweight, a professional term in sports. It is the most lightweight in boxing, wrestling, and weightlifting competitions. Porting this word to a software project is also used to represent a very small object, that is, a fine-grained object. As to why we translate flyweight into a "shared object", we can understand it as a shared object, that is, a shared fine-grained object.

Use sharing to support large numbers of fine-grained objects
Efficiently.

Metadata-basedUMLFigure: 

The metadata mode involves the following roles:

Abstract flyweight role: it is a superclass of all the specific Metadata classes. Specify the public interfaces to be implemented for these classes. operations that require the external State can be passed in through the parameters of the method. Abstract metadata-sharing interfaces make it possible to enjoy the metadata, but do not force sub-classes to share. Therefore, not all metadata-sharing objects can be shared.

Concreteflyweight: the object type provides the interface defined in the abstract object type. If an internal state exists, it must provide storage space for the internal state. The inherent state of the object must be unrelated to the environment of the object, so that the object can be shared in the system. Sometimes, a specific metadata class is called a simple metadata class, because a compound metadata class is composed of a specific metadata role.

Unsharableflyweight: A Shared Meta class, also called a composite Meta class. A composite object is composed of multiple single-object objects that can be shared, but the composite object cannot be shared.

Flyweightfactory: the metadata factory class is used to create and manage metadata objects. When a client object requests a metadata object, the metadata factory needs to check whether there is a qualified metadata object in the system. If yes, the meta-factory role should provide the existing meta-object. If the system does not have an appropriate meta-object, the meta-factory role should create a new appropriate meta-object.

Customer class (client): the customer class needs to store the external status of all the object.

The UML diagram of the metadata mode is as follows:

In-depth analysis of the enjoy Mode:

It shares data with other similar objects to reduce memory usage.

The first type of state of the object is called the internal state ). It will not change as the environment changes and is stored inside the object, so the internal state can be shared. For any object, its value is identical.

The second type of state of the object to be shared is the external State (external
State ). It changes as the environment changes, so it cannot be shared. For different meta objects, its values may be different. The external status of the object must be saved by the client. After the object is created, it is uploaded to the object. Therefore, the external and internal statuses of the shared element are independent of each other and are not associated with each other.

Scenario Analysis andCodeImplementation: 

In the above application scenarios, text messages stored on Gg mobile phones are specific object of interest.

The UML Model diagram is as follows:

 

Create an abstract meta-role:

PackageCom. diermeng. designpattern. flyweight;

/*

* Abstract meta-Role

*/

Public InterfaceBasesweetword {

// Display Method

Public VoidDisplay ();

}

 

Create a role:

PackageCom. diermeng. designpattern. flyweight. impl;

ImportCom. diermeng. designpattern. flyweight. basesweetword;

 

/*

* Specific metadata

*/

Public ClassMysweetwordImplementsBasesweetword {

// Specific metadata attributes

PrivateString mychar;

 

PublicMysweetword (){}

// Constructor with the instantiation property Function

PublicMysweetword (string mychar ){

This. Mychar = mychar;

}

 

// Specific implementation of real-world functions

Public VoidDisplay (){

System.Out. Println (mychar );

}

}

 

Create a metadata factory class:

PackageCom. diermeng. designpattern. flyweight. impl;

 

ImportJava. util. hashmap;

ImportJava. util. Map;

 

ImportCom. diermeng. designpattern. flyweight. basesweetword;

 

/*

* Metadata Factory

*/

Public ClassMysweetwordfactory {

// A collection of metadata objects

Private
Map <string, basesweetword> pool;

 

// Constructor instantiates a set of object objects.

PublicMysweetwordfactory (){

Pool =NewHashmap <string, basesweetword> ();

}

 

// Obtain the metadata object. If the object does not exist in the collection, create the object.

PublicBasesweetword
Getmycharacter (string character ){

Basesweetword mychar = pool. Get (character );

If(Mychar =Null){

Mychar =New
Mysweetword (character );

Pool. Put (character, mychar );

}

ReturnMychar;

}

}

Finally, we create a test client:

PackageCom. diermeng. designpattern. flyweight. client;

 

ImportCom. diermeng. designpattern. flyweight. basesweetword;

Import
Com. diermeng. designpattern. flyweight. impl. mysweetwordfactory;

 

/*

* Test the client in the enjoy mode.

*/

Public ClassFlyweighttest {

 

Public Static VoidMain (string [] ARGs ){

// Create a metadata Factory

Mysweetwordfactory factory =New
Mysweetwordfactory ();

 

// Retrieve the corresponding mycharacter from the specific metadata Factory

Basesweetword mychar1 =
Factory. getmycharacter ("baby, good night ");

Basesweetword mychar2 =
Factory. getmycharacter ("you are my angel ");

Basesweetword mychar3 = factory. getmycharacter ("baby, good night ");

Basesweetword mychar4 =
Factory. getmycharacter ("Baby, I love you forever ");

 

Mychar1.display ();

Mychar2.display ();

Mychar3.display ();

Mychar4.display ();

 

If(Mychar1 = mychar3 ){

System.Out. Println ("Haha, congratulations! Let's just copy the same sentence! ");

}Else{

System.Out. Println ("oh, we are not the same sentence! ");

}

}

 

}

The output result is as follows:

Good night, baby

You are my angel

Good night, baby

Baby, I love you forever

Haha, congratulations! Let's just copy the same sentence!

 

Advantages and disadvantages of the shared element model: 

Advantages:

The metadata mode can reduce the number of objects in the memory, thus saving the system a lot of memory space.

Disadvantages:

The metadata mode makes the system more complex, because some States need to be externalized to make objects shared, which complicate the logic of the program. In addition, because the Yuan-sharing factory needs to maintain all the Yuan-sharing objects, if there are many Yuan-sharing objects to be maintained, it will take a lot of time to find specific Yuan-sharing objects. In other words, the metadata mode is a time-for-space mode.

Introduction to the practical application of the enjoy mode: 

The metadata mode is not commonly used in general project development. It is often used in the development of the underlying system to solve system performance problems.

The string type in Java uses the meta mode.

If a String object string1 has been created in Java, when the same string string2 is created next time, the system just points the reference of string2 to the specific object referenced by string1, this achieves the sharing of the same string in the memory. If you create a new String object each time you execute the string1 = "ABC" operation, the memory overhead will be high.

If you are interested, you can use the following program for testing to see if the references of string1 and string2 are consistent:

String string1 = "love you for 10 thousand years, love your heart will never change ";

String string2 = "love you for 10 thousand years, love your heart will never change ";

// "=" Is used to determine whether two objects are the same. Equals checks whether the string value is equal.

If (string1 = string2 ){

System. Out. println ("the same as the two ");

} Else {

System. Out. println ("the two are different ");

}

After the program runs, the output result is "the same as the two". This shows that the string class adopts the metadata mode. If the content of string1 changes, for example, execute string1 + = "let's get married! The S1 and S2 references are no longer consistent.

Let's talk about how to process strings in PHP. As a weak type language, PHP's string type is a basic type, not an object. In addition, its execution method is significantly different from that of Java. When a script file is executed, all required resources are loaded. After the execution, all the resources occupied are released immediately, therefore, it basically does not produce similar performance problems. Its character string processing design naturally does not use the metadata mode.

Tip: 

 

Although object-oriented solves the abstract problem, we also need to consider the cost of Object-oriented for a software system running in practice, the metadata mode solves the problem of object-oriented cost. The object sharing mode reduces the number of objects in the system, thus reducing the memory pressure on the system caused by fine-grained objects.

In terms of specific implementation, we should pay attention to the processing of object states. We must correctly distinguish the internal and external States of objects, which is the key to realizing the metadata mode.

The advantage of the Meta mode is that it greatly reduces the number of objects in the memory. To achieve this, the Yuan-sharing model has also paid a certain price:

1. In order to share objects, the metadata mode needs to externalize some States, which makes the system logic complex.

2. In the metadata mode, partial states of the object are externalized, and reading the external States leads to a longer running time.

In addition, there is another interesting question: what kind of conditions must the system meet to use the metadata-sharing model. The following points are summarized:

1. A system contains a large number of fine-grained objects;

2. These fine-grained objects consume a lot of memory.

3. Most of the statuses of these fine-grained objects can be externalized;

4. These fine-grained objects can be divided into many groups according to the inherent state. When the external object is removed from the object, each group can be replaced by only one object.

5. The software system does not depend on the identities of these objects. In other words, these objects can be undistinguished.

The system that meets the preceding conditions can use a shared object. Finally, you need to maintain a hash table that records all the existing metadata in the system. It is also called an object pool, which consumes a certain amount of resources. Therefore, the metadata mode should be used only when there are enough metadata instances available for sharing.

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.