博客好久没有更新,自己也好久没有写代码了。
惭愧!最近诸事不顺,算了,不说废话了,呵呵!
博客design principle:模拟 android Button 控件点击事件主要说了一下模拟 android 的 Listener 模式,其实这就是一种委派与回调机制的体现。
委派,也可以叫做委托,从字面上来理解的话,应该是委托其他类做事情而自己不做或者只做一部分工作;而回调,就是调用自己的方法。
在 java 中,这两种机制很类似,你姑且可以认为它们就是一码事。
推荐一篇博文给大家:Java中委托事件模型
java 中,实现委派和回调都是通过接口来实现的。下面举个小例子吧!
该例子是这样的(只是一个例子):
ProfessionalWorker 、SparetimeWorker 负责发射 Rocket,Rocket 类通过接口 IRocketDelegate 委派(或者说是回调)ProfessionalWorker 、SparetimeWorker自己发射。
总之,Rocket不做具体的事情。看实例代码:
IRocketDelegate.java源码
共有三个方法,分别是用于计算 Rocket 发射时间、计算 Rocket 发射完毕的时间以及发送是否失败的。
Rocket.java源码
在这个类中,声明一个 IRocketDelegate 接口对象,使用该对象调用接口的方法。我们知道,接口不可以直接实例化,换句话说,实例化接口必须实现接口的所有方法。
那么,我们就把这些实现工作交给具体的发射者来完成。实现回调。
ProfessionalWorker.java源码
SparetimeWorker.java源码
这个类采用内部类的方式完成。
Test.java
显示结果
这就体现 Java 中的一句话Don't call me,I'll call you. 其含义就是回调(或者委派/委托)。
android 中很多这样的用法,关于接口的好处还有很多,后续慢慢研究,记住一句话:面向对象面向接口编程。
分享到:
相关推荐
作者地址:https://github.com/AlfredTheBest/Design-Pattern * Lesson 0[《设计模式简介》](https://github.com/AlfredTheBest/Design-Pattern/tree/master/lesson0) * Lesson 1 [简单工厂模式]...
Material Design风格的笔记app,该项目需要依赖RoboGuice,floating-action-button,appcompat 两个项目,因此编译起来比较困难,不太适合初学者。项目地址:https://github.com/dan-zx/material-notes 效果图:使用...
TCP/IP Architecture, Design and Implementation in Linux 1/5 For All: [Part 1/5] http://download.csdn.net/source/1345609 [Part 2/5] http://download.csdn.net/source/1345649 [Part 3/5] ...
Concept of design for Color Admin is based on the FLAT design and finally it comes out with a clean and neat design. It is built on top of the popular Bootstrap Framework. Besides that, it is bundled...
TCP/IP Architecture, Design and Implementation in Linux 5/5 For All: [Part 1/5] http://download.csdn.net/source/1345609 [Part 2/5] http://download.csdn.net/source/1345649 [Part 3/5] ...
基于JAVA实现的快速开发平台。模块化插件式开发,前后端分离,开箱即用。后台基于SpringBoot框架,数据库访问同时集成了Jpa与MyBatis-Plus,即可自动生成数据库表结构,又可灵活编写sql。前端基于vue-next-admin/...
TCP/IP Architecture, Design and Implementation in Linux 4/5 For All: [Part 1/5] http://download.csdn.net/source/1345609 [Part 2/5] http://download.csdn.net/source/1345649 [Part 3/5] ...
介绍一种使用现代方法的嵌入式系统设计,该方法需要软件和硬件的统一视图。
TCP/IP Architecture, Design and Implementation in Linux 2/5 For All: [Part 1/5] http://download.csdn.net/source/1345609 [Part 2/5] http://download.csdn.net/source/1345649 [Part 3/5] ...
React+TS+Redux Tool Kit+Ant Design+ESLint+Git Lint + Stylelint 项目构建企业级模板,具体参考https://gitee.com/zhang_huai_wei/zhw-blog-pc || React+TS+Redux Tool Kit+Ant Design+ESLint+Git Lint + ...
***有关如何使用生成的页面对象文件的更多信息:Java:https://code.google.com/p/selenium/wiki/ PageFactory#C#:http://relevantcodes.com/pageobjects-and-pagefactory-design-patterns-in-selenium...
TCP/IP Architecture, Design and Implementation in Linux 3/5 For All: [Part 1/5] http://download.csdn.net/source/1345609 [Part 2/5] http://download.csdn.net/source/1345649 [Part 3/5] ...
Operating Systems, Internals and Design Principle, 7th 仅供参考,如有侵权请联系删除
This is a native Android Application written purely in Java. Wiki https://github.com/openMF/android-client/wiki Product Roadmap https://mifosforge.jira.com/wiki/display/MIFOSX/Usability and Design - ...
使用customRow 设置行属性,写对应事件 :customRow=”rowClick” 然后在data里面写 rowClick: record => ({ // 事件 on: { click: () => { ... console.log(record, 'record') ...补充知识:Ant-Design-Vue table ...
Material Design 完整应用模板和空模板; ASP.NET MVC5/6 + AngularJS 完整应用模板和空模板; ASP.NET MVC5/6 + HTML5/jQuery 完整应用模板和空模板; Ruby on Rails HTML5/jQuery 完整应用模板和空模板; MeteorJS...
请改用 Ant Design Pro。Ant Design Pro 是管理界面的生产就绪解决方案。该项目基于 Ant Design 开发的设计原则,引入了更高级别的组件;我们开发了模板、组件和相应的设计工具包,以改善管理界面的用户和开发体验。...
详细介绍可以见博客以及https://github.com/ki-ljl/Linux-Curriculum-Design,博客借鉴了https://github.com/ki-ljl/Linux-Curriculum-Design,其中实现了匿名管道、命名管道以及消息队列
两个版本的参考文献都有对这种机制的详细解释。 第4版所做的修改 在开始编写新版之前,我们对使用本书第3版的老师做了一个调查。我们根据调查结果来确定新内容和要做的修改。为此,编写了全新的三章,并在书中插入...
Material Design 完整应用模板和空模板; ASP.NET MVC5/6 + AngularJS 完整应用模板和空模板; ASP.NET MVC5/6 + HTML5/jQuery 完整应用模板和空模板; Ruby on Rails HTML5/jQuery 完整应用模板和空模板; MeteorJS...