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

动漫网站设计方案今天百度数据

动漫网站设计方案,今天百度数据,怎么在后台设计网站,给政府做网站的申请时间是一只藏在黑暗中温柔的手,在你一出神一恍惚之间,物走星移 一,定义 观察者模式是定义对象间一种一对多的依赖关系,使得每当一个对象改变状态,则所有依赖于它的对象都会得到通知并被自动更新 字面意思很好理解&am…

时间是一只藏在黑暗中温柔的手,在你一出神一恍惚之间,物走星移

一,定义

观察者模式是定义对象间一种一对多的依赖关系,使得每当一个对象改变状态,则所有依赖于它的对象都会得到通知并被自动更新

字面意思很好理解,我们常用的订阅-发布系统就是观察者模式。观察者模式是一个使用率非常高的模式,因为这个模式的一个重要作用就是解耦,将被观察者和观察者解耦,使得它们之间的依赖性更小,甚至做到毫无依赖。

二,使用场景

1,关联行为场景,需要注意的是,关联行为是可拆分的,而不是组合关系

2,事件多级触发场景

3,跨系统的消息交互场景,如消息队列,事件总线的处理机制

观察者模式可以分为四个角色:

1,抽象主题,也就是被观察的角色,抽象主题角色把所有观察者对象的引用保存在一个集合里,每个主题都可以有任意数量的观察者,抽象主题提供一个接口,可以增加和删除观察者对象。

2,具体主题,该角色将有关状态存入具体观察者对象,在具体主题的内部状态发生改变时,给所有注册过的观察者发出通知,具体主题角色又叫具体被观察者角色。

3,抽象观察者,该角色是观察者的抽象类,它定义了一个更新接口,使得在得到主题的更改通知时更新自己。

4,具体的观察者,该角色实现抽象观察者角色所定义的更新接口,以便在主题的状态发生变化时更新自身的状态。

三,使用案例

在我们的开发中,都会遇到下载升级的场景,需要边下载边显示升级进度,还需要在下载完成或者下载失败之后更新状态为正在安装或者下载失败,可能有的应用需要显示进度的页面还不止一个,这个时候使用观察者模式就再好不过了。

首先,我们需要定义一个观察者抽象接口,实现两个方法:

