My blog hasn't been updated for a long time, and I haven't written any code for a long time.
Shame! Recent events are not good. Forget it. Don't talk nonsense. Haha!
Blog Design Principle: Simulate the android button control click event. It mainly describes simulating the android listener mode. In fact, this is a manifestation of the delegation and callback mechanism.
A delegate can also be called a delegate. To put it literally, it is to delegate other classes to do things without doing or only doing part of the work. A callback is to call your own method.
In Java, these two mechanisms are very similar. You can think of them as a code.
I recommend a blog post: Delegate event model in Java
In Java, delegates and Callbacks are implemented through interfaces. Here is a small example!
The example is as follows (just an example ):
Extensionalworker and sparetimeworker are responsible for launching the Rocket. The rocket class uses the irocketdelegate API to delegate (or callback) The extensionalworker and sparetimeworker to launch the rocket.
In short, the rocket does not do anything specific. View the instance code:
Irocketdelegate. Java source code
package mark.zhang;public interface IRocketDelegate {public abstract long startAtTime();public abstract long endAtTime();public abstract void sendDidFail();}
There are three methods used to calculate the rocket launch time, the end time of the rocket launch, and whether the sending fails.
Rocket. Java source code
Package mark. zhang; public class rocket {irocketdelegate delegate = NULL; Public rocket (irocketdelegate delegate) {This. delegate = delegate;} private long getrocketstarttime () {long starttime = delegate. startattime (); Return starttime;} private long getrocketendtime () {long endtime = delegate. endattime (); Return endtime;} public Boolean isok () {// timeout 0.1 seconds: If (getrocketendtime ()-getrocketstarttime ()> = 100) {delegate. senddidfail (); Return false;} return true ;}}
In this class, declare an irocketdelegate interface object and use this object to call the interface method. We know that an interface cannot be directly instantiated. In other words, an interface must implement all methods of the interface.
Then, we will hand over these implementations to the specific Launch personnel. Implement callback.
Professionalworker. Java source code
package mark.zhang;public class ProfessionalWorker implements IRocketDelegate {@Overridepublic long startAtTime() {System.out.println("startAtTime is call-back inProfessionalWorker!");return System.currentTimeMillis();}@Overridepublic long endAtTime() {System.out.println("endAtTime is call-back in ProfessionalWorker!");return System.currentTimeMillis();}@Overridepublic void sendDidFail() {System.out.println("ProfessionalWorker send Rocket fail !");}public void send() {if (new Rocket(this).isOk()) {System.out.println("ProfessionalWorker send Rocket ok !");}}}
Sparetimeworker. Java source code
package mark.zhang;public class SparetimeWorker {public void send() {boolean isOk = new Rocket(new IRocketDelegate() {@Overridepublic long startAtTime() {System.out.println("startAtTime is call-back in SparetimeWorker !");return System.currentTimeMillis();}@Overridepublic long endAtTime() {System.out.println("endAtTime is call-back in SparetimeWorker!");return System.currentTimeMillis() + 100L;}@Overridepublic void sendDidFail() {System.out.println("SparetimeWorker send Rocket fail !");}}).isOk();if(isOk) {System.out.println("SparetimeWorker send Rocket ok !");}}}
This class is completed using an internal class.
Test. Java
package mark.zhang;public class Test {public static void main(String[] args) {new ProfessionalWorker().send();System.out.println("*********************************************");new SparetimeWorker().send();}}
Display result
endAtTime is call-back in ProfessionalWorker!startAtTime is call-back inProfessionalWorker!ProfessionalWorker send Rocket ok !*********************************************endAtTime is call-back in SparetimeWorker!startAtTime is call-back in SparetimeWorker !SparetimeWorker send Rocket fail !
This reflects a sentence in Java: Don't call me, I'll call you. It means callback (or delegate/Delegate ).
There are many such usage in Android, and there are still many advantages about interfaces. I will study them later And remember one sentence: object-oriented interface-oriented programming.