设计模式:模板模式(包含abstract class的使用方法)

CaffeineBeverage.d
module CaffeineBeverage;
import  std.stdio;
/**
  *这个程序程序是为了测试templete模式使用
  *
  */
public abstract class CaffineBeverage{
	void prepareRecipe(){
		boilWater();	///烧水
		brew();	///浸制
		pourInCup();	///倒水
		addCondiments();	///加调味品
	}
	void boilWater(){
		writefln("Boiling water.");
	}
	abstract void brew();
	abstract void addCondiments();
	void pourInCup(){
		writefln("Pouring into cup.");
	}	
}

public class Tea : CaffineBeverage{
	void brew(){
		writefln("Steeping the tea.");
	}
	void addCondiments(){
		writefln("Adding Lemon.");
	}
}
public class Coffee : CaffineBeverage{
	void brew(){
		writefln("Dripping Coffee through filter.");
	}
	void addCondiments(){
		writefln("Adding Sugar and Milk.");
	}
}


runCaffeineBeverage.d
import CaffeineBeverage;
int main(char[][] args){
	Coffee coffee=new Coffee();
	coffee.prepareRecipe();
	Tea tea=new Tea();
	tea.prepareRecipe();
	return 1;
}

dmd runCaffeineBeverage.d CaffineBeverage.D -I

你可能感兴趣的:(设计模式,brew)