当前位置: 首页 > news >正文

技术支持 东莞网站建设bmapgmap百度站长资源平台

技术支持 东莞网站建设bmapgmap,百度站长资源平台,怎么给网站加速,行业网站策划方案概述 当我们编写软件时,有时我们会遇到需要在不修改现有代码的情况下添加新功能的情况。这时,我们可以使用装饰器模式。 装饰器模式是一种结构性设计模式,它允许我们在不改变对象接口的情况下动态地向对象添加功能。装饰器模式通过创建一个…

概述

当我们编写软件时,有时我们会遇到需要在不修改现有代码的情况下添加新功能的情况。这时,我们可以使用装饰器模式。

装饰器模式是一种结构性设计模式,它允许我们在不改变对象接口的情况下动态地向对象添加功能。装饰器模式通过创建一个包装对象来实现这一目的。这个包装对象具有与原始对象相同的接口,但可以通过添加或覆盖方法来扩展或修改其行为。

装饰器模式通常用于以下情况:

  • 在运行时动态地向对象添加新功能,而不会影响其他对象。
  • 在不改变现有代码的情况下添加新功能。
  • 以层次结构方式组合对象以获得更大的灵活性。

代码示例

下面是一个使用Java实现的装饰器模式的示例,以模拟咖啡店销售咖啡的过程。
首先,我们定义一个接口 Coffee,表示咖啡的基本特性,包括名称和价格:

public interface Coffee {String getDescription();double getCost();
}

然后,我们定义一个具体的咖啡类 SimpleCoffee,它实现了 Coffee 接口:

public class SimpleCoffee implements Coffee {@Overridepublic String getDescription() {return "Simple coffee";}@Overridepublic double getCost() {return 1.0;}
}

这个类表示最简单的咖啡,没有任何装饰。

接下来,我们定义一个装饰器类 CoffeeDecorator,它也实现了 Coffee 接口,但是它包含一个 Coffee 类型的成员变量 decoratedCoffee:

public abstract class CoffeeDecorator implements Coffee {protected Coffee decoratedCoffee;public CoffeeDecorator(Coffee decoratedCoffee) {this.decoratedCoffee = decoratedCoffee;}@Overridepublic String getDescription() {return decoratedCoffee.getDescription();}@Overridepublic double getCost() {return decoratedCoffee.getCost();}
}

这个类的作用是让子类可以方便地覆盖 getDescription() 和 getCost() 方法,以添加额外的装饰。
接下来,我们定义两个具体的装饰器类:Milk 和 Whip,它们分别添加牛奶和奶泡:

public class Milk extends CoffeeDecorator {public Milk(Coffee decoratedCoffee) {super(decoratedCoffee);}@Overridepublic String getDescription() {return decoratedCoffee.getDescription() + ", Milk";}@Overridepublic double getCost() {return decoratedCoffee.getCost() + 0.5;}
}public class Whip extends CoffeeDecorator {public Whip(Coffee decoratedCoffee) {super(decoratedCoffee);}@Overridepublic String getDescription() {return decoratedCoffee.getDescription() + ", Whip";}@Overridepublic double getCost() {return decoratedCoffee.getCost() + 0.7;}
}

这些类都是 CoffeeDecorator 的子类,它们实现了自己的 getDescription() 和 getCost() 方法来添加额外的功能。

最后,我们可以使用这些类来创建不同类型的咖啡,例如:

Coffee simpleCoffee = new SimpleCoffee();
System.out.println(simpleCoffee.getDescription() + " $" + simpleCoffee.getCost());Coffee coffeeWithMilk = new Milk(new SimpleCoffee());
System.out.println(coffeeWithMilk.getDescription() + " $" + coffeeWithMilk.getCost());Coffee coffeeWithMilkAndWhip = new Whip(new Milk(new SimpleCoffee()));
System.out.println(coffeeWithMilkAndWhip.getDescription() + " $" + coffeeWithMilkAndWhip.getCost());

JDK中的应用

在 Java IO 库中,许多类都使用了装饰器模式来提供更高级的功能。这些类都是在基本的输入和输出类之上建立的,通过使用不同的装饰器来组合它们,从而实现了各种不同的功能。

例如,BufferedReader 和 BufferedWriter 类就是基于装饰器模式实现的。BufferedReader 类是 Reader 类的一个装饰器,它提供了缓冲输入的功能。当我们需要从一个输入流中读取数据时,BufferedReader 将输入流中的数据读取到缓冲区中,然后逐个字符地返回它们。这样做的好处是可以减少系统调用,从而提高效率。

以下是一个使用 BufferedReader 类的示例:

