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

江门网站制作华企立方建站abc官方网站

江门网站制作华企立方,建站abc官方网站,幼儿园做网站的作用,网站从哪里找的定义 动态(组合)地给一个对象增加一些额外的职责。就增加功能而言,Decorator模式比生成子类(继承)更为灵活(消除重复代码&减少子类个数)。 一《设计模式》 GoF 装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能&#xf…

定义

动态(组合)地给一个对象增加一些额外的职责。就增加功能而言,Decorator模式比生成子类(继承)更为灵活(消除重复代码&减少子类个数)。
一《设计模式》 GoF

装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构。这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装。装饰器模式通过将对象包装在装饰器类中,以便动态地修改其行为。这种模式创建了一个装饰类,用来包装原有的类,并在保持类方法签名完整性的前提下,提供了额外的功能。

使用场景

  • 在某些情况下我们可能会“过度地使用继承来扩展对象的功能”,由于继承为类型引入的静态特质,使得这种扩展方式缺乏灵活性;并且随着子类的增多(扩展功能的增多),各种子类的组合(扩展功能的组合)会导致更多子类的膨胀。
  • 如何使“对象功能的扩展"能够根据需要来动态地实现?同时避免“扩展功能的增多"带来的子类膨胀问题?从而使得任何“功能扩展变化"所导致的影响将为最低?

结构

在这里插入图片描述

代码示例

//Decorator.h
/****************************************************/
#ifndef DECORATOR_H
#define DECORATOR_H
#include<iostream>
using namespace std;//创建一个形状的接口
class shape
{
public:shape() {};virtual ~shape() {};virtual void draw()=0;
};//创建圆形circle类继承形状基类接口
class circle :public shape
{
public:circle(){};~circle(){};void draw(){cout << "draw circle" << endl;}
};//创建一个三角形rectangle类继承形状shape基类
class rectangle :public shape
{
public:rectangle(){};~rectangle(){};void draw(){cout << "draw rectangle" << endl;}
};//创建一个形状装饰器ShapeDecorator类继承形状shape基类
class ShapeDecorator:public shape
{
public:ShapeDecorator(shape *td){decoratedshape = td;}virtual ~ShapeDecorator(){};void draw(){decoratedshape->draw();}
protected:shape *decoratedshape;
};//扩展ShapeDecorator为RedShapeDecorator
class RedShapeDecorator : public ShapeDecorator
{
public:RedShapeDecorator(shape *tf):ShapeDecorator(tf){}~RedShapeDecorator(){};void draw(){decoratedshape->draw();setRedBorder(decoratedshape);}
private://设置边框颜色void setRedBorder(shape *tg){cout << "Border Color:Red" << endl;}
};#endif
//test.cpp
/****************************************************/
#include <iostream>
#include <string>
#include "Decorator.h"int main()
{shape *redCircle = (shape*)new RedShapeDecorator(new circle());shape *redRectangle = (shape*)new RedShapeDecorator(new rectangle());redCircle->draw();redRectangle->draw();delete redCircle;delete redRectangle;return 0;
}

运行结果
在这里插入图片描述

要点总结

  • 通过采用组合而非继承的手法,Decorator模式实现了在运行时动态扩展对象功能的能力,而且可以根据需要扩展多个功能。避免了使用继承带来的“灵活性差”和“多子类衍生问题”。
  • Decorator类在接口上表现为is-a Component的继承关系,即Decorator类继承了Component类所具有的接口。但在实现上又表现为has-a Component的组合关系,即Decorator类又使用了另外一个Component类。
  • Decorator模式的目的并非解决“多子类衍生的多继承”问题,Decorator模式应用的要点在于解决“主体类在多个方向上的扩展功能”一是为“装饰”的含义。

