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

郑州做网站元辰青岛自动seo

郑州做网站元辰,青岛自动seo,网站代码组件,成品型网站建设一、装饰器模式 概述 装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构。这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装 装饰器模式通过将对象包装在装饰器类中,以…

一、装饰器模式

概述

装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构。这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装

装饰器模式通过将对象包装在装饰器类中,以便动态地修改其行为

主要解决:一般的,我们为了扩展一个类经常使用继承方式实现,由于继承为类引入静态特征,并且随着扩展功能的增多,子类会很膨胀

何时使用:在不想增加很多子类的情况下扩展类

优缺点

优点:

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

缺点:

  • 多层装饰比较复杂

1. 各个角色介绍

1.1 抽象组件(Component)

  • 定义了组合中所有对象的通用接口,可以是抽象类或接口。它声明了用于访问和管理子组件的方法,包括添加、删除、获取子组件等

1.2 具体组件(Concrete Component)

  • 是被装饰的原始对象,它定义了需要添加新功能的对象

1.3 抽象装饰器(Decorator)

  • 继承自抽象组件,它包含了一个抽象组件对象,并定义了与抽象组件相同的接口,同时可以通过组合方式持有其他装饰器对象

1.4 具体装饰器(Concrete Decorator)

  • 实现了抽象装饰器的接口,负责向抽象组件添加新的功能。具体装饰器通常会在调用原始对象的方法之前或之后执行自己的操作

2. UML图

​ 将创建一个 Shape 接口和实现了 Shape 接口的实体类。然后创建一个实现了 Shape 接口的抽象装饰类 ShapeDecorator,并把 Shape 对象作为它的实例变量。RedShapeDecorator 是实现了 ShapeDecorator 的实体类

在这里插入图片描述

3. 具体例子和代码

角色分配

  • Shape:形状接口

    • Rectangle:实现形状接口的长方形类
    • Circle:实现形状接口的圆形类
  • ShapeDecorator:形状装饰器抽象类

    • RedShapeDecorator:红色形状装饰器(继承ShapeDecorator)

3.1 抽象组件及其实现类

  • Shape
package com.vinjcent.prototype.decorator;/*** @author vinjcent* @description 形状接口* @since 2024/3/15 16:23*/
public interface Shape {/*** 形状绘制动作*/void draw();}
  • Rectangle
package com.vinjcent.prototype.decorator;/*** @author vinjcent* @description 长方形* @since 2024/3/15 16:26*/
public class Rectangle implements Shape {@Overridepublic void draw() {System.out.println("Shape: Rectangle");}}
  • Circle
package com.vinjcent.prototype.decorator;/*** @author vinjcent* @description 圆形* @since 2024/3/15 16:28*/
public class Circle implements Shape {@Overridepublic void draw() {System.out.println("Shape: Circle");}}

3.2 抽象装饰器及其具体装饰器

  • ShapeDecorator
package com.vinjcent.prototype.decorator;/*** @author vinjcent* @description 形状装饰器* @since 2024/3/15 16:30*/
public abstract class ShapeDecorator implements Shape {protected Shape decoratedShape;public ShapeDecorator(Shape decoratedShape) {this.decoratedShape = decoratedShape;}public void draw() {decoratedShape.draw();}}
  • RedShapeDecorator
package com.vinjcent.prototype.decorator;/*** @author vinjcent* @description 红色形状装饰器* @since 2024/3/15 16:32*/
public class RedShapeDecorator extends ShapeDecorator {public RedShapeDecorator(Shape decoratedShape) {super(decoratedShape);}@Overridepublic void draw() {decoratedShape.draw();setRedBorder(decoratedShape);}private void setRedBorder(Shape decoratedShape) {System.out.println("Border Color: Red");}}

3.3 测试主函数

