JAVA中的设计模式 - 模版方法模式
2017-12-14 12:02
302 查看
JAVA中的设计模式 - 模版方法模式
模版方法模式:通过调用抽象父类,实现对子类的调用1.构建抽象类
import java.util.Random; /** * Created by 谭健 on 2017/12/14. 11:45. * © All Rights Reserved. */ public abstract class AbstractParent { public final int birthToChild(int bound) { System.out.println("调用了抽象类自己的 birthToChild() 方法。"); int number = new Random().nextInt(bound); return birthToChild(correct(number),number); } public abstract int birthToChild(boolean isDie,int number); public boolean correct(int number) { System.out.println("调用了抽象类自己的 correct() 方法。"); return number == 0; } }
2.构建子类
/** * Created by 谭健 on 2017/12/14. 11:53. * © All Rights Reserved. */ public class RealChild extends AbstractParent { @Override public int birthToChild(boolean isDie, int number) { System.out.println("调用了抽象类子类的 birthToChild() 方法。"); return 4000 isDie ? 0 : number; } public static void main(String[] args) { AbstractParent parent = new RealChild(); parent.birthToChild(2); } }
3.输出
调用了抽象类自己的 birthToChild() 方法。 调用了抽象类自己的 correct() 方法。 调用了抽象类子类的 birthToChild() 方法。
相关文章推荐
- Java设计模式(二)之模版方法模式
- 12.java设计模式(读书笔记)模版方法模式
- Java设计模式之模版方法模式简介
- java设计模式(九)---模版方法模式
- 设计模式(java)--模版方法模式之任务分配
- java设计模式(二):Template Method(模版方法)
- 【Java设计模式09】——模版方法模式
- java设计模式八--Template Method(方法模版模式)
- java设计模式【全】-模版方法模式
- java设计模式八:--Template Method(方法模版模式)
- 23种设计模式(12)java模版方法模式
- java设计模式-模版方法
- Java设计模式_(行为型)_模版方法模式
- Java设计模式笔记(4)模版方法模式
- 黑马程序员——JAVA基础之抽象和接口 , 模版方法设计模式
- Java设计模式(六) 模版方法模式详解
- JAVA--模版方法模式(Template Method)--设计模式十四
- java笔记--设计模式之模版方法模式
- 黑马程序员-Java_模版方法设计模式(Template Pattern)