模版方法模式定义了一个算法的骨架,它将其中一些步骤的实现推迟到子类里面,使得子类可以在不改变算法结构的情况下重新定义算法中的某些步骤。
设计实现一个制作咖啡的场景,其中一些步骤是相同的,而一些不同的步骤可以由子类重写
// 模板类
abstract class CoffeeTemplate {
// 模板方法,定义咖啡的制作步骤
final void makeCoffee() {
boilWater();
brewCoffeeGrounds();
pourInCup();
addCondiments();
}
// 具体步骤,煮沸水
void boilWater() {
System.out.println("Boiling water");
}
// 具体步骤,冲泡咖啡
void brewCoffeeGrounds() {
System.out.println("Brewing coffee grounds");
}
// 具体步骤,倒入杯中
void pourInCup() {
System.out.println("Pouring into cup");
}
// 具体步骤,添加调料,子类实现
abstract void addCondiments();
}
// 具体子类,制作茶
class TeaTemplate extends CoffeeTemplate {
@Override
void addCondiments() {
System.out.println("Adding lemon");
}
}
// 具体子类,制作咖啡
class CoffeeTemplateImpl extends CoffeeTemplate {
@Override
void addCondiments() {
System.out.println("Adding sugar and milk");
}
}
// 客户端代码
public class Client {
public static void main(String[] args) {
CoffeeTemplate tea = new TeaTemplate();
tea.makeCoffee();
CoffeeTemplate coffee = new CoffeeTemplateImpl();
coffee.makeCoffee();
}
}
// 模板类
abstract class CoffeeTemplate {
// 模板方法,定义咖啡的制作步骤
final void makeCoffee() {
boilWater();
brewCoffeeGrounds();
pourInCup();
if (customerWantsCondiments()) {
addCondiments();
}
}
// 具体步骤,煮沸水
void boilWater() {
System.out.println("Boiling water");
}
// 具体步骤,冲泡咖啡
void brewCoffeeGrounds() {
System.out.println("Brewing coffee grounds");
}
// 具体步骤,倒入杯中
void pourInCup() {
System.out.println("Pouring into cup");
}
// 具体步骤,添加调料,子类实现
abstract void addCondiments();
// 钩子方法,决定是否添加调料,默认添加
boolean customerWantsCondiments() {
return true;
}
}
// 具体子类,制作茶
class TeaTemplate extends CoffeeTemplate {
@Override
void addCondiments() {
System.out.println("Adding lemon");
}
// 通过重写钩子方法,决定是否添加调料
@Override
boolean customerWantsCondiments() {
// 用户不想要调料
return false;
}
}
// 具体子类,制作咖啡
class CoffeeTemplateImpl extends CoffeeTemplate {
@Override
void addCondiments() {
System.out.println("Adding sugar and milk");
}
}
// 客户端代码
public class Client {
public static void main(String[] args) {
CoffeeTemplate tea = new TeaTemplate();
tea.makeCoffee();
CoffeeTemplate coffee = new CoffeeTemplateImpl();
coffee.makeCoffee();
}
}
// 使用钩子方法 customerWantsCondiments 来判断是否执行添加调料的步骤,然后在 TeaTemplate 中
// 重写钩子方法,用户可以选择是否添加。CoffeeTemplateImpl 中没有重写钩子方法,就默认执行
// 策略接口,调料的添加策略
interface CondimentsStrategy {
void addCondiments();
}
// 具体的调料策略,添加柠檬
class LemonCondiments implements CondimentsStrategy {
@Override
public void addCondiments() {
System.out.println("Adding lemon");
}
}
// 具体的调料策略,添加糖和牛奶
class SugarAndMilkCondiments implements CondimentsStrategy {
@Override
public void addCondiments() {
System.out.println("Adding sugar and milk");
}
}
// 模板类
abstract class CoffeeTemplate {
// 策略对象,用于调料的添加
private CondimentsStrategy condimentsStrategy;
// 设置调料策略
void setCondimentsStrategy(CondimentsStrategy condimentsStrategy) {
this.condimentsStrategy = condimentsStrategy;
}
// 模板方法,定义咖啡的制作步骤
final void makeCoffee() {
boilWater();
brewCoffeeGrounds();
pourInCup();
addCondiments();
}
// 具体步骤,煮沸水
void boilWater() {
System.out.println("Boiling water");
}
// 具体步骤,冲泡咖啡
void brewCoffeeGrounds() {
System.out.println("Brewing coffee grounds");
}
// 具体步骤,倒入杯中
void pourInCup() {
System.out.println("Pouring into cup");
}
// 具体步骤,添加调料,通过策略对象调用
void addCondiments() {
if (condimentsStrategy != null) {
condimentsStrategy.addCondiments();
}
}
}
// 具体子类,制作茶
class TeaTemplate extends CoffeeTemplate {
// 构造方法中设置具体的调料策略
TeaTemplate() {
setCondimentsStrategy(new LemonCondiments());
}
}
// 具体子类,制作咖啡
class CoffeeTemplateImpl extends CoffeeTemplate {
// 构造方法中设置具体的调料策略
CoffeeTemplateImpl() {
setCondimentsStrategy(new SugarAndMilkCondiments());
}
}
// 客户端代码
public class Client {
public static void main(String[] args) {
CoffeeTemplate tea = new TeaTemplate();
tea.makeCoffee();
CoffeeTemplate coffee = new CoffeeTemplateImpl();
coffee.makeCoffee();
}
}
// 引入 CondimentsStrategy 策略接口和具体的策略实现类:LemonCondiments 和 SugarAndMilkCondiments
// 如此将调料的添加变成一个可变的部分。在 CoffeeTemplateImpl 中引入策略对象,通过
// setCondimentsStrategy 设置具体的调料策略
// 模板类
abstract class CoffeeTemplate {
// 策略对象,用于调料的添加
private CondimentsStrategy condimentsStrategy;
// 设置调料策略
void setCondimentsStrategy(CondimentsStrategy condimentsStrategy) {
this.condimentsStrategy = condimentsStrategy;
}
// 模板方法,定义咖啡的制作步骤
final void makeCoffee() {
boilWater();
brewCoffeeGrounds();
pourInCup();
addCondiments();
}
// 具体步骤,煮沸水
void boilWater() {
System.out.println("Boiling water");
}
// 具体步骤,冲泡咖啡
void brewCoffeeGrounds() {
System.out.println("Brewing coffee grounds");
}
// 具体步骤,倒入杯中
void pourInCup() {
System.out.println("Pouring into cup");
}
// 具体步骤,添加调料,通过策略对象调用
void addCondiments() {
if (condimentsStrategy != null) {
condimentsStrategy.addCondiments();
}
}
}
// 具体子类,制作茶
class TeaTemplate extends CoffeeTemplate {
// 构造方法中设置具体的调料策略
TeaTemplate() {
setCondimentsStrategy(new LemonCondiments());
}
// 具体步骤,添加茶叶
void addTeaLeaves() {
System.out.println("Adding tea leaves");
}
}
// 具体子类,制作咖啡
class CoffeeTemplateImpl extends CoffeeTemplate {
// 构造方法中设置具体的调料策略
CoffeeTemplateImpl() {
setCondimentsStrategy(new SugarAndMilkCondiments());
}
// 具体步骤,添加咖啡粉
void addCoffeePowder() {
System.out.println("Adding coffee powder");
}
}
// 客户端代码
public class Client {
public static void main(String[] args) {
TeaTemplate tea = new TeaTemplate();
tea.makeCoffee();
tea.addTeaLeaves();
CoffeeTemplateImpl coffee = new CoffeeTemplateImpl();
coffee.makeCoffee();
coffee.addCoffeePowder();
}
}
迭代器模式定义了一种方法来顺序访问一个容器对象中的各个元素,而不需要暴露该对象的内部细节,把对元素的访问和遍历从容器对象中分离出来,使得容器和迭代器可以独立地变化。
设计实现一个集合类来实现迭代器模式
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
// 迭代器接口
interface MyIterator {
boolean hasNext();
Object next();
}
// 集合接口
interface MyCollection {
MyIterator createIterator();
}
// 具体迭代器实现
class MyListIterator implements MyIterator {
private List
迭代器模式不适用于所有的集合,主要是集合内部结构不方便直接使用迭代器的场景。