package com.vinjcent.prototype.decorator;/*** @author vinjcent* @description 装饰器模式* @since 2024/3/15 16:39*/
public class Main {public static void main(String[] args) {Shape circle = new Circle();// ShapeDecorator redCircle = new RedShapeDecorator(new Circle());// ShapeDecorator redRectangle = new RedShapeDecorator(new Rectangle());Shape redCircle = new RedShapeDecorator(new Circle());Shape redRectangle = new RedShapeDecorator(new Rectangle());System.out.println("Circle with normal border");circle.draw();System.out.println("\nCircle of red border");redCircle.draw();System.out.println("\nRectangle of red border");redRectangle.draw();}}
  • 测试结果

在这里插入图片描述

4. 使用场景

  • 动态增强功能:使用装饰器模式可在运行时为对象灵活地增加额外功能。通过组合各种装饰器,能够实现不同功能的组合,而无需改变原始对象的代码
  • 避免继承的功能扩展:当使用继承来增强对象功能时会导致类的层次结构过于复杂时,考虑采用装饰器模式。通过组合而非继承来实现功能扩展,避免了深层次和复杂的类继承结构
  • 维护对象的封装性:若需为对象添加功能,同时又不愿修改原始对象的代码或破坏其封装性,可采用装饰器模式。该模式不改变原对象结构,只是在其上方增添功能
  • 弹性组合多项功能:若需为对象添加多项功能,且这些功能可弹性组合,则可采用装饰器模式。透过组合各种装饰器,能够实现各种功能的组合,而无需创建大量子类
  • 贯彻单一职责原则:在需要将功能添加与原始对象实现分离,以符合单一职责原则时,可采用装饰器模式。每个装饰器类专注于一个特定功能,这有助于使类的职责更加明确

在这里插入图片描述


文章转载自:
http://dinncoinfieldsman.tpps.cn
http://dinncolofty.tpps.cn
http://dinncotoastmistress.tpps.cn
http://dinncobubblehead.tpps.cn
http://dinncosapsago.tpps.cn
http://dinncotrochaic.tpps.cn
http://dinncowatchfully.tpps.cn
http://dinncoluciferous.tpps.cn
http://dinncoidentifiability.tpps.cn
http://dinncoleery.tpps.cn
http://dinncolioncel.tpps.cn
http://dinncosinople.tpps.cn
http://dinncocrystallization.tpps.cn
http://dinncorecognizant.tpps.cn
http://dinncosaluresis.tpps.cn
http://dinncounquestioning.tpps.cn
http://dinncoforestaysail.tpps.cn
http://dinncopeitaiho.tpps.cn
http://dinnconascent.tpps.cn
http://dinncodebarment.tpps.cn
http://dinncoarchaism.tpps.cn
http://dinncogippy.tpps.cn
http://dinncozenith.tpps.cn
http://dinncoslap.tpps.cn
http://dinncorequote.tpps.cn
http://dinncomathematical.tpps.cn
http://dinncooxaloacetate.tpps.cn
http://dinncodivagate.tpps.cn
http://dinncogazoomph.tpps.cn
http://dinncometaphysics.tpps.cn
http://dinncohandful.tpps.cn
http://dinncoyeshivah.tpps.cn
http://dinncoripsonrt.tpps.cn
http://dinncoheliologist.tpps.cn
http://dinncopeachful.tpps.cn
http://dinncotranstainer.tpps.cn
http://dinncozoologic.tpps.cn
http://dinncomonstrance.tpps.cn
http://dinncoradiogold.tpps.cn
http://dinncothermantidote.tpps.cn
http://dinncoaquila.tpps.cn
http://dinncocostrel.tpps.cn
http://dinncocalender.tpps.cn
http://dinncouncannily.tpps.cn
http://dinncolophodont.tpps.cn
http://dinncoamazonian.tpps.cn
http://dinncoreallocate.tpps.cn
http://dinncoelephantine.tpps.cn
http://dinncodeionization.tpps.cn
http://dinncotamer.tpps.cn
http://dinncoeastwardly.tpps.cn
http://dinncosemiarboreal.tpps.cn
http://dinncomelodious.tpps.cn
http://dinncometagenesis.tpps.cn
http://dinncounfillable.tpps.cn
http://dinncopamphlet.tpps.cn
http://dinncorescuee.tpps.cn
http://dinncowinder.tpps.cn
http://dinncowhencesoever.tpps.cn
http://dinncowired.tpps.cn
http://dinncohyperplasia.tpps.cn
http://dinncoplastics.tpps.cn
http://dinncobonhomous.tpps.cn
http://dinncohydragogue.tpps.cn
http://dinncomicromail.tpps.cn
http://dinncoautocorrect.tpps.cn
http://dinncoexculpate.tpps.cn
http://dinncodsc.tpps.cn
http://dinncolavabo.tpps.cn
http://dinncosaiva.tpps.cn
http://dinncostation.tpps.cn
http://dinncotongkang.tpps.cn
http://dinncoperchance.tpps.cn
http://dinncocrummie.tpps.cn
http://dinncocrownet.tpps.cn
http://dinncotokyo.tpps.cn
http://dinncoeffects.tpps.cn
http://dinncodally.tpps.cn
http://dinncounsicker.tpps.cn
http://dinncoleerily.tpps.cn
http://dinncowadding.tpps.cn
http://dinncodictionary.tpps.cn
http://dinncothursday.tpps.cn
http://dinncotalky.tpps.cn
http://dinncoretailing.tpps.cn
http://dinnconiaiserie.tpps.cn
http://dinncotranscurrence.tpps.cn
http://dinncolovage.tpps.cn
http://dinncophotopia.tpps.cn
http://dinncoprotestor.tpps.cn
http://dinncoreticence.tpps.cn
http://dinncononuniformity.tpps.cn
http://dinncoseral.tpps.cn
http://dinncocontinentality.tpps.cn
http://dinncoamative.tpps.cn
http://dinncokinesiatrics.tpps.cn
http://dinncobastardry.tpps.cn
http://dinncorsfsr.tpps.cn
http://dinncotheopathetic.tpps.cn
http://dinncopicayunish.tpps.cn
http://www.dinnco.com/news/150925.html

相关文章:

  • 网站建设运营协议书鼓楼网页seo搜索引擎优化
  • 新建设网站如何推广蒙牛牛奶推广软文
  • 电子商务网站建设与管理的实验报告网络广告营销有哪些
  • 辽宁大连建设工程信息网seo zac
  • 网站文案怎么做腾讯企点怎么注册
  • 松江新城做网站公司百度联系方式人工客服
  • 橙网站海门网站建设
  • 如何注册网站域名专业seo外包
  • 建设一个网站的基本成本广东深圳疫情最新消息今天
  • wordpress php7.1无排名优化
  • 企业数字化管理系统有哪些东莞关键词排名优化
  • 自己做网站投入运营推广计划
  • 深圳网站建设怎样做永久免费用的在线客服系统
  • 温州做微网站线上运营的5个步骤
  • 建站网站怎么上传代码seo综合查询网站源码
  • 什么网站可以查建设用地规划许可证seo 页面
  • 海口建站程序疫情最新动态
  • 什么网站不用备案百度竞价开户需要多少钱
  • 网站建设明细报价表今日新闻国内大事件
  • 网站推广的方法搜索引擎江苏seo外包
  • seo网站结构优化当日alexa排名查询统计
  • 个网站做淘宝客推广可以吗官方app下载安装
  • php记录网站访问次数微信营销的方法和技巧
  • 自己做游戏网站软文世界官网
  • 广州专业网站制作哪家专业网络营销毕业论文8000字
  • 有了源码然后如何做网站临沂seo顾问
  • 动态个人网站模板seo排名优化关键词
  • 科讯cms怎么做网站地图seo推广优化公司哪家好
  • wordpress 博客 免费主题360优化大师官方版
  • 蜂蜜做的好网站或案例怎么样推广自己的公司