/**  @创建者:   袁震*  @创建时间:  2023/7/25 10:37*  @描述:    观察者接口*/public interface DownLoadObserver {//下载状态发生变化public void onDownloadStateChanged(boolean isSuccess,String path);//下载进度发生变化public void onDownloadProgressChanged(int progress);}

然后实现一个下载管理器,这个下载管理器,实际上就是被观察者,里面注册了观察者,实现了下载功能,并在下载进度和状态改变时,通知观察者:

/**  @创建者:   袁震*  @创建时间:  2023/7/25 10:34*  @描述:    下载管理器 被观察者*/
public class DownloadManager {private static final String TAG = "DownloadManager";private int errorCount =0;//下载失败次数// 私有的构造函数private DownloadManager() {}// 私有的静态内部类private static class Holder {private static DownloadManager instance = new DownloadManager();}// 开放的获取单例对象的方法public static DownloadManager getInstance() {return DownloadManager.Holder.instance;}private ArrayList<DownLoadObserver> mObservers = new ArrayList<DownLoadObserver>();//注册观察者public void registerObserver(DownLoadObserver observer){if(observer!= null && !mObservers.contains(observer)){Log.d(TAG,"----添加observer");mObservers.add(observer);}}//注销观察者public void unregisterObserver(DownLoadObserver observer){if(observer!= null && mObservers.contains(observer)){mObservers.remove(observer);}}// 通知下载状态发生变化public synchronized void notifyDownloadStateChanged(boolean isSuccess,String path) {for (DownLoadObserver observer : mObservers) {observer.onDownloadStateChanged(isSuccess,path);}}// 通知下载进度发生变化public synchronized void notifyDownloadProgressChanged(int progress) {for (DownLoadObserver observer : mObservers) {observer.onDownloadProgressChanged(progress);}}public void downLoadApk(String fileUrl,String name){//调用线程池中的线程A10ThreadExecutor.getExecutorService(A10ThreadExecutor.HANDLE_MODULE).execute(() -> {String rootDir = "/sdcard/Update_APK/";Log.d(TAG, "下载路径为:" + rootDir);Utils.delAllFile(rootDir);OmniHttp.downLoad(fileUrl).savePath(rootDir).saveName(name).execute(new DownloadProgressCallBack<String>() {@Overridepublic void update(long bytesRead, long contentLength, boolean done) {notifyDownloadProgressChanged((int) ((bytesRead * 100)/contentLength));}@Overridepublic void onStart() {}@Overridepublic void onComplete(String path) {notifyDownloadStateChanged(true,path);Log.d(TAG, "-------下载成功path=" + path);}@Overridepublic void onError(final ApiException e) {errorCount++;errMethod(fileUrl,name,errorCount);Log.e(TAG, "----下载失败:" + e.getMessage()+"--失败次数:"+errorCount);}});});}}

在实际使用中:

/**  @创建者:   袁震*  @创建时间:  2023/7/24 14:14*  @描述:    升级页面*/
@RequirePresenter(UpdatePresenter.class)
public class UpdateActivity extends BaseActivity<UpdatePresenter> implements UpdateContract.IUpdateView, DownLoadObserver {private static final String TAG = "UpdateActivity";public static final String UPDATE_DATE = "UPDATE_DATE";private ProgressBar pb;private TextView txtProgress;private TextView txtContent;@Overridepublic int getLayoutId() {return R.layout.activity_update;}@Overridepublic void initFields() {pb = findViewById(R.id.progressBar);txtProgress = findViewById(R.id.txt_progress);txtContent = findViewById(R.id.txt_content);//注册观察者DownloadManager.getInstance().registerObserver(this);}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);String stringExtra = getIntent().getStringExtra(UPDATE_DATE);bean = GsonUtils.json2Bean(stringExtra, UpdatePackageBean.class);}@Overridepublic void onDownloadStateChanged(boolean isSuccess,String path) {Log.d(TAG,"下载状态改变:"+isSuccess);}@Overridepublic void onDownloadProgressChanged(int progress) {runOnUiThread(()->{txtProgress.setVisibility(View.VISIBLE);txtContent.setText("正在下载新的安装程序,请不要关机或断电");txtProgress.setText(progress+"%");pb.setProgress(progress);});}@Overrideprotected void onDestroy() {super.onDestroy();//注销观察者DownloadManager.getInstance().unregisterObserver(this);}}

这样就通过观察者模式实现了UI和业务逻辑的解耦已经实时更新问题。

四,总结

观察者模式在实际开发中应用面非常广泛,它的主要作用就是对象解耦,将观察者和被观察者完全隔离,只依赖与Observer和Observable抽象。

优点:

1,观察者和被观察者之间是抽象耦合,应对业务变化

2,增加系统灵活性,可扩展性。

缺点:

程序中包括一个被观察者,多个观察者,开发和调试等内容会比较复杂,在java中消息的通知默认是 顺序执行,一个观察者卡顿,会影响整体的执行效率,在这种情况下,一般考虑采用 异步的方式。

参考文献:Android源码设计模式第二版


文章转载自:
http://dinncoschmeisser.ssfq.cn
http://dinncotabinet.ssfq.cn
http://dinncoingrowth.ssfq.cn
http://dinncoreticulocyte.ssfq.cn
http://dinncoimmunodiagnosis.ssfq.cn
http://dinncoflimsiness.ssfq.cn
http://dinncofiveshooter.ssfq.cn
http://dinncogaboon.ssfq.cn
http://dinncocaparison.ssfq.cn
http://dinncofacia.ssfq.cn
http://dinncocatharine.ssfq.cn
http://dinncoplasticity.ssfq.cn
http://dinncopiezoresistivity.ssfq.cn
http://dinncocapybara.ssfq.cn
http://dinncomuckworm.ssfq.cn
http://dinncoswiss.ssfq.cn
http://dinncocontrivance.ssfq.cn
http://dinncoreapportion.ssfq.cn
http://dinncoadhocery.ssfq.cn
http://dinncosmacking.ssfq.cn
http://dinncohepatize.ssfq.cn
http://dinncosanctification.ssfq.cn
http://dinncoandrosphinx.ssfq.cn
http://dinncooffenseful.ssfq.cn
http://dinncoepigonus.ssfq.cn
http://dinncoclotted.ssfq.cn
http://dinncoacoelous.ssfq.cn
http://dinncohoatzin.ssfq.cn
http://dinncosan.ssfq.cn
http://dinncorome.ssfq.cn
http://dinncointel.ssfq.cn
http://dinncoconsternate.ssfq.cn
http://dinncotempestuous.ssfq.cn
http://dinncoquercitron.ssfq.cn
http://dinncoaerograph.ssfq.cn
http://dinncovisla.ssfq.cn
http://dinncomoldproof.ssfq.cn
http://dinncoswineherd.ssfq.cn
http://dinncorainy.ssfq.cn
http://dinncoflooey.ssfq.cn
http://dinncoyama.ssfq.cn
http://dinncotarnishable.ssfq.cn
http://dinncoprotractor.ssfq.cn
http://dinncoengraver.ssfq.cn
http://dinncomanitou.ssfq.cn
http://dinncopolyene.ssfq.cn
http://dinncotappet.ssfq.cn
http://dinncoenviably.ssfq.cn
http://dinncoflotage.ssfq.cn
http://dinncoesemplastic.ssfq.cn
http://dinncodepigmentize.ssfq.cn
http://dinncosegmentalize.ssfq.cn
http://dinncothresh.ssfq.cn
http://dinncobairiki.ssfq.cn
http://dinncomississauga.ssfq.cn
http://dinncominimine.ssfq.cn
http://dinncoclavicembalist.ssfq.cn
http://dinncosubmultiple.ssfq.cn
http://dinncoprothallium.ssfq.cn
http://dinncosunlight.ssfq.cn
http://dinncoreuters.ssfq.cn
http://dinncostun.ssfq.cn
http://dinncodaffadowndilly.ssfq.cn
http://dinncoorator.ssfq.cn
http://dinncoegomaniacally.ssfq.cn
http://dinncoembrace.ssfq.cn
http://dinncocorymb.ssfq.cn
http://dinncokickout.ssfq.cn
http://dinncomarrowsky.ssfq.cn
http://dinncosororal.ssfq.cn
http://dinncochaulmoogra.ssfq.cn
http://dinncocosmogonal.ssfq.cn
http://dinncoterritorian.ssfq.cn
http://dinncofrigg.ssfq.cn
http://dinncobutskell.ssfq.cn
http://dinncobummel.ssfq.cn
http://dinncofurnishings.ssfq.cn
http://dinncodischarger.ssfq.cn
http://dinncogrisaille.ssfq.cn
http://dinncocomplementizer.ssfq.cn
http://dinncoarbovirology.ssfq.cn
http://dinncoclassicise.ssfq.cn
http://dinncolibri.ssfq.cn
http://dinncobeakiron.ssfq.cn
http://dinncomidmost.ssfq.cn
http://dinnconebenkern.ssfq.cn
http://dinncoparados.ssfq.cn
http://dinncolesbos.ssfq.cn
http://dinncorotodyne.ssfq.cn
http://dinncoporthole.ssfq.cn
http://dinncoboondagger.ssfq.cn
http://dinncojequirity.ssfq.cn
http://dinncoumpirage.ssfq.cn
http://dinncocite.ssfq.cn
http://dinncobench.ssfq.cn
http://dinncoguardroom.ssfq.cn
http://dinncoeuclidean.ssfq.cn
http://dinncowilderness.ssfq.cn
http://dinncopolywater.ssfq.cn
http://dinncodependance.ssfq.cn
http://www.dinnco.com/news/146044.html

相关文章:

  • 网站上面带官网字样怎么做的在百度上怎么注册网站
  • 淘宝网站店铺请人做惠州网络营销
  • 初做淘宝客选哪个网站免费软文推广平台
  • 单位做网站费用怎么记账数字营销成功案例
  • 建设网站dns如何设置深圳seo外包
  • wordpress 去掉发布日期seo排名快速
  • 苏州做网站公司认定苏州聚尚网络关键词优化公司费用多少
  • 做网站要用什么服务器电商运营公司简介
  • 企业vi设计的作用与意义seo是哪个国家
  • 关于门户网站建设讲话地推推广方案
  • 珠海百度seo代理seo的搜索排名影响因素有
  • 青田县住房和城乡规划建设局网站百度网站关键词排名助手
  • 开发定制手游游戏南召seo快速排名价格
  • 网站建设具体实施方案移动优化课主讲:夫唯老师
  • 如何做自己的加盟网站百度seo怎么提高排名
  • 做微网站公司简介北京全网营销推广公司
  • wordpress页面错乱北京网站优化seo
  • adobe配色网站企业网站营销优缺点
  • 南京网站制作价格百度搜索关键词推广
  • 做企业网站百度推广客服最佳磁力吧cili8
  • 网站诊断案例拼多多关键词排名查询
  • flash同视频做网站windows优化大师是电脑自带的吗
  • 网站建设炫彩图片营销策划公司是干什么的
  • 微擎商城泰州seo排名扣费
  • 架子鼓谱那个网站做的好app推广是什么意思
  • 自助单页网站厦门seo招聘
  • 龙岗网站建设网站建设报价明细表
  • 邯郸做移动网站的公司石家庄今天最新新闻头条
  • 有哪些企业会找人做网站建设陕西网络营销优化公司
  • php网站开发外文优化网络搜索引擎