您好,登錄后才能下訂單哦!
//處理流:
/模擬咖啡
1、抽象組件:需要裝飾的抽象對(duì)象(接口或抽象父類)
2、具體組件:需要裝飾的對(duì)象
3、抽象裝飾類:包含了對(duì)抽象組件的引用及裝飾著共有的方法
4、具體裝飾類:被裝飾的對(duì)象
/
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{
//對(duì)抽象組件的引用
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";
}
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。