Template Method Design Pattern essence: IOC controls reversal, that is, the subclass overrides the method that the parent class wants to override, and then the parent class calls this method. In layman's terms, my father had a desire not fulfilled, so he pinned his hope on his son to achieve it. His son followed the goal set by his father to achieve it, the father satisfies his desires through his son's desire for himself.
The class diagram is as follows:
Use the template mode in android:
Parent Class View:
Public class View {/*** hook operation, empty implement */protected void onDraw (Canvas canvas) {}/ *** hook operation, empty implement */protected void dispatchDraw (Canvas canvas) {}// algorithm skeleton public void draw (Canvas canvas) {if (! VerticalEdges &&! HorizontalEdges) {// Step 1 if (! DirtyOpaque) onDraw (canvas); // Step 2 dispatchDraw (canvas); // step 3 onDrawScrollBars (canvas); return ;}}//......}
Subclass MyView overrides some methods in the parent class:
public class MyView extends View { public MyView(Context context) { super(context); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); } @Override protected void dispatchDraw(Canvas canvas) { super.dispatchDraw(canvas); } }