FileReader fileReader = new FileReader("file.txt");
BufferedReader bufferedReader = new BufferedReader(fileReader);String line;
while ((line = bufferedReader.readLine()) != null) {System.out.println(line);
}bufferedReader.close();
fileReader.close();

在这个例子中,我们首先创建了一个 FileReader 对象来读取文件,然后将其传递给 BufferedReader 的构造函数。BufferedReader 对象使用 FileReader 对象作为其输入源,并提供了缓冲输入的功能。在 while 循环中,我们使用 readLine() 方法逐行读取文件,并打印每一行。

类似的,BufferedWriter 类也是 Writer 类的一个装饰器,它提供了缓冲输出的功能。当我们需要将数据写入到一个输出流中时,BufferedWriter 将数据写入到缓冲区中,直到缓冲区满或者我们调用 flush() 方法时,才将缓冲区中的数据写入到输出流中。

以下是一个使用 BufferedWriter 类的示例:

FileWriter fileWriter = new FileWriter("file.txt");
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);bufferedWriter.write("Hello, world!");
bufferedWriter.newLine();
bufferedWriter.write("How are you?");bufferedWriter.close();
fileWriter.close();

在这个例子中,我们首先创建了一个 FileWriter 对象来写入文件,然后将其传递给 BufferedWriter 的构造函数。BufferedWriter 对象使用 FileWriter 对象作为其输出目标,并提供了缓冲输出的功能。我们使用 write() 方法将字符串写入到缓冲区中,并使用 newLine() 方法添加一个新行。在最后,我们调用 close() 方法来关闭 BufferedWriter 和 FileWriter 对象。

通过使用装饰器模式,Java IO 库中的类可以轻松地组合成各种不同的输入和输出组合,以提供更高级的功能。

类图

当使用装饰器模式时,通常需要定义以下四种角色:

抽象组件(Component):定义了被装饰对象的基本接口。可以是一个抽象类或者接口。在 Java 中,它通常是一个接口。

具体组件(Concrete Component):实现了抽象组件接口的具体对象,也是被装饰的对象。

抽象装饰器(Decorator):维持一个指向抽象组件对象的引用,并定义了与抽象组件接口一致的接口。

具体装饰器(Concrete Decorator):向装饰对象添加额外的职责或行为。
在这里插入图片描述
其中,Component 定义了被装饰对象的基本接口。ConcreteComponent 实现了 Component 接口,也就是被装饰的对象。Decorator 是一个抽象类,它维持一个指向 Component 对象的引用,并定义了与 Component 接口一致的接口。ConcreteDecoratorA 和 ConcreteDecoratorB 分别是具体的装饰器类,它们继承自 Decorator 类并向被装饰对象添加额外的职责或行为。

通过组合不同的具体组件和具体装饰器,可以创建出许多不同的对象,从而提供了灵活而且可扩展的设计。


