简介:在系统中要创建大量的对象,这些对象之间具有几乎完全相同的功能,只是在细节上有一点儿差别
用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象
example 1:
我们需要一张Bitmap的几种不同格式:ARGB_8888、RGB_565、ARGB_4444、ALAPHA_8等。那我们就可以先创建一个ARGB_8888的Bitmap作为原型,在它的基础上,通过调用Bitmap.copy(Config)来创建出其它几种格式的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, 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 be modified)
*@returnthe new bitmap, or null if the copy could not be made.*/
public Bitmap 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;
}
另外一个例子就是Java中所有对象都有的一个名字叫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 = new ArraySet(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);
}
}
@OverridepublicObject clone() {return new Intent(this);
}
原文:http://www.cnblogs.com/suncan/p/4331859.html