【设计模式-装饰模式】

在装饰模式中的各个角色有:
  (1)抽象构件(Component)角色:给出一个抽象接口,以规范准备接收附加责任的对象。
  (2)具体构件(Concrete Component)角色:定义一个将要接收附加责任的类。
  (3)装饰(Decorator)角色:持有一个构件(Component)对象的实例,并定义一个与抽象构件接口一致的接口。
  (4)具体装饰(Concrete Decorator)角色:负责给构件对象添加上附加的责任。
以下示例中,ThirdParty.Java假定是一个现有的或者第三方的功能,因某种原因我们不能直接修改,它提供了一个sayMsg()的方法,而我们现在要做的是想在它的sayMsg()方法中增加一些我们想额外输出的内容,于是我们重写了一个Decorator.java类。MailTest.java是客户端测试程序。
IthirdParty.Java--抽象接口类
=====================
package decorator.saystr;
public interface IthirdParty {
public String sayMsg();
}
ThirdParty.Java--具体类
===================
public class ThirdParty implements IthirdParty {
public String sayMsg() {
  return "hello";
  }
}
Decorator1.java 具体装饰类1
==================
package decorator.saystr;
public class Decorator1 implements IThirdParty {
private IThirdParty thirdParty;
public Decorator1(IThirdParty thirdParty){
this.thirdParty= thirdParty;
}
public String sayMsg(){
return "##1"+ thirdParty.sayMsg() + "##1";
}
}
Decorator1.java 具体装饰类2
==================
package decorator.saystr;
public class Decorator2 implements IThirdParty {
private IThirdParty thirdParty;
public Decorator2(IThirdParty thirdParty){
this.thirdParty= thirdParty;
}
public String sayMsg(){
return "##2"+ thirdParty.sayMsg() + "##2";
}
}
MailTest.java
====================
package decorator.saystr;
public class MailTest {
public static void main(String[] args){

  IthirdParty thirdPartyOne =new ThirdParty();
  IthirdParty decorator1 =new Decorator1(thirdPartyOne);
  IthirdParty decorator2 =new Decorator2(decorator1);
  
  System.out.println(decorator2.sayMsg());
}
}
执行结果是:##2##1hello##1##2
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
基于python+pyqt5开发的俄罗斯方块游戏源码+使用说明.zip基于python+pyqt5开发的俄罗斯方块游戏源码+使用说明.zip 【优质项目推荐】 1.项目代码功能经验证ok,确保稳定可靠运行。欢迎下载使用!在使用过程中,如有问题或建议,请及时私信沟通,帮助解答。 2.项目主要针对各个计算机相关专业,包括计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网等领域的在校学生、专业教师或企业员工使用。 3.项目具有丰富的拓展空间,不仅可作为入门进阶,也可直接作为毕设、课程设计、大作业、项目初期立项演示等用途。 4.如果基础还行,或热爱钻研,可基于此项目进行二次开发,DIY其他不同功能。 功能 播放音乐 暂停游戏 消除方块的音效 窗口自由拖拽 游戏存档 安装和运行 ### 1. 使用`pip` ```shell # csdn下载此项目源码解压重命名为PyQt5-Tetris(不要有中文路径) 进入到项目 cd PyQt5-Tetris # 使用pip安装依赖 pip install -r requirements.txt # 运行 python main.py ``` ### 2. 使用`pipenv` ```shell # csdn下载此项目源码解压重命名为PyQt5-Tetris(不要有中文路径) # 进入到项目 cd PyQt5-Tetris # 使用pipenv安装依赖 pipenv sync # 运行 pipenv run start ``` ### 3. 按键说明 - `A` - 向左移动 - `D`- 向右移动 - `W` - 变形 - `S` - 加速下降 - `P` - 暂停 - `ESC` - 退出程序 ## PyInstaller 打包 ### 1. 使用`pip` ```shell # 安装依赖 pip install -r requirements-dev.txt # 打包,打包成功之后exe文件在dist文件夹下 pyinstaller main.spec ``` ### 2. 使用`pipenv` ```shell # 安装依赖 pipenv install --dev # 打包,打包成功之后exe文件在dist文件夹下 pipenv run build ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值