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

怎么做外围网站代理合肥seo招聘

怎么做外围网站代理,合肥seo招聘,四川城乡住房城乡建设厅网站首页,asp网站 证书Decorator装饰模式 前言模式定义动机(Motivation)结构核心思想基础实现1、定义组件接口2、实现具体组件(被修饰对象)3、定义装饰器基类4、实现具体装饰器5、使用示例 模式应用(具体应用)1、定义组件接口(应用)2、实现具体组件(被修饰对象)3、定义装饰器基…

Decorator装饰模式

  • 前言
  • 模式定义
  • 动机(Motivation)
  • 结构
  • 核心思想
  • 基础实现
    • 1、定义组件接口
    • 2、实现具体组件(被修饰对象)
    • 3、定义装饰器基类
    • 4、实现具体装饰器
    • 5、使用示例
  • 模式应用(具体应用)
    • 1、定义组件接口(应用)
    • 2、实现具体组件(被修饰对象)
    • 3、定义装饰器基类
    • 4、实现具体装饰器
    • 5、使用示例
    • 输出结果
  • 要点总结


前言

在软件组件的设计中,如果责任划分的不清楚,使用继承得到的结果往往是随着需求的变化,子类急剧膨胀,同时充斥着重复代码,这时候的关键是划清责任。

模式定义

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

允许通过将对象包装在装饰器类中,动态地为对象添加额外的功能。该模式通过组合而非继承来扩展对象的行为,避免因多重继承导致的类爆炸问题。

动机(Motivation)

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

结构

在这里插入图片描述

核心思想

  • 动态扩展: 运行时为对象添加功能,而非编译时。
  • 透明性: 装饰后的对象与原始对象接口一致,对客户端透明。
  • 组合优于继承: 通过嵌套装饰器实现功能的灵活叠加。

基础实现

1、定义组件接口

#include"component.h"

#pragma once
class Component {//抽象的构件
public:virtual void operate() = 0;
};

2、实现具体组件(被修饰对象)

concretecomponent.h

#pragma once
#include<iostream>
#include"component.h"
class ConcreteComponent :public Component {
public:void operate()override {std::cout << "ConcreteComponent do Something";}
};

3、定义装饰器基类

decorator.h

#pragma once
#include"component.h"class Decorator :public Component {//通过构造函数传递被修饰者
public:Decorator(Component*component) {component_ = component;}//委托给被修饰者执行void operate() override {}
private:Component*component_ = nullptr;
};

4、实现具体装饰器

concretedecorator.h

#pragma once
#include<iostream>
#include"decorator.h"
class ConcreteDecorator1 :public Decorator {
public:ConcreteDecorator1(Component*component) :Decorator(component) {}//重写父类的Operation方法void operate()override {method1();Decorator::operate();}
private://定义自己的修饰方法void method1() {std::cout << "method1 of Decorator1";}
};class ConcreteDecorator2 : public Decorator {
public:ConcreteDecorator2(Component *component): Decorator(component) {}//重写父类的Operation方法void operate() override {method1();Decorator::operate();}
private://定义自己的修饰方法void method1() {std::cout << "method1 of Decorator2";}
};

5、使用示例

#include <iostream>
#include"concretecomponent.h"
#include"concretedecorator.h"int main()
{Component*component = new ConcreteComponent();//第一次修饰component = new ConcreteDecorator1(component);//第二次修饰component = new ConcreteDecorator2(component);//修饰后运行component->operate();
}

模式应用(具体应用)

1、定义组件接口(应用)

component1.h

#pragma once
#include<iostream>
#include<memory>
#include<string>//抽象组件:定义基础功能接口
class Beverage {
public:virtual ~Beverage() = default;virtual std::string getDescription()const = 0;    //获取描述virtual double cost()const = 0;                   //计算价格
};

2、实现具体组件(被修饰对象)

concretecomponent1.h

