学妹惊呼:使用Java8改造后的模板方法模式真的是yyds
共 11102字,需浏览 23分钟
·
2021-08-24 17:51
/**
* 模板方法设计模式的抽象类
* @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<String> getList() {
return new ArrayList();
}
Supplier<List<String>> listSupplier = ArrayList::new;
public void sum(String a1) {
System.out.println(a1);
}
Consumer<String> printConsumer = a1 -> System.out.println(a1);
Consumer<String> stringConsumer = (s) -> System.out.println(s.length());
Arrays.asList("ab", "abc", "a", "abcd").stream().forEach(stringConsumer);
/**
* @author Hollis
*/
public class BankBusinessHandler {
private void execute(Consumer<BigDecimal> consumer) {
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(Supplier<String> supplier, Consumer<BigDecimal> 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
最近有很多人问,有没有读者交流群,想知道怎么加入。
最近我创建了一些群,大家可以加入。交流群都是免费的,只需要大家加入之后不要随便发广告,多多交流技术就好了。
目前创建了多个交流群,全国交流群、北上广杭深等各地区交流群、面试交流群、资源共享群等。
有兴趣入群的同学,可长按扫描下方二维码,一定要备注:全国 Or 城市 Or 面试 Or 资源,根据格式备注,可更快被通过且邀请进群。
往期推荐
干掉 Postman?测试接口直接生成API文档,这个工具我爱了
程序员千万不要做舔狗啊!
官方认证:软件及信息技术从业者为新生代农民工