装饰者模式

设计模式——装饰模式 装饰模式是一种用来替代继承的思想,它使用对象之间的关联关系来取代类之间的继承关系。继承是一种常用的一种拓展原有类的功能的方法,通过创建一个继承父类的子类来拓展原有的功能,在“合成复用原则”中曾提到,在处理类的功能拓展这个问题上应尽量使用类之间的关联关系而少使用继承关系。为什么要用这个模式:以对客户透明的方式动态地给一个对象附加上更多的责任,装饰模式可以在不需要创造更多的子类的情况下,将对象的功能加... 阅读详情

1、uml类图

在这里插入图片描述

2、两个对象

主体:被装饰的对象(打包)
包装:包装核心类,即各个在类中继承Decorator的调料类

3、代码

//包装核心类(包装)

package test.decorator;

/**
 * 包装类
 */
public class Decorator extends Drink{
    private Drink drink;

    public Decorator(Drink drink){
        this.drink = drink;
    }

    //计算总金额
    @Override
    public float cost() {
        return super.getPrice() + drink.cost();
    }

    //打印被装饰者的信息
    @Override
    public String getDes(){
        return des + " " + getPrice() + " && " + drink.getDes();
    }
}

包装类子类

public class Chocolate extends Decorator{
    public Chocolate(Drink drink) {
        super(drink);
        setDes("巧克力");
        setPrice(0.2f);
    }
}
public class Soy extends Decorator{
    public Soy(Drink drink) {
        super(drink);
        setDes("豆浆");
        setPrice(0.1f);
    }
}

被包装类

//缓冲
public class Coffee extends Drink{
    @Override
    public float cost() {
        return super.getPrice();
    }
}
public class AmericanCoffee extends Coffee{
    public AmericanCoffee(){
        setDes("AmericanCoffee");
        setPrice(0.6f);
    }
}
public class EnglishCoffee extends Coffee{
    public EnglishCoffee(){
        setDes("EnglishCoffee");
        setPrice(0.4f);
    }
}

运行代码

public class RunMain {
    public static void main(String[] args) {
        //1、要一份加了巧克力的英式咖啡
        Drink drink = new EnglishCoffee();
        System.out.println("价格----"+drink.cost());
        System.out.println("名称----"+drink.getDes());

        //2、要巧克力
        drink = new Chocolate(drink);
        System.out.println("价格----"+drink.cost());
        System.out.println("名称----"+drink.getDes());

        //3、加豆浆
        drink = new Soy(drink);
        System.out.println("价格----"+drink.cost());
        System.out.println("名称----"+drink.getDes());
    }
}

运行结果
由于没有使用BigDecimal进行精度运算,可能存在精度问题。
在这里插入图片描述

4、装饰者模式的好处

1)动态的将新功能附加到对象上。在对象功能扩展方面,它比继承更有弹性,装饰者模式也体现了开闭原则(ocp)

如果需要新增功能,只需要新增一个对象继承缓冲层对象即可,照样能使用包装类对改对象进行包装

人大金仓数据库Kingbase FlySync同步程序安装 Kingbase FlySync同步程序(以下简称 KFS)是面向异地容灾、数据集中共享的场景推出的具备在异构数据平台间实现实时、增量数据同步的工具类产品,主要采用了物理日志解析技术,帮助用户解决不同数据平台的数据间可实现任意方向实时移动流转的问题,并保证此过程中数据不丢失,状态可监控,流转数据量可统计。KFS 需要安装的主要组件有 2 个,分别是后端同步程序和管理控制平台,另外KFS的部署方式也有2种,为分离部署何集中部署•集中部署:KFS与数据库部署在同一机器上。 阅读详情

相关推荐

从零开始掌握RDMA拥塞控制:原理、算法与实战解析

本文深入解析了RDMA拥塞控制的核心原理与实战配置。RDMA依赖无损网络保障高性能,其关键在于PFC与ECN两大技术协同防止丢包。文章重点剖析了主流算法DCQCN的工作机制与参数调优,并简要介绍了HPCC与TIMELY等前沿算法。最后提供了在Linux上配置RoCEv2与DCQCN的详细步骤及性能调试方法,帮助读者从零构建稳定的RDMA网络环境。

weixin_28452161的博客 137

装饰者模式(decorator)完全解析

先看一下装饰者模式的UML图: 装饰模式到底是什么呢?举个简单的例子来说明,现在有一杯白开水(WaterClass),想向白开水里面加茶叶(TeaClass),加点糖(SugarClass),或者加点盐(SaltClass)。也许会认为那还不简单吗,加茶叶的白开水就再生成一个(WaterWithTeaClass),加糖的白开水就再生成一个(WaterWithSugarClass)这样不就行了

有图有真相 2953

PCA9548实战:从硬件连接到Linux驱动的I2C扩展全解析

本文详细解析了PCA9548 I2C多路复用器从硬件连接到Linux驱动配置的全过程。涵盖硬件引脚连接、电源去耦、地址选择等关键设计要点,以及Linux驱动加载、设备树配置和应用层编程技巧,帮助开发者高效实现I2C总线扩展。

weixin_28726391的博客 412

设计模式 —— 装饰者模式

目录 一、星巴克咖啡订单项目需求 二、方案1-解决星巴克咖啡订单项目(较差) 三、方案2-解决星巴克咖啡订单(好点) 四、装饰者模式定义 4.1 装饰者模式原理 4.2 装饰者模式解决星巴克咖啡订单 五、装饰者模式在 JDK 应用的源码分析 装饰者模式定义: 动态给一个对象添加一些额外的职责,就像在墙上刷油漆,使用Decorator 模式相比用生成子类方式达到功能的扩充显得更为灵活。 初计初衷: 通常可以使用继承来实现功能的扩展,如果这些需要扩展的功能的种类很繁多,那么势必...

