Object oriented programming — implementation of callback_ Template method mode

Time:2022-7-28

Implementation of callback

 CallBac Callback function

 Hook Some are also calledHook

Template method mode

When executing code?

The problem with uncertain results is that it can be usedAfter the callback assigns a value to him, it calls

Realize the separation of the two (leave the uncertain to others)

package cn.bjsxt.oop.callback;
/**
*Paintframe painting window
*@author magical dream
*In short, polymorphism.
*To put it bluntly, two or three formulation modes are object-oriented applications
*Define subclasses,Register after subclass definition,Methods related to subclasses will be called automatically after registration
* (As for how it fell, never mind) it has been written in the template
*
*/
//AWT defines aA method like paintframe AWT is in Java
//Hang the classes you have defined on the hook through a hook
public class PaintFrame {
//When calling drawframeCall a subclass object
//If it is inheritance, the local variable MyFrame type
//If it is the interface, the local variable imyframe type
public static void drawFrame(IMyFrame f) {
System.out.println (“start thread”);
System.out.println (“increase cycle”);
System.out.println (“view message line”);
//Draw window
//The method paint of MyFrame was called
//F.print () here is a place to change, which has been written in the template.
//When the time comes, you can transfer which one here.
f.paint();
System.out.println (“start cache to increase efficiency”);
}

public static void main(String[] args) {
//Directly construct the gameframe02 method and call it with drawframe ()
//Template method mode
//When it comes to the construction here, which key will be transferred
drawFrame(new GameFrame02());
//Override call of inherited subclasses
// drawFrame(new GameFrame022());
}
}
class GameFrame01 implements IMyFrame /* extends MyFrame */{
public void paint() {
System.out.println(“GameFrame01.paint()”);
System.out.println (“draw window!”);
}
}

class GameFrame02 implements IMyFrame /* extends MyFrame */{
public void paint() {
System.out.println(“GameFrame01.paint()”);
System.out.println (“draw window!”);
}
}

class GameFrame011 extends MyFrame {
public void paint() {
System.out.println(“GameFrame011.paint()”);
System.out.println (“draw window!”);
}
}
class GameFrame022 extends MyFrame{
public void paint() {
System.out.println(“GameFrame022.paint()”);
System.out.println (“draw window!”);
}
}

package cn.bjsxt.oop.callback;

//MyFrame is a parent class
//public class MyFrame {
// public void paint() {
//System.out.println (“draw your own window”);
// }
//}
//If you want subclasses to write your methods, make an abstract class.
public abstract class MyFrame {
public abstract void paint();
}

//The most important part of the project is the interface
interface IMyFrame{
void paint();
}