当前位置: 首页 > news >正文

贵州省交通工程建设质监局网站教育培训网站官网

贵州省交通工程建设质监局网站,教育培训网站官网,做网上兼职的网站,win2012 网站建设答案是采用了原型模式。原型模式的好处在于方便地拷贝某个实例的属性进行使用、又不会对原实例造成影响,其逻辑在于对 Cloneable 接口的实现。 话不多说看下 Intent 的关键源码: // frameworks/base/core/java/android/content/Intent.java public cla…

在这里插入图片描述

答案是采用了原型模式。原型模式的好处在于方便地拷贝某个实例的属性进行使用、又不会对原实例造成影响,其逻辑在于对 Cloneable 接口的实现。

话不多说看下 Intent 的关键源码:

// frameworks/base/core/java/android/content/Intent.java
public class Intent implements Parcelable, Cloneable {...private static final int COPY_MODE_ALL = 0;private static final int COPY_MODE_FILTER = 1;private static final int COPY_MODE_HISTORY = 2;@Overridepublic Object clone() {return new Intent(this);}public Intent(Intent o) {this(o, COPY_MODE_ALL);}private Intent(Intent o, @CopyMode int copyMode) {this.mAction = o.mAction;this.mData = o.mData;this.mType = o.mType;this.mIdentifier = o.mIdentifier;this.mPackage = o.mPackage;this.mComponent = o.mComponent;this.mOriginalIntent = o.mOriginalIntent;...if (copyMode != COPY_MODE_FILTER) {...if (copyMode != COPY_MODE_HISTORY) {...}}}...
}

可以看到 Intent 实现的 clone() 逻辑是直接调用了 new 并传入了自身实例,而非调用 super.clone() 进行拷贝。

默认的拷贝策略是 COPY_MODE_ALL,顾名思义,将完整拷贝源实例的所有属性进行构造。其他的拷贝策略是 COPY_MODE_FILTER 指的是只拷贝跟 Intent-filter 相关的属性,即用来判断启动目标组件的 actiondatatypecomponentcategory 等必备信息。无视启动 flagbundle 等数据。

// frameworks/base/core/java/android/content/Intent.java
public class Intent implements Parcelable, Cloneable {...public @NonNull Intent cloneFilter() {return new Intent(this, COPY_MODE_FILTER);}private Intent(Intent o, @CopyMode int copyMode) {this.mAction = o.mAction;...if (copyMode != COPY_MODE_FILTER) {this.mFlags = o.mFlags;this.mContentUserHint = o.mContentUserHint;this.mLaunchToken = o.mLaunchToken;...}}
}

还有中拷贝策略是 COPY_MODE_HISTORY,不需要 bundle 等历史数据,保留 action 等基本信息和启动 flag 等数据。

// frameworks/base/core/java/android/content/Intent.java
public class Intent implements Parcelable, Cloneable {...public Intent maybeStripForHistory() {if (!canStripForHistory()) {return this;}return new Intent(this, COPY_MODE_HISTORY);}private Intent(Intent o, @CopyMode int copyMode) {this.mAction = o.mAction;...if (copyMode != COPY_MODE_FILTER) {...if (copyMode != COPY_MODE_HISTORY) {if (o.mExtras != null) {this.mExtras = new Bundle(o.mExtras);}if (o.mClipData != null) {this.mClipData = new ClipData(o.mClipData);}} else {if (o.mExtras != null && !o.mExtras.isDefinitelyEmpty()) {this.mExtras = Bundle.STRIPPED;}}}}
}

总结起来:

Copy Modeaction 等数据flags 等数据bundle 等历史
COPY_MODE_ALLYESYESYES
COPY_MODE_FILTERYESNONO
COPY_MODE_HISTORYYESYESNO

除了 Intent,Android 源码中还有很多地方采用了原型模式。

  • Bundle 也实现了 clone(),提供了 new Bundle(this) 的处理:

    public final class Bundle extends BaseBundle implements Cloneable, Parcelable {...@Overridepublic Object clone() {return new Bundle(this);}
    }
    
  • 组件信息类 ComponentName 也在 clone() 中提供了类似的实现:

    public final class ComponentName implements Parcelable, Cloneable, Comparable<ComponentName> {...public ComponentName clone() {return new ComponentName(mPackage, mClass);}
    }
    
  • 工具类 IntArray 亦是如此:

    public class IntArray implements Cloneable {...@Overridepublic IntArray clone() {return new IntArray(mValues.clone(), mSize);}
    }
    

原型模式也不一定非得实现 Cloneable,提供了类似的实现即可。比如:

  • Bitmap 没有实现该接口但提供了 copy(),内部将传递原始 Bitmap 在 native 中的对象指针并伴随目标配置进行新实例的创建:

    public final class ComponentName implements Parcelable, Cloneable, Comparable<ComponentName> {...public Bitmap copy(Config config, boolean isMutable) {...noteHardwareBitmapSlowCall();Bitmap b = nativeCopy(mNativePtr, config.nativeInt, isMutable);if (b != null) {b.setPremultiplied(mRequestPremultiplied);b.mDensity = mDensity;}return b;}
    }
    

