装饰模式,制作一个蛋糕java
import java.text.DecimalFormat; //抽象组件组件 interface mkcake { public void cake(); } class Cake implements mkcake { @Override public void cake() { // TODO Auto-generated method stub System.out.println("一个成型蛋糕"); } } //装饰接口 abstract class Decorator implements mkcake { mkcake m; public Decorator(mkcake m) { this.m=m; } } class AddMilk extends Decorator { public AddMilk(mkcake m) { super(m); } @Override public void cake() { // TODO Auto-generated method stub m.cake(); System.out.println("加了牛奶"); } } class AddEgg extends Decorator { public AddEgg(mkcake m) { super(m); } @Override public void cake() { // TODO Auto-generated method stub m.cake(); System.out.println("加了鸡蛋"); } } public class 装饰模式 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub //开始制作蛋糕 mkcake c=new Cake(); mkcake m=new AddMilk(new AddEgg(new Cake())); m.cake(); } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。