用Java8改造后的模板方法模式真的是yyds!
/**
* 模板方法设计模式的抽象类
* @author hollis
*/
public abstract class AbstractBusinessHandler {
/**
* 模板方法
*/
public final void execute(){
getNumber();
handle();
judge();
}
/**
* 取号
* @return
*/
private void getNumber(){
System.out.println("number-00" + RandomUtils.nextInt());
}
/**
* 办理业务
*/
public abstract void handle(); //抽象的办理业务方法,由子类实现
/**
* 评价
*/
private void judge(){
System.out.println("give a praised");
}
}
public class SaveMoneyHandler extends AbstractBusinessHandeler {
@Override
public void handle() {
System.out.println("save 1000");
}
}
public static void main(String []args){
SaveMoneyHandler saveMoneyHandler = new SaveMoneyHandler();
saveMoneyHandler.execute();
}
number-00958442164
save 1000
give a praised
//取钱业务的实现类
public class DrawMoneyHandler extends AbstractBusinessHandeler {
@Override
public void handle() {
System.out.println("draw 1000");
}
}
//理财业务的实现类
public class MoneyManageHandler extends AbstractBusinessHandeler{
@Override
public void handle() {
System.out.println("money manage");
}
}
public List
getList() {
return new ArrayList();
}
Supplier
>>
listSupplier = ArrayList::new;
public void sum(String a1) {
System.out.println(a1);
}
Consumer<String> printConsumer = a1 -> System.out.println(a1);
Consumer
stringConsumer = (s) -> System.out.println(s.length());
Arrays.asList("ab", "abc", "a", "abcd").stream().forEach(stringConsumer);
/**
* @author Hollis
*/
public class BankBusinessHandler {
private void execute(Consumerconsumer ) {
getNumber();
consumer.accept(null);
judge();
}
private void getNumber() {
System.out.println("number-00" + RandomUtils.nextInt());
}
private void judge() {
System.out.println("give a praised");
}
}
/**
* @author Hollis
*/
public class BankBusinessHandler {
public void save(BigDecimal amount) {
execute(a -> System.out.println("save " + amount));
}
}
public static void main(String[] args) throws {
BankBusinessHandler businessHandler = new BankBusinessHandler();
businessHandler.save(new BigDecimal("1000"));
}
number-001736151440
save1000
give a praised
/**
* @author Hollis
*/
public class BankBusinessHandler {
public void save(BigDecimal amount) {
execute(a -> System.out.println("save " + amount));
}
public void draw(BigDecimal amount) {
execute(a -> System.out.println("draw " + amount));
}
public void moneyManage(BigDecimal amount) {
execute(a -> System.out.println("draw " + amount));
}
}
/**
* 模板方法
*/
protected void execute(Suppliersupplier, Consumer ) {consumer
String number = supplier.get();
System.out.println(number);
if (number.startsWith("vip")) {
//Vip号分配到VIP柜台
System.out.println("Assign To Vip Counter");
}
else if (number.startsWith("reservation")) {
//预约号分配到专属客户经理
System.out.println("Assign To Exclusive Customer Manager");
}else{
//默认分配到普通柜台
System.out.println("Assign To Usual Manager");
}
consumer.accept(null);
judge();
}
public class BankBusinessHandler extends AbstractBusinessHandler {
public void saveVip(BigDecimal amount) {
execute(() -> "vipNumber-00" + RandomUtils.nextInt(), a -> System.out.println("save " + amount));
}
public void save(BigDecimal amount) {
execute(() -> "number-00" + RandomUtils.nextInt(), a -> System.out.println("save " + amount));
}
public void saveReservation(BigDecimal amount) {
execute(() -> "reservationNumber-00" + RandomUtils.nextInt(), a -> System.out.println("save " + amount));
}
}
BankBusinessHandler businessHandler = new BankBusinessHandler();
businessHandler.saveVip(new BigDecimal("1000"));
vipNumber-001638110566
Assign To Vip Counter
save 1000
give a praised
程序汪资料链接
卧槽!字节跳动《算法中文手册》火了,完整版 PDF 开放下载!
卧槽!阿里大佬总结的《图解Java》火了,完整版PDF开放下载!
欢迎添加程序汪个人微信 itwang009 进粉丝群或围观朋友圈