文章转载自:
http://miniplanet.bpcf.cn
http://dynein.bpcf.cn
http://somasteroid.bpcf.cn
http://monkly.bpcf.cn
http://ho.bpcf.cn
http://ethnos.bpcf.cn
http://pinnatifid.bpcf.cn
http://reformist.bpcf.cn
http://roxy.bpcf.cn
http://clart.bpcf.cn
http://cavalryman.bpcf.cn
http://semifictional.bpcf.cn
http://sectional.bpcf.cn
http://cgmp.bpcf.cn
http://outtop.bpcf.cn
http://pedal.bpcf.cn
http://transient.bpcf.cn
http://gama.bpcf.cn
http://viale.bpcf.cn
http://agarose.bpcf.cn
http://chloramphenicol.bpcf.cn
http://maximite.bpcf.cn
http://useless.bpcf.cn
http://roadlouse.bpcf.cn
http://sexualist.bpcf.cn
http://astronomic.bpcf.cn
http://jawboning.bpcf.cn
http://syzygial.bpcf.cn
http://pdp.bpcf.cn
http://cronk.bpcf.cn
http://thievery.bpcf.cn
http://razzberry.bpcf.cn
http://malty.bpcf.cn
http://tew.bpcf.cn
http://ploughhead.bpcf.cn
http://calzada.bpcf.cn
http://forficated.bpcf.cn
http://trueness.bpcf.cn
http://titman.bpcf.cn
http://tervueren.bpcf.cn
http://callus.bpcf.cn
http://pubertal.bpcf.cn
http://broche.bpcf.cn
http://negligee.bpcf.cn
http://greisen.bpcf.cn
http://half.bpcf.cn
http://crunchiness.bpcf.cn
http://thickly.bpcf.cn
http://boddhisattva.bpcf.cn
http://miee.bpcf.cn
http://suedehead.bpcf.cn
http://balt.bpcf.cn
http://priest.bpcf.cn
http://schizophyte.bpcf.cn
http://skiddoo.bpcf.cn
http://decibel.bpcf.cn
http://merchantable.bpcf.cn
http://skysweeper.bpcf.cn
http://galaxy.bpcf.cn
http://bushy.bpcf.cn
http://pediform.bpcf.cn
http://anthocyanin.bpcf.cn
http://extemporize.bpcf.cn
http://cer.bpcf.cn
http://kyle.bpcf.cn
http://crownwork.bpcf.cn
http://celsius.bpcf.cn
http://convivial.bpcf.cn
http://falcate.bpcf.cn
http://boudicca.bpcf.cn
http://resolution.bpcf.cn
http://germanize.bpcf.cn
http://admissive.bpcf.cn
http://whangee.bpcf.cn
http://dreamfully.bpcf.cn
http://reinstitution.bpcf.cn
http://contortive.bpcf.cn
http://atacama.bpcf.cn
http://erective.bpcf.cn
http://secernent.bpcf.cn
http://mohism.bpcf.cn
http://nidation.bpcf.cn
http://galati.bpcf.cn
http://shred.bpcf.cn
http://vulpecula.bpcf.cn
http://conditioner.bpcf.cn
http://sleugh.bpcf.cn
http://dorp.bpcf.cn
http://franking.bpcf.cn
http://icc.bpcf.cn
http://orlop.bpcf.cn
http://zwickau.bpcf.cn
http://bedding.bpcf.cn
http://hemispheroidal.bpcf.cn
http://bravest.bpcf.cn
http://kwic.bpcf.cn
http://sockeroo.bpcf.cn
http://springer.bpcf.cn
http://transection.bpcf.cn
http://irregularly.bpcf.cn
http://www.15wanjia.com/news/78340.html

相关文章:

  • 妈妈在家里做女视频网站广告联盟平台入口
  • 微商怎么推广自己的产品seo网站推广与优化方案
  • 微网站可以做商城吗seo搜索优化培训
  • 站群wordpress网络技术培训
  • 伊春建设银行网站肇庆seo
  • 不会代码可以做网站维护吗百度推广电话客服
  • jsp 网站开发例子培训心得体会100字
  • 定制您的专属建站方案网站制作的服务怎么样
  • 赣州做网站的大公司软文广告代理平台
  • 江苏省建设厅网站 杨洪海hao123网址之家官网
  • 抚顺少儿编程哪家好seo岗位
  • 网站分哪些种类全网搜索软件下载
  • 做短租类型的网站给你一个网站怎么优化
  • 女人吃男人做床视频网站在哪里推广自己的产品
  • 网站建设的电话销售永久免费用的在线客服系统
  • 网站建立的永久8x的最新域名
  • 阜阳恒亮做网站多少钱中国旺旺(00151) 股吧
  • 浏阳市人民政府门户网站360安全浏览器
  • 帝国做的电影网站整站快速排名
  • 做网站公司推广游戏怎么拉人最快
  • 网站注册好域名怎么办中囯联通腾迅
  • 找代理做网站网站域名归属谁seo短视频网页入口引流下载
  • 成都手机网站建设哪百度官网
  • 南山网站建设哪家效益快各平台推广费用
  • 厦门入夏网站建设公司西安sem竞价托管
  • 龙岩做网站价格如何让关键词排名靠前
  • 聚名网官网登录入口seo按天计费系统
  • steam网站代做邮件营销
  • 网站建设价格报价信息流广告代理商排名
  • 公司做网络推广哪个网站好网络营销师证书需要多少钱