认知 行动 坚持 6790

HC-SR04测距不准?STM32F103定时器捕获模式详解与精度提升实战

本文详细解析了HC-SR04超声波测距模块在STM32F103上的精度问题,并提供了定时器输入捕获模式的实战配置方法。通过硬件级时间测量和温度补偿算法,将测距精度提升至±0.1cm,适用于工业级应用场景。文章包含完整的代码实现和测试对比数据,帮助开发者解决超声波测距不准的常见问题。

weixin_30313409的博客 351

腾讯云服务器被攻击,带宽被占满的抗战日记

故事 时间:2021-08-23 地点:腾讯云服务器 事件:服务器内容被黑,修复后某人气不过,开始DDOS,占满服务器的带宽,导致网站访问非常缓慢。 杂记 根据腾讯云后台+宝塔后台,带宽被占满:(小垃圾水管没办法) 部分参考:记一次阿里云服务器ECS流出带宽占满的解决过程 https://blog.youkuaiyun.com/wangzhihao1994/article/details/99563456 首先是在云主机上使用远程连接访问到云主机,安装iftop工具 CentOS .

Rudon滨海渔村的博客 1304

什么是装饰者模式?

一、 什么是装饰者模式 装饰者模式又称包装模式,是指不改变原有对象的基础上,将功能附加到对象上,提供了比继承更有弹性的替代方案.属于结构型设计模式. 二、装饰者模式uml图 装饰器其中包含4个角色: 抽象组件(Component): 可以是一个接口或者抽象类,规定了被装饰者的行为. 具体组件(ConcreteComponent): 实现或继承(component)的一个具体对象.也即被装饰者....

weixin_40980639的博客 2369

LT9211 单路mipi转双路mipi芯片,方案成熟,提供技术支持

LT9211 单路mipi转双路mipi芯片

TEL15012949045的博客 5026

装饰模式

装饰模式 1.介绍 装饰模式,也称为包装模式,结构型设计模式之一,其使用一种对客户端透明的方式来动态地拓展对象的功能,同时它也是继承关系的一种替代方案之一,在现实生活中你也可以看见很多装饰模式的例子。从人来说,人需要各式各样的一桌,不管穿着什么样,但是对于个人的本质来说是不变的,充其量只是在外面披上一层衣物而已,这就是装饰模式,装饰物也许各不相同但是装饰的对象本质是不变的。 2.定义 动态地给一个...

祁门路搅屎王的博客 1478

STM32实战:如何用GPS信号同时同步海康摄像头和Livox Mid-360雷达(附完整接线图)

本文详细介绍了如何利用STM32实现GPS信号分频,同步海康摄像头和Livox Mid-360雷达的时间同步方案。通过硬件架构设计、STM32信号处理核心实现及设备接口规范,解决了多传感器数据融合中的时间对齐难题,适用于自动驾驶和机器人导航领域。

weixin_29182481的博客 596

【fpdlink显示】DS90UB948关于941连接948 GPIO调试问题

1. 前言 使用一个941连接2 948显示: 941的GPIO0/1/2/3到前948的GPIO0/1/2/3和941的D_GPIO0/1/2/3到948的GPIO0/1/2/3。 设置gpio3输出,则941的D_GPIO3设置高,而948的gpio3设置不高; 但设置gpio3输入后,看到941的D_GPIO3跟随948的gpio3变化。 2. 原理图连接 3. 测试代码 static int du90ub941_enable_port(struct goodix_ts_d

jzwjzw19900922的博客 2558

java设计模式——装饰者模式

目录一、装饰者模式的定义与类型二、装饰者模式的适用场景三、装饰者模式的优点四、装饰者模式的缺点五、装饰者模式相关的设计模式六、普通示例:七、普通示例演进,使用装饰者模式示例: 一、装饰者模式的定义与类型 1、定义 在不改变原有对象的基础之上,将功能附加到对象上 提供了比继承更具有弹性的替代方法(扩展原有对象功能) 2、类型 结构型 二、装饰者模式的适用场景 扩展一个类的功能或给一个类添加附加职责 动态的给一个对象添加功能,这些功能可以在动态的撤销 三、装饰者模式的优点 继承的有力补充,比继承灵

小志的博客 1049

【AI智能体】Claude Code 核心记忆文件 CLAUDE.md实战操作详解

Claude Code 核心记忆文件 CLAUDE.md实战操作详解

congge 5459

理解装饰者模式

装饰者模式的书面定义是:“动态地给一个对象添加一些额外的职责。就增加功能来说,装饰模式相比生成子类更为灵活。”——语出《Android源码设计模式解析与实战》。

hmh0512的专栏 1576

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

装饰模式和python装饰器

​###装饰器和装饰模式 先给出两者的定义: - 装饰器:装饰器是一个很著名的设计模式,经常被用于有切面需求的场景,较为经典的有插入日志、性能测试、事务处理等。装饰器是解决这类问题的绝佳设计,有了装饰器,我们就可以抽离出大量函数中与函数功能本身无关的雷同代码并继续重用。概括的讲,装饰器的作用就是为已经存在的对象添加额外的功能。 - 装饰模式:在不必改变原类文件和使用继承的情况下,动态地扩展一个

Mislead的专栏 1913

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

上一篇: 桥接模式
下一篇: Activiti7学习笔记———activiti基本介绍及使用
magicproblem
博客等级 码龄7年 22粉丝 59原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值