装饰模式,制作一个蛋糕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();

    }

}
posted @ 2014-07-18 21:36  hansongjiang8  阅读(525)  评论(0编辑  收藏  举报