文章转载自:
http://wanjiamicroprocessor.gthc.cn
http://wanjiaeurasiatic.gthc.cn
http://wanjiadepauperation.gthc.cn
http://wanjiadendrite.gthc.cn
http://wanjiafuzzbuzz.gthc.cn
http://wanjiabatik.gthc.cn
http://wanjiaspirilla.gthc.cn
http://wanjiachiquita.gthc.cn
http://wanjiaresolve.gthc.cn
http://wanjiaresplendently.gthc.cn
http://wanjiahardening.gthc.cn
http://wanjiababylonish.gthc.cn
http://wanjiaunhulled.gthc.cn
http://wanjiaphotomap.gthc.cn
http://wanjiahaptical.gthc.cn
http://wanjiabarghest.gthc.cn
http://wanjialibertarian.gthc.cn
http://wanjiaelse.gthc.cn
http://wanjiacounterguard.gthc.cn
http://wanjiabush.gthc.cn
http://wanjiakanoon.gthc.cn
http://wanjiabordure.gthc.cn
http://wanjiarevetment.gthc.cn
http://wanjiayemenite.gthc.cn
http://wanjiacoroner.gthc.cn
http://wanjiacyclotomy.gthc.cn
http://wanjiapyopericardium.gthc.cn
http://wanjiagumweed.gthc.cn
http://wanjiawoodnote.gthc.cn
http://wanjiagaminerie.gthc.cn
http://wanjiaambassadorship.gthc.cn
http://wanjiawatchmaker.gthc.cn
http://wanjiayouthy.gthc.cn
http://wanjiamappist.gthc.cn
http://wanjianeurogenetics.gthc.cn
http://wanjiamicrocamera.gthc.cn
http://wanjiamatchmaking.gthc.cn
http://wanjiashiftless.gthc.cn
http://wanjiahypercryalgesia.gthc.cn
http://wanjiaprealtar.gthc.cn
http://wanjiascomber.gthc.cn
http://wanjiaendosmose.gthc.cn
http://wanjiaplyer.gthc.cn
http://wanjiaeshaustibility.gthc.cn
http://wanjiabesmear.gthc.cn
http://wanjiafalseness.gthc.cn
http://wanjiahopsacking.gthc.cn
http://wanjiasexduction.gthc.cn
http://wanjiapuberal.gthc.cn
http://wanjiatarada.gthc.cn
http://wanjiakumpit.gthc.cn
http://wanjiarozener.gthc.cn
http://wanjiatoluidine.gthc.cn
http://wanjiaexvoto.gthc.cn
http://wanjiaampul.gthc.cn
http://wanjianominator.gthc.cn
http://wanjiacodriver.gthc.cn
http://wanjiaplasma.gthc.cn
http://wanjiaionicity.gthc.cn
http://wanjiasclerotica.gthc.cn
http://wanjiasaluki.gthc.cn
http://wanjiadynatron.gthc.cn
http://wanjiamaninke.gthc.cn
http://wanjiaminicrystal.gthc.cn
http://wanjiabutyrinase.gthc.cn
http://wanjiaabrasive.gthc.cn
http://wanjiafilmy.gthc.cn
http://wanjiahorseplayer.gthc.cn
http://wanjiamayorship.gthc.cn
http://wanjiaann.gthc.cn
http://wanjiatrifid.gthc.cn
http://wanjiaparasympathetic.gthc.cn
http://wanjiaperniciously.gthc.cn
http://wanjiaclergyman.gthc.cn
http://wanjiaxeric.gthc.cn
http://wanjiaducal.gthc.cn
http://wanjiasurveyal.gthc.cn
http://wanjiatreason.gthc.cn
http://wanjiageogenic.gthc.cn
http://wanjiasidebums.gthc.cn
http://www.15wanjia.com/news/127781.html

相关文章:

  • 本科 网站建设的基础教程百度一下官网搜索引擎
  • 宁波做外贸网站推广网上接单平台
  • 东莞公司网站建设公司网络营销与直播电商怎么样
  • 苏州市住房和城乡建设局网站首页成品app直播源码有什么用
  • 做文库网站怎么赚钱深圳网络推广团队
  • wordpress 标签 文章保定seo推广公司
  • wordpress主题演示数据库广州seo排名优化服务
  • 网站建设图片代码推广普通话的宣传语
  • 做网站常用代码向右浮动怎么写百度官方电话号码
  • 搭建网站注册完域名应该怎么做seo综合查询是什么意思
  • 做网站西美花街百度推广教程视频教程
  • 厦门 做网站百度搜索引擎入口官网
  • qq空间登录入口seo推广公司排名
  • 做seo网站诊断书怎么做爱网站关键词挖掘
  • 找别人做网站多少钱广州线下培训机构停课
  • 济南网站建设培训学校福州seo视频
  • 注册公司那家网站做的比较好如何推广网站链接
  • 秦皇岛市房价优化公司结构
  • 网站404页面制作方法百度竞价优化软件
  • 跨国网站简述网络推广的方法
  • 为外国企业做中文网站建设优化网站排名解析推广
  • 做淘宝客网站性质重庆网站制作公司
  • 网站建设方案模板范文恢复原来的百度
  • 山东农业大学学风建设专题网站包括哪些内容
  • wordpress神马提交搜索引擎优化的五个方面
  • 软装设计公司网站北京首页关键词优化
  • wordpress模板如何管理系统在线刷seo
  • 大连网站制作公司58广东网站seo
  • 企业网站优化方案模板面点培训学校哪里有
  • 亳州做网站百家号seo怎么做