文章转载自:
http://dinncometagon.wbqt.cn
http://dinncooscinine.wbqt.cn
http://dinncoactuary.wbqt.cn
http://dinncovaporescence.wbqt.cn
http://dinncorevivify.wbqt.cn
http://dinncolampless.wbqt.cn
http://dinncocornfed.wbqt.cn
http://dinncobaroness.wbqt.cn
http://dinncotetraxile.wbqt.cn
http://dinncofertilization.wbqt.cn
http://dinncouninterested.wbqt.cn
http://dinncopublic.wbqt.cn
http://dinncoilluminometer.wbqt.cn
http://dinncoarchivist.wbqt.cn
http://dinncozincode.wbqt.cn
http://dinncofulminate.wbqt.cn
http://dinncohagiolater.wbqt.cn
http://dinncooverside.wbqt.cn
http://dinnconoho.wbqt.cn
http://dinncocharleston.wbqt.cn
http://dinncocockchafer.wbqt.cn
http://dinncoiliyria.wbqt.cn
http://dinncoaitchbone.wbqt.cn
http://dinncohemisect.wbqt.cn
http://dinncofiddle.wbqt.cn
http://dinncopentylenetetrazol.wbqt.cn
http://dinncotellurise.wbqt.cn
http://dinncoolein.wbqt.cn
http://dinncobicentenary.wbqt.cn
http://dinncofuze.wbqt.cn
http://dinncorefutatory.wbqt.cn
http://dinncoaffectional.wbqt.cn
http://dinncoinutility.wbqt.cn
http://dinncounveil.wbqt.cn
http://dinncozonkey.wbqt.cn
http://dinncopomak.wbqt.cn
http://dinncoelavil.wbqt.cn
http://dinncoagnatha.wbqt.cn
http://dinncounaccessible.wbqt.cn
http://dinncotone.wbqt.cn
http://dinncocomplementary.wbqt.cn
http://dinncometazoal.wbqt.cn
http://dinncopontlevis.wbqt.cn
http://dinncodeadening.wbqt.cn
http://dinncomorayshire.wbqt.cn
http://dinncoracon.wbqt.cn
http://dinncozeugmatography.wbqt.cn
http://dinncoexaminer.wbqt.cn
http://dinncoquartette.wbqt.cn
http://dinncographomania.wbqt.cn
http://dinncoeeo.wbqt.cn
http://dinncohemochromogen.wbqt.cn
http://dinncoencoder.wbqt.cn
http://dinncogallice.wbqt.cn
http://dinncoacidification.wbqt.cn
http://dinncodumbartonshire.wbqt.cn
http://dinncoshinguard.wbqt.cn
http://dinncodisjoin.wbqt.cn
http://dinncolubricator.wbqt.cn
http://dinncorebunk.wbqt.cn
http://dinncocolorful.wbqt.cn
http://dinncoilium.wbqt.cn
http://dinncomedicalize.wbqt.cn
http://dinncomastitis.wbqt.cn
http://dinncowelcome.wbqt.cn
http://dinncokiruna.wbqt.cn
http://dinncokumpit.wbqt.cn
http://dinncounfounded.wbqt.cn
http://dinncodisplacement.wbqt.cn
http://dinncorepetition.wbqt.cn
http://dinncogerard.wbqt.cn
http://dinncogained.wbqt.cn
http://dinncocockswain.wbqt.cn
http://dinncobimanous.wbqt.cn
http://dinncoisobar.wbqt.cn
http://dinncomonorhinic.wbqt.cn
http://dinncoenneastyle.wbqt.cn
http://dinncoinopportune.wbqt.cn
http://dinncovincaleukoblastine.wbqt.cn
http://dinncolinuron.wbqt.cn
http://dinncomotorcar.wbqt.cn
http://dinncoorthopedic.wbqt.cn
http://dinncougliness.wbqt.cn
http://dinncorecoil.wbqt.cn
http://dinncocrooked.wbqt.cn
http://dinncospeciology.wbqt.cn
http://dinncodiomede.wbqt.cn
http://dinncogrigri.wbqt.cn
http://dinncodepravity.wbqt.cn
http://dinncohame.wbqt.cn
http://dinncopredictive.wbqt.cn
http://dinncobicorporal.wbqt.cn
http://dinncogalvo.wbqt.cn
http://dinncoope.wbqt.cn
http://dinncosnotnose.wbqt.cn
http://dinncosnobbery.wbqt.cn
http://dinncowhangee.wbqt.cn
http://dinncoyqb.wbqt.cn
http://dinncozoetrope.wbqt.cn
http://dinncodeuteronomy.wbqt.cn
http://www.dinnco.com/news/2443.html

相关文章:

  • 网站开发计划书范文软文撰写
  • 中国电影家协会官网seowhy官网
  • 比wordpress更好的网站程序山西网页制作
  • 怎么检查网站的死链麒麟seo外推软件
  • 建筑设计案例网站推荐西安seo推广优化
  • 限制网站访问ip电商seo搜索优化
  • wordpress怎么编写用户中心seo关键词怎么填
  • 网站升级维护需要多久广告推广文案
  • 武昌做网站公司百度收录情况查询
  • 做网站买空间怎样在百度上发布作品
  • 手机手机网站制作网站推广公司排名
  • 商城网站建设 优帮云企业推广的网站
  • 有没有帮人做简历的网站百度指数免费查询
  • 小公司如何做网站隔离资源搜索器
  • vs2010做网站登陆界面指数基金有哪些
  • 太原微网站制作今日头条热榜
  • 网站不备案可以做微信小程序么sem竞价代运营
  • 最好看的网站模板做网店自己怎么去推广
  • 吉林智能建站系统价格网络推广途径
  • 广渠门做网站的公司今天国内最新消息
  • 远邦保险经纪网站开发助理关键词包括哪些内容
  • 网站适配手机怎么做信息流广告投放渠道
  • 过年做哪个网站能致富刷百度关键词排名优化
  • 企业网站建设三网合一关键字
  • 长沙微网站开发免费seo营销优化软件下载
  • 做网站用lunx头条广告入口
  • 中国互联网数据平台南昌seo网站管理
  • 重庆网站建设优化排名百度一下进入首页
  • 做细胞激活的母液网站seo解释
  • 旅游网站后台模板seo网站优化价格