告别祖传代码 -Java 8 语境下模板模式的再审视
点击上方“蓝字”关注我们
模板模式回顾
public abstract class AbstractProcessor {
public final void process() {
step1();
step2();
step3();
}
public abstract void step1();
public abstract void step2();
public abstract void step3();
}
public class ClientLogic {
private AbstractProcessor processor;
public void foo() {
processor.process();
}
}
public class ConcreteProcessor1 extends AbstractProcessor {
@Override
public void step1() {
System.out.println("Step1 Logic in ConcreteProcessor1");
}
@Override
public void step2() {
System.out.println("Step2 Logic in ConcreteProcessor1");
}
@Override
public void step3() {
System.out.println("Step3 Logic in ConcreteProcessor1");
}
}
public class ConcreteProcessor2 extends ConcreteProcessor1{
@Override
public void step3() {
System.out.println("Step3 Logic in ConcreteProcessor2");
}
}
现实的问题
public abstract class AbstractProcessor {
public final void process() {
step1();
step2();
step3();
step4();
step5();
}
public abstract void step1();
public abstract void step2();
public abstract void step3();
public abstract void step4();
public abstract void step5();
}
public class DefaultProcessor extends AbstractProcessor {
@Override
public void step1() {
System.out.println("default step1");
}
@Override
public void step2() {
System.out.println("default step2");
}
@Override
public void step3() {
System.out.println("default step3");
}
@Override
public void step4() {
System.out.println("default step4");
}
@Override
public void step5() {
System.out.println("default step5");
}
}
问题
理论
表示类 A 和类 B 之间共享代码的行数,
表示类 B 的代码总行数,
表示类 A 和类 B 的关系系数。
,取值范围从 0 到 1,0 表示没有关系,1 表示最大关系,可以将取值对应到 Dependency、Association、Aggregation、Composition、Inheritance 五类关系中,取值从小到大。
,如果类 A 被类 B 继承,那么
等于类 A 所有非 private 修饰的代码行数;如果类 A 只是作为引用被类 B 调用,那么
等于类 A 所有 public 方法签名的行数。
取值更大,而且
取值也更大,从而导致耦合度更高。
解决方案
public class TemplatePattern1 {
private Step1Processor step1Processor;
private Step2Processor step2Processor;
private Step3Processor step3Processor;
private Step4Processor step4Processor;
private Step5Processor step5Processor;
public interface Step1Processor {
void step1();
}
public interface Step2Processor {
void step2();
}
public interface Step3Processor {
void step3();
}
public interface Step4Processor {
void step4();
}
public interface Step5Processor {
void step5();
}
public void templateMethod() {
step1Processor.step1();
step2Processor.step2();
step3Processor.step3();
step4Processor.step4();
step5Processor.step5();
}
}
public class TemplatePattern2 {
public interface Step1Processor {
void step1();
}
public interface Step2Processor {
void step2();
}
public interface Step3Processor {
void step3();
}
public interface Step4Processor {
void step4();
}
public interface Step5Processor {
void step5();
}
public void templateMethod(
Step1Processor step1Processor,
Step2Processor step2Processor,
Step3Processor step3Processor,
Step4Processor step4Processor,
Step5Processor step5Processor) {
step1Processor.step1();
step2Processor.step2();
step3Processor.step3();
step4Processor.step4();
step5Processor.step5();
}
}
public interface TemplatePattern3 {
void templateMethod(Step1Processor step1Processor,
Step2Processor step2Processor,
Step3Processor step3Processor,
Step4Processor step4Processor,
Step5Processor step5Processor);
static TemplatePattern3 getTemplate(Step1Processor step1Processor,
Step2Processor step2Processor,
Step3Processor step3Processor,
Step4Processor step4Processor,
Step5Processor step5Processor){
return (step1Processor1, step2Processor1, step3Processor1, step4Processor1, step5Processor1) -> {
step1Processor1.step1();
step1Processor1.step1();
step1Processor1.step1();
step1Processor1.step1();
step1Processor1.step1();
};
}
}
总结
猜你喜欢
近期活动:金科大咖 云讲坛
如何融汇科技,打造数字化转型引擎?
如何共享资源,主动赋能同业与社会?
如何共建生态,构建无界多维的生态空间?
如何创新引领,成为新金融的实践者和先行者?
38位金融科技大咖应邀云端,与你探索未来科技前沿方向。豪华专家师资队伍,精选38项高端课程,尽在优源汇!
参与方式:关注公众号,点击“金科大咖”进入直播间
本文分享自微信公众号 - 金科优源汇(jkyyh2020)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

