//模板模式----实现编程式事务管理
abstract class TransactionTemplate{
public void transaction(){
beforeTransaction();
doInTransaction();
afterTransaction();
}
private void beforeTransaction(){
System.out.println("事务开始前");
}
private void afterTransaction(){
System.out.println("事务结束后");
}
public abstract void doInTransaction();
}
class MyTransaction extends TransactionTemplate{
@Override
public void doInTransaction() {
System.out.println("进行转账操作");
}
}
public class Demo{
public static void main(String [] args){
MyTransaction myTransaction=new MyTransaction();
myTransaction.transaction();
}
}
//回调模式----实现编程式事务管理
interface InTransaction{
public void doInTransaction();
}
class TransactionTemplate{
public void transaction(InTransaction inTransaction){
beforeTransaction();
inTransaction.doInTransaction();
afterTransaction();
}
private void beforeTransaction(){
System.out.println("事务开始前");
}
private void afterTransaction(){
System.out.println("事务结束后");
}
}
public class Demo{
public static void main(String [] args){
//回调方法模式
new TransactionTemplate().transaction(new InTransaction() {
@Override
public void doInTransaction() {
System.out.println("进行转账操作");
}
});
}
}