Prototype mode of Android design mode

Source: Internet
Author: User

Summary: In the system to create a large number of objects, these objects have almost identical functionality, but only a little bit different in detail

Specify the kind of objects created with the prototype instance and create new objects by copying the prototypes

Example 1:

We need a bitmap of several different formats: argb_8888, rgb_565, argb_4444, alapha_8, etc. So we can first create a argb_8888 bitmap as a prototype, based on it, by calling Bitmap.copy (Config) to create several other formats bitmap

  /*** Tries to make a new bitmap based on the dimensions of this bitmap, * Setting the new Bitmap's config to the One specified, and then copying * This bitmap's pixels into the new bitmap.  If the conversion is not * supported, or the allocator fails and then this returns NULL.     The returned * Bitmap initially has the same density as the original. *     * @paramconfig the desired config for the resulting bitmap *@paramismutable True If the resulting bitmap should be mutable (i.e. * Its pixels can modified) *@returnthe new bitmap, or null if the copy could not be made. */     PublicBitmap copy (config config,Booleanismutable) {checkrecycled ("Can ' t copy a recycled bitmap"); Bitmap b=nativecopy (Mnativebitmap, Config.nativeint, ismutable); if(b! =NULL) {b.setpremultiplied (mrequestpremultiplied); B.mdensity=mdensity; }        returnb; }

Another example of this is the method that all objects in Java have a name called clone.

   /*** Copy constructor. */     PublicIntent (Intent o) { This. maction =o.maction;  This. Mdata =O.mdata;  This. Mtype =O.mtype;  This. Mpackage =O.mpackage;  This. mcomponent =o.mcomponent;  This. Mflags =O.mflags;  This. Mcontentuserhint =O.mcontentuserhint; if(O.mcategories! =NULL) {             This. mcategories =NewArrayset<string>(o.mcategories); }        if(O.mextras! =NULL) {             This. Mextras =NewBundle (O.mextras); }        if(O.msourcebounds! =NULL) {             This. Msourcebounds =NewRect (o.msourcebounds); }        if(O.mselector! =NULL) {             This. Mselector =NewIntent (O.mselector); }        if(O.mclipdata! =NULL) {             This. Mclipdata =NewClipdata (O.mclipdata); }} @Override PublicObject Clone () {return NewIntent ( This); }

Prototype mode of Android design mode

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.