//处理流:
/模拟咖啡
1、抽象组件:需要装饰的抽象对象(接口或抽象父类)
2、具体组件:需要装饰的对象
3、抽象装饰类:包含了对抽象组件的引用及装饰着共有的方法
4、具体装饰类:被装饰的对象
/
public class hel<E>{
public static void main(String[]args)
{
Drink coffee =new Coffee();
Drink suger =new Suger(coffee); //装饰
System.out.println(suger.info()+"-->"+suger.cost());
Drink milk =new Milk(coffee); //装饰
System.out.println(milk.info()+"-->"+milk.cost());
milk=new Milk(suger); //装饰
System.out.println(milk.info()+"-->"+milk.cost());
}
}
//抽象组件
interface Drink{
double cost();
String info();
}
//具体组件
class Coffee implements Drink{
private String name="原味咖啡";
public double cost()
{
return 10;
}
public String info()
{
return name;
}
}
//抽象装饰类
abstract class Decorate implements Drink{
//对抽象组件的引用
private Drink drink;
public Decorate(Drink drink) {
this.drink=drink;
}
public double cost()
{
return this.drink.cost();
}
public String info() {
return this.drink.info();
}
}
//具体装饰类
class Milk extends Decorate{
public Milk(Drink drink) {
super(drink);
// TODO Auto-generated constructor stub
}
public double cost()
{
return super.cost()*4;
}
public String info() {
return super.info()+"Milk";
}
}
class Suger extends Decorate{
public Suger(Drink drink) {
super(drink);
// TODO Auto-generated constructor stub
}
public double cost()
{
return super.cost()*2;
}
public String info() {
return super.info()+"Suger";
}
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。