介绍
意图:动态地给一个对象添加一些额外的职责。就增加功能来说,装饰模式相比生成子类更灵活。
主要解决:我们扩展一个类常使用继承方式实现,由于继承为类引入静态特征,并且随着扩展功能的增多,子类会越来越膨胀。
如何使用:在不想增加很多子类的情况下扩展。
如何解决:将具体功能职责划分,同时继承装饰者模式。
关键代码:
1. Component 类充当抽象角色,不应该具体实现。
2. 修饰类引用合继承 Component 类,具体扩展类重写父类方法。
使用场景:
1. 扩展一个类的功能。
2. 动态增加功能,动态撤销。
实现
创建一个 Shape 接口合实现了 Shape 接口的实体类。然后再创建一个实现了 Shape 接口的抽象装饰类 ShapeDecorator,并把 Shape 对象作为它的实例变量。RedShapeDecorator 是实现了 ShapeDecorator 的实体类。DecoratorPatternDemo 类使用 RedShapeDecorator 来装饰 Shape 对象。
步骤1:
public interface Shape{ void draw(); }
步骤2:
public class Rectangle implements Shape{ @Override public ovid draw(){ System.out.println("draw Rectangle"); } }
public class Cricle implements Shape{ @Override public ovid draw(){ System.out.println("draw Circle"); } }
步骤3:
public abstract class ShapeDecorator implements Shape{ private Shape shape; // 持有一个 Shape 对象 public ShapeDecorator(Shape shape){ this.shape = shape; } public void draw(){ shape.draw();// TODO 根据传进来的具体 Shape 对象,调用对应的 draw 方法 } }
步骤4:
public class RedShapeDecorator extends ShapeDecorator{ public RedShapeDecorator(Shape shape){ super(shape); } @Override public void draw(){ shape.draw(); setRedBorder(shape); } public void setRedBorder(Shape shape){ System.out.println("Border Color: Red"); } }
步骤5
public class DecoratorPatterndDemo{ public static void main(String args[]){ //TODO 面向抽象层编程 // 普通的Circle Shape circle = new Circle(); System.out.println("Circle with normal Border"); circle.darw(); // 红色边界的 Circle ShapeDecorator redCircle = new RedShapeDecorator(new Circle); System.out.println("Circle of red Border"); redCircle.draw(); // 红色边界的 Rectangle ShapeDecorator redRectangle = new RedShapeDecorator(new Rectangel): System.out.println("Rectangle of red Border"); redRectangle.draw(); } }
优缺点
优点:装饰类和被装饰类可以独立发展,不会相互耦合,装饰模式是继承的一个替代模式,装饰模式可以动态扩展一个实现类的功能。
缺点:多层装饰比较复杂。
以上是Java结构型设计模式之装饰模式怎么实现的详细内容。更多信息请关注PHP中文网其他相关文章!

JavaachievesPlatFormIndependencethroughTheJavavIrtualMachine(JVM),允许Codetorunondifferentoperatingsystemsswithoutmodification.thejvmcompilesjavacodeintoplatform-interploplatform-interpectentbybyteentbytybyteentbybytecode,whatittheninternterninterpretsandectectececutesoneonthepecificos,atrafficteyos,Afferctinginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginging

JavaispoperfulduetoitsplatFormitiondence,对象与偏见,RichstandardLibrary,PerformanceCapabilities和StrongsecurityFeatures.1)Platform-dimplighandependectionceallowsenceallowsenceallowsenceallowsencationSapplicationStornanyDevicesupportingJava.2)

Java的顶级功能包括:1)面向对象编程,支持多态性,提升代码的灵活性和可维护性;2)异常处理机制,通过try-catch-finally块提高代码的鲁棒性;3)垃圾回收,简化内存管理;4)泛型,增强类型安全性;5)ambda表达式和函数式编程,使代码更简洁和表达性强;6)丰富的标准库,提供优化过的数据结构和算法。

javaisnotirelyPlatemententduetojvmvariationsandnativecodinteintration,butitlargelyupholdsitsitsworapromise.1)javacompilestobytecoderunbythejvm

thejavavirtualmachine(JVM)IsanabtractComputingmachinecrucialforjavaexecutionasitrunsjavabytecode,使“ writeononce,runanywhere”能力

Javaremainsagoodlanguageduetoitscontinuousevolutionandrobustecosystem.1)Lambdaexpressionsenhancecodereadabilityandenablefunctionalprogramming.2)Streamsallowforefficientdataprocessing,particularlywithlargedatasets.3)ThemodularsystemintroducedinJava9im

Javaisgreatduetoitsplatformindependence,robustOOPsupport,extensivelibraries,andstrongcommunity.1)PlatformindependenceviaJVMallowscodetorunonvariousplatforms.2)OOPfeatureslikeencapsulation,inheritance,andpolymorphismenablemodularandscalablecode.3)Rich

Java的五大特色是多态性、Lambda表达式、StreamsAPI、泛型和异常处理。1.多态性让不同类的对象可以作为共同基类的对象使用。2.Lambda表达式使代码更简洁,特别适合处理集合和流。3.StreamsAPI高效处理大数据集,支持声明式操作。4.泛型提供类型安全和重用性,编译时捕获类型错误。5.异常处理帮助优雅处理错误,编写可靠软件。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

Dreamweaver CS6
视觉化网页开发工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)