设计模式之装饰者模式
来源:互联网 发布:在linux用yum安装jdk 编辑:程序博客网 时间:2024/05/29 07:14
装饰者模式:
要点:装饰者与被装饰者拥有共同的超类,继承的目的是继承类型,而不是行为。
装饰者模式的的特点:可以在运行时动态,透明的为一个组件扩展功能,比继承更加灵活,具有弹性;缺点也很明显:它会导致设计中出现许多小对象,增加类的数目,如果过度使用,会让程序变得很复杂。
下面用“煮鱼”做饭为例,描述如何将一条清汤寡水的鱼,不断的装饰(添加葱姜蒜作料),变成一份可口的食物。
1.创建一个超类(抽象类或者借口),装饰者和被装饰对象都要继承,这样可以保证装饰后的对象和装饰前仍属于同一类型。
package com.sample.decorator;public interface IFood {public void cookMethod();public String getDescription();}2.具体原始被装饰者。
package com.sample.decorator;public class Fish implements IFood {@Overridepublic void cookMethod() {// TODO Auto-generated method stubSystem.out.println("煮一条清汤鱼");}@Overridepublic String getDescription() {// TODO Auto-generated method stubreturn "清汤鱼";}}3.抽象的装饰对象,实现所有具体装饰对象的共有属性。
package com.sample.decorator;public abstract class Condiments implements IFood {private IFood food;public Condiments(IFood food){this.food=food;}@Overridepublic void cookMethod() {// TODO Auto-generated method stubfood.cookMethod();}@Overridepublic String getDescription() {// TODO Auto-generated method stubreturn food.getDescription();}}4.具体的装饰对象,装饰对象可以在被装饰对象的接口的前后添加新的功能。
package com.sample.decorator;//葱public class Onion extends Condiments {public Onion(IFood food) {super(food);// TODO Auto-generated constructor stub}@Overridepublic void cookMethod() {// TODO Auto-generated method stubsuper.cookMethod();System.out.println("加点葱");}@Overridepublic String getDescription() {// TODO Auto-generated method stubreturn super.getDescription()+"/+葱";}}
package com.sample.decorator;//姜public class Ginger extends Condiments {public Ginger(IFood food) {super(food);// TODO Auto-generated constructor stub}@Overridepublic void cookMethod() {// TODO Auto-generated method stubsuper.cookMethod();System.out.println("加点姜");}@Overridepublic String getDescription() {// TODO Auto-generated method stubreturn super.getDescription()+"/+姜";}}
package com.sample.decorator;//蒜public class Garlic extends Condiments {public Garlic(IFood food) {super(food);// TODO Auto-generated constructor stub}@Overridepublic void cookMethod() {// TODO Auto-generated method stubsuper.cookMethod();System.out.println("加点大蒜");}@Overridepublic String getDescription() {// TODO Auto-generated method stubreturn super.getDescription()+"/+蒜";}}5.测试用例
package com.sample.decorator;public class CookTest {public static void main(String[] args) {// TODO Auto-generated method stubIFood food=new Fish();food.cookMethod();food.getDescription();System.out.println("----------------------------------------分割线------原始");Onion onionFish=new Onion(food);onionFish.cookMethod();onionFish.getDescription();System.out.println("----------------------------------------分割线------加上葱了");Ginger GingerFish=new Ginger(onionFish);GingerFish.cookMethod();GingerFish.getDescription();System.out.println("----------------------------------------分割线------加上姜了");Garlic GarlicFish=new Garlic(GingerFish);GarlicFish.cookMethod();GarlicFish.getDescription();System.out.println("----------------------------------------分割线------加上姜了");}}6.测试结果:
阅读全文
0 0
- 设计模式之装饰者设计模式
- 设计模式 之 装饰者
- 设计模式之装饰者模式
- 设计模式之装饰者模式
- 设计模式之Decorator(装饰者模式)
- 设计模式之五---装饰者模式
- 设计模式之---装饰者模式
- c++设计模式之装饰者模式
- Php设计模式之装饰者模式
- java设计模式之装饰者模式
- 设计模式之装饰者模式
- 【设计模式】之装饰者模式
- HeadFirst设计模式之装饰者模式
- c++设计模式之装饰者模式
- 设计模式之装饰者模式
- 设计模式之--装饰者模式
- 设计模式之装饰者模式
- 设计模式之装饰者模式
- 【Java】类加载机制
- 敏捷开发原则
- hadoop
- .Net Core(二)EFCore
- 纯css实现瀑布流效果
- 设计模式之装饰者模式
- React Native单选,多选
- JS之异步异常处理
- vijos1404 遭遇战(建图spfa)
- Codeforces Round #433
- javaSE-DAY2-String类
- js中的_poto_和prototype的问题
- CF853B Jury Meeting(dp)
- 最全TEE方案商名单(14家)一句话介绍