温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

java装饰者模式如何使用

发布时间:2022-06-01 15:18:08 来源:亿速云 阅读:167 作者:iii 栏目:大数据

本篇内容介绍了“java装饰者模式如何使用”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

1、使用说明

(1)装饰者模式可以带来比继承更加灵活的扩展功能,使用更加方法,可以通过组合不同的装饰者对象来获取具有不同行为状态的多样化的结果。装饰者模式比继承更具良好的扩展性,完美的遵循开闭原则,继承是静态的附加责任,装饰者则是动态的附加责任。

(2)装饰类和被装饰类可以独立发展,不会相互耦合,装饰模式是继承的一个替代模式,装饰模式可以动态扩展一个实现类的功能。

2、实例

public class HelloWorld {     public static void main(String[] args) {         //点一份炒饭         FastFood food = new FriedRice();         //花费的价格         System.out.println(food.getDesc() + " " + food.cost() + "元");           System.out.println("========");           //点一份加鸡蛋的炒饭         FastFood food1 = new FriedRice();         food1 = new Egg(food1);         //花费的价格         System.out.println(food1.getDesc() + " " + food1.cost() + "元");           System.out.println("========");           //点一份加培根的炒面         FastFood food2 = new FriedNoodles();         food2 = new Bacon(food2);         //花费的价格         System.out.println(food2.getDesc() + " " + food2.cost() + "元");     } }   // 快餐抽象类 abstract class FastFood {     private float price;     private String desc;       public FastFood() {}       public FastFood(float price, String desc) {         this.price = price;         this.desc = desc;     }       public float getPrice() {         return price;     }       public void setPrice(float price) {         this.price = price;     }       public String getDesc() {         return desc;     }       public void setDesc(String desc) {         this.desc = desc;     }       // 获取价格     public abstract float cost(); }   // 炒饭 class FriedRice extends FastFood {     public FriedRice() {         super(10, "炒饭");     }       @Override     public float cost() {         return getPrice();     } }   // 炒面 class FriedNoodles extends FastFood {     public FriedNoodles() {         super(12, "炒面");     }       @Override     public float cost() {         return getPrice();     } }   // 配料 abstract class Garnish extends FastFood {     private FastFood fastFood;       public FastFood getFastFood() {         return fastFood;     }       public void setFastFood(FastFood fastFood) {         this.fastFood = fastFood;     }       public Garnish(FastFood fastFood, float price, String desc) {         super(price, desc);         this.fastFood = fastFood;     } }   // 鸡蛋配料 class Egg extends Garnish {     public Egg(FastFood fastFood) {         super(fastFood, 1, "鸡蛋");     }       @Override     public float cost() {         return getPrice() + getFastFood().getPrice();     }       @Override     public String getDesc() {         return super.getDesc() + getFastFood().getDesc();     } }   //培根配料 class Bacon extends Garnish {     public Bacon(FastFood fastFood) {         super(fastFood,2,"培根");     }       @Override     public float cost() {         return getPrice() + getFastFood().getPrice();     }       @Override     public String getDesc() {         return super.getDesc() + getFastFood().getDesc();     } }

“java装饰者模式如何使用”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI