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