Android Design Pattern series (5)-Memorandum pattern of SDK source code

Source: Internet
Author: User

Memorandum mode, at workCodeIn, or frequently used.
For example,ProgramMembers like to write code, coding, and coding. At this time, the status is very high, but they always need to go to the toilet at intervals. The status is to relax and relax, after the test is returned, it is restored to the High State and coding continues. This process for the boss behind him, by default, agrees that you leave to go to the toilet. He also hopes that you will resume working after you come back, but when you go to the toilet, he doesn't need to know the details, and as a client, you don't want him to know the details of your toilet. You just need to resume your work after you come back, the boss should not pick your thorn.
This is the memorandum mode.
This article analyzes a save () and restore () Operation of canvas today. However, after reading this article, if you do not understand the memorandum mode, you still do not understand it, however, canvas is a major event of Android. It is definitely helpful for you to learn more about Android.
Learning the memorandum mode, through the internal implementation of saving the status and restoring the status, is helpful for understanding some seemingly useless operations that are actually very important.

1. Intention
Capture the internal state of an object without compromising encapsulation, and save the state outside the object. In this way, the object can be restored to the previously saved state.
Popular Words:Mark the State memorandum master

2. Structure and code

The organizer saves the state (all or part of the state, usually the value of the variable) of the primary machine by using the createmento () method. After running, wait for the appropriate time, you can use the setmemento () method to restore to the previous state again. In this process, we have not made any access and settings for these statuses. In fact, these statuses are private and access is prohibited to the outside world, we achieved this through the two simplest methods of the memento object. Memento is often written as the internal class of originator.
In Android, canvas has two methods: Save () and restore (), which are used for graphic transformation. This is because cross-language conversion is involved, I believe that this is a memorandum of understanding, but this idea is definitely a memorandum of understanding.


Let's read the comments of its source code. First, check the SAVE () Save status:

 
Public class canvas {/*** saves the current matrix and clip onto a private stack. subsequent * callto translate, scale, rotate, skew, Concat or clip rect, clippath * Will all operate as usual, but when the balancing call to restore () * is made, those callwill be forgotten, and the settings that existed * Before the SAVE () will be reinstated. * // *** Save the current matrix and crop it to a private stack. In fact, the matrix and clipping are the state of the current canvas */Public native int save ();}

Restore ():

Public class canvas {/*** this call balances a previous call to save (), and is used to remove all * modifications to the matrix/clip State since the last save call. it is * an error to call restore () More times than save () was called. * // *** remove the modification made since the last save operation and restore it to the previous state. Because it is a stack implementation, the pull operation cannot be equal to the push operation, save () and restore () should be used in pairs, otherwise the recovery status may be wrong */Public native void restore ();}

From the above two methods, they achieve self-state recovery. In fact, we only execute two methods that do not have access to any internal information, in fact, these two methods are the internal status information that we cannot see during operations.

3. Results
(1) Keep the encapsulation boundary and hide the internal information of the complex primary object from other external objects.
(2) The simplified primary machine virtually converts state operations to the customer's hand, simplifying some implementations of the primary machine.
(3). Pay attention to the management cost of the memorandum.

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.