Java设计模式之装饰者模式

本文详细介绍了装饰模式的概念及其特点,包括装饰对象与真实对象接口的一致性、请求转发机制及附加功能的实现方式。通过具体的Java代码示例展示了如何在不改变对象结构的前提下为其添加新的功能。

装饰模式的特点:
(1) 装饰对象和真实对象有相同的接口。这样客户端对象就能以和真实对象相同的方式和装饰对象交互。
(2) 装饰对象包含一个真实对象的引用(reference)
(3) 装饰对象接受所有来自客户端的请求。它把这些请求转发给真实的对象。
(4) 装饰对象可以在转发这些请求以前或以后增加一些附加功能。这样就确保了在运行时,不用修改给定对象的结构就可以在外部增加附加的功能。在面向对象的设计中,通常是通过继承来实现对给定类的功能扩展。

在装饰模式中的各个角色有:
  (1)抽象构件(Component)角色:给出一个抽象接口,以规范准备接收附加责任的对象。
  (2)具体构件(Concrete Component)角色:定义一个将要接收附加责任的类。
  (3)装饰(Decorator)角色:持有一个构件(Component)对象的实例,并实现一个与抽象构件接口一致的接口。
  (4)具体装饰(Concrete Decorator)角色:负责给构 件对象添加上附加的责任。
  这里写图片描述
 
Component->Human

public interface Human {
     public void wear();
}

Concrete Component->Man

public class Man implements Human {

    @Override
    public void wear() {
        System.out.println("给我穿各种颜色的衣服");
    }
}

Decorator ->Decorator

public class Decorator implements Human {

    private Human human;

    public Decorator(Human human) {
        this.human = human;
    }
    @Override
    public void wear() {
        human.wear();
    }
}

Concrete Decorator->One/Two/Three

public class DecoratorOne extends Decorator {

    public DecoratorOne(Human human) {
        super(human);
    }
    public void wear() {
        super.wear();
        System.out.println("红色衣服");
    }
}
public class DecoratorTwo extends Decorator {

    public DecoratorTwo(Human human) {
        super(human);
    }
    public void wear() {
        super.wear();
        System.out.println("绿色衣服");
    }
}
public class DecoratorThree extends Decorator {
    public DecoratorThree(Human human) {
        super(human);
    }
    public void wear() {
        super.wear();
        System.out.println("蓝色衣服");
    }
}

测试类->Test

public class Test {
    public static void main(String[] args) {
//      Human huge=new Man();
//      Human one=new DecoratorOne(huge);
//      Human two=new DecoratorTwo(one);
//      Human three=new DecoratorThree(two);
        Human three=new DecoratorThree(new
                DecoratorTwo(new DecoratorOne(new Man())));

        three.wear();
    }
}

控制台输出:

给我穿各种颜色的衣服
红色衣服
绿色衣服
蓝色衣服

参考链接:
http://www.cnblogs.com/java-my-life/archive/2012/04/20/2455726.html
http://blog.youkuaiyun.com/jason0539/article/details/22713711

【CNN-GRU-Attention】基于卷积神经网络和门控循环单元网络结合注意力机制的多变量回归预测研究(Matlab代码实现)内容概要:本文介绍了基于卷积神经网络(CNN)、门控循环单元网络(GRU)与注意力机制(Attention)相结合的多变量回归预测模型研究,重点利用Matlab实现该深度学习模型的构建与仿真。该模型通过CNN提取输入数据的局部特征,利用GRU捕捉时间序列的长期依赖关系,并引入注意力机制增强关键时间步的权重,从而提升多变量时间序列回归预测的精度与鲁棒性。文中涵盖了模型架构设计、训练流程、参数调优及实际案例验证,适用于复杂非线性系统的预测任务。; 适合人群:具备一定机器学习与深度学习基础,熟悉Matlab编程环境,从事科研或工程应用的研究生、科研人员及算法工程师,尤其适合关注时间序列预测、能源预测、智能优化等方向的技术人员。; 使用场景及目标:①应用于风电功率预测、负荷预测、交通流量预测等多变量时间序列回归任务;②帮助读者掌握CNN-GRU-Attention混合模型的设计思路与Matlab实现方法;③为学术研究、毕业论文或项目开发提供可复现的代码参考和技术支持。; 阅读建议:建议读者结合Matlab代码逐模块理解模型实现细节,重点关注数据预处理、网络结构搭建与注意力机制的嵌入方式,并通过调整超参数和更换数据集进行实验验证,以深化对模型性能影响因素的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值