java 基于继承的模板设计模式

[size=medium][color=red][b]
模板设计模式代码[/b][/color][/size]

package com.svse.template;


/**
* 基于继承实现莫办设计模式
* @author Administrator
*
*/
public abstract class BasedExtendTemplate {

//链接数据库
public void beginConnection(){
System.out.println("conection successs");
}

//关闭数据库
public void closeConnection(){
System.out.println("close connection");
}

/**
* 通过派生类的继承对该方法进行重写,达到各自的需求
*/
public abstract void run();

/**
* 在模板方法中有一种函数叫钩子函数
* 作用:让实现类可以通过一些方法来控制模板中的流程,控制是否执行相应的动作
* @return
*/
public abstract boolean isLog();

/**
* 一起执行模板中的方法
*/
public void execute(){
this.beginConnection();
if(isLog()){
System.out.println("添加日志");
}
run();
this.closeConnection();
}

}


[size=medium][color=red][b]具体化实现对应功能:
[/b][/color][/size]


package com.svse.template;

/**
* 基于继承实现模板
* @author Administrator
*
*/
public class RoleDao extends BasedExtendTemplate {

/**
* 对模板中的具体功能进行详细的实现
*/
public void run() {
System.out.println("run");
}

/**
* 钩子函数进行判断
* 是否加入日志
*/
public boolean isLog() {
return true;
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值