#pragma once
//具体组件:基础咖啡
#include"component1.h"class Coffee :public Beverage {
public:std::string getDescription()const override {return "coffee";}double cost()const override {return 20.0;}
};

3、定义装饰器基类

decorator1

#pragma once
//装饰器基类:继承自组件接口,并持有组件对象的引用
#include"component1.h"
#include<memory>
class CondimentDecorator :public Beverage {
protected:std::unique_ptr<Beverage> beverage_;     //被装饰的组件public:explicit CondimentDecorator(std::unique_ptr<Beverage>beverage):beverage_(std::move(beverage)){}
};

4、实现具体装饰器

concretedecorator1.h

#pragma once#include"decorator1.h"
//具体装饰器:加糖
class Sugar :public CondimentDecorator {
public:using CondimentDecorator::CondimentDecorator;std::string getDescription()const override {return beverage_->getDescription() + "+ 糖";}double cost() const override {return beverage_->cost() + 2.0;      //糖的价格增加2元}
};//具体装饰器:加牛奶
class Milk :public CondimentDecorator {
public:using CondimentDecorator::CondimentDecorator;std::string getDescription()const override {return beverage_->getDescription() + " + 牛奶";}double cost() const override {return beverage_->cost() + 5.0;     //牛奶增加五元}
};

5、使用示例

#include"concretedecorator1.h"
#include"concretecomponent1.h"
#include"decorator1.h"
#include<memory>int main()
{//创建基类咖啡auto coffee = std::make_unique<Coffee>();std::cout << "订单: " << coffee->getDescription()<< " , 价格: " << coffee->cost() << "元\n";//动态添加装饰:先加糖,再加牛奶std::unique_ptr<Beverage>  decoratedCoffee = std::make_unique<Sugar>(std::move(coffee));decoratedCoffee = std::make_unique<Milk>(std::move(decoratedCoffee));std::cout << "订单: " << decoratedCoffee->getDescription()<< " , 总价: " << decoratedCoffee->cost() << "元\n";
}

输出结果

订单: coffee , 价格: 20元
订单: coffee++ 牛奶 , 总价: 27

要点总结

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

相关文章:

  • 服装手机商城网站建设优化设计六年级下册数学答案
  • 阿里企业邮箱客服电话seo搜索引擎优化人才
  • 美国建网站的价格网络营销seo培训
  • 成都的网站建设开发公司国外网站推广公司
  • 怎么做二维码网站网站制作的基本流程
  • 成都访问公司网站亚马逊提升关键词排名的方法
  • 济南泰安网站建设公司万网域名交易
  • vs2105制作个人网站专业seo网络推广
  • 用符号做照片的网站怎么给自己的公司建立网站
  • 怎么做淘宝 天猫京东网店的网站网络营销平台的主要功能
  • 网站开发作品网络安全培训机构排名
  • 网站购物车设计全网营销平台
  • 自己做的网站用国外的空间怎么样如何做到精准客户推广
  • 网站建设要钱么培训学校招生方案范文
  • 怎样建立网站网站seo诊断报告
  • 福州做网站的公司有哪些今天刚刚发生的新闻
  • 有哪些做推送的网站seo新人怎么发外链
  • 建设网站的企业排行站长工具关键词查询
  • 织梦cms怎么做双语网站北京seo顾问推推蛙
  • 网站做淘宝客还行吗怎么把产品推广到各大平台
  • 电商网站费用网络推广包括哪些
  • 怎样看网站有没有做301app下载注册推广平台
  • 网站建设公司一月赚多少注册安全工程师
  • 电子科技网站建设百度地图人工客服电话
  • wordpress怎么修改css样式长春seo按天计费
  • 做付费视频网站好做app推广去哪找商家
  • php+mysql网站开发全程实例 下载郑州网站seo外包
  • 爱做片视频网站百度问答怎么赚钱
  • 提供企业网站建设价格推广找客户平台
  • 哪个网站可以做公务员题淘宝流量平台