Template Method - advanced application hook (hook)

Template Method pattern entry: https: //blog.csdn.net/dengjili/article/details/79631472

Modify the design

 

 


Add hook, wherein the hook is optional

Examples are given, then an explanation is given

Package headfirst.hd.template.eg; 

public  abstract  class the AbstractClass { 

// not to overwrite this method subclasses declared as final type
 // parent class definition program performing template 
public  final  void templateMethod () { 
the init (); 
privimitiveOperation1 ( ); // can be anywhere 
doSomething (); 
privimitiveOperation2 (); // can be any position of 
the destroy (); 
Hook (); 
} 

// defined as a type of policy holders, this method is not visible outside 
protected  void Hook () {
 // empty implementation, leaving room subclasses scalable 
} 

public  void the init () { 
System.out.println ("This is a parent class init initialization method, needs to do many things" ); 
} 

public  void destroy () { 
System.out.println ( "This is a parent class initialization destroy method, needs to do many things \ n-" ); 
} 

public  void doSomething () { 
System.out.println ( "do a lot" ); 
} 

// general method for data not many abstract 
public  abstract  void privimitiveOperation1 ();
 public  abstract  void privimitiveOperation2 (); 
}

 

A subclass without rewriting the hook method

Package headfirst.hd.template.eg; 

public  class ConcreteClass the extends the AbstractClass { 

@Override 
public  void privimitiveOperation1 () { 
System.out.println ( "ConcreteClass class, subclass 1, do a lot of things" ); 
} 

@Override 
public  void privimitiveOperation2 () { 
System.out.println ( "ConcreteClass class, subclass method, a lot of things to do" ); 
} 

}

 

Two subclasses, hook method override

Package headfirst.hd.template.eg; 

public  class ConcreteClass2 the extends the AbstractClass { 

@Override 
public  void privimitiveOperation1 () { 
System.out.println ( "Class ConcreteClass2, subclass 1, do a lot of things" ); 
} 

@Override 
public  void privimitiveOperation2 () { 
System.out.println ( "class ConcreteClass2, subclass method, a lot of things to do" ); 
} 

@Override 
protected  void hook () { 
System.out.println ( "category ConcreteClass2, hook method, add additional features " ); 
} 
}

 

Test code

Package headfirst.hd.template.eg; 

public  class Client { 

// Template Method: Define program execution process, some of the steps to achieve the subclass 
public  static  void main (String [] args) { 
the AbstractClass temlate = new new ConcreteClass (); 
temlate. templateMethod (); 

// replace the second sub-category, program execution flow constant 
temlate = new new ConcreteClass2 (); 
temlate.templateMethod (); 
} 

}

 

Test Results

 

 

This is the parent class initialization init method, we need to do a lot of things 
like ConcreteClass, subclass 1, do a lot of things 
to do many things 
like ConcreteClass, subclass method, do a lot of things 
which is the parent of an initialization destroy method, the need do a lot of things 

which is the parent of an initialization init method, we need to do a lot of things 
like ConcreteClass2, subclass 1, do a lot of things 
to do many things 
like ConcreteClass2, subclass method, do a lot of things 
which is the parent of an initialization destroy method, we need to do a lot of things 

like ConcreteClass2, hook method, add some additional features

 

Conclusion subclass concluded, hook left hook method is a subclass of choice

Other manifestations of hooks hook

public  Final  void templateMethod () { 
the init (); 
privimitiveOperation1 (); // can be anywhere 
doSomething (); 
privimitiveOperation2 (); // can be any position of 
the destroy (); 

IF (isHook ()) { 
Hook (); 
} 
} 

Private  Boolean isHook () {
 return  to false ; 
} 
// defined for policy holders method, this method is not visible outside 
protected  void Hook () {
 // empty implementation, leaving room subclasses scalable 
}

 


Which is isHook hook method, because it is to decide whether to call the code in brackets, a subclass can override this method to determine

hook hook method is not, can not decide whether to execute the code

Methods of paint JFrame hooks of hook java
example

package callback.other;

import javax.swing.JFrame;

public class TestFrame extends JFrame {

private static final long serialVersionUID = 1L;

public TestFrame() {
super("钩子hook");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400,400);
setVisible(true);
}
}

test

package callback.other;

public class MyFrame {
public static void main(String[] args) {
new TestFrame();
}

}

 

Test Results

 

Calls the hook method paint

package callback.other;

import java.awt.Graphics;

import javax.swing.JFrame;

public class TestFrame extends JFrame {

private static final long serialVersionUID = 1L;

public TestFrame() {
super("钩子hook");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400,400);
setVisible(true);
}

@Override
public void paint(Graphics g) {
super.paint(g);

g.drawString("Hook override method", 180, 180 ); 
} 
}

 


Test Results

 

 

----------------
Disclaimer: This article is the original article CSDN bloggers "dengjili", and follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement. .
Original link: https: //blog.csdn.net/dengjili/article/details/79657157

Guess you like

Origin www.cnblogs.com/yhxb/p/11799565.html