装饰者模式
来源:互联网 发布:光大证券金阳光mac版 编辑:程序博客网 时间:2024/06/07 17:41
定义:动态给一个对象添加一些额外的职责,Decorator模式相比用生成子类方式达到功能的扩充显得更为灵活。
要点:装饰者与被装饰者拥有共同的超类,继承的目的是继承类型,而不是行为
代码
package Decorator;
abstract class human {
public abstract void show();
}
class person extends human{
private String name;
public person(String name) {
this.name=name;
}
@Override
public void show() {
System.out.print("我是"+name+" 今天我要 ");
}
}
abstract class Decorator extends human{
private human human;
public void Decorate(human human) {
this.human=human;
}
@Override
public void show(){
human.show();
}
}
class TShirts extends Decorator{
public void show(){
super.show();
wereTShirts();
}
public void wereTShirts() {
System.out.print("穿T恤 ");
}
}
class Trouser extends Decorator{
public void show(){
super.show();
wereTrouser();
}
public void wereTrouser() {
System.out.print("穿裤衩 ");
}
}
class Sneakers extends Decorator{
public void show(){
super.show();
wereSneakers();
}
public void wereSneakers() {
System.out.print("打领带 ");
}
// ......
}
package Decorator;
public class Client {
public static void main(String[]args){
person person=new person("1e0zj");
TShirts tShirts=new TShirts();
Trouser trouser=new Trouser();
Sneakers sneakers=new Sneakers();
tShirts.Decorate(person);
trouser.Decorate(tShirts);
sneakers.Decorate(trouser);
sneakers.show();
}
}
输出:我是1e0zj 今天我要 穿T恤 穿裤衩 打领带
阅读全文
1 0
- 装饰者模式(Derector)
- 装饰者模式
- Decorator 装饰者模式
- 装饰者设计模式
- 装饰者模式
- 装饰者模式
- 装饰者模式
- 装饰者模式(Decorator)
- 装饰者模式
- 装饰者模式
- 装饰者模式
- 装饰者模式 DecoratorPattern
- 设计模式 - 装饰者
- 装饰者模式
- 装饰者模式
- 装饰者模式 - 2
- 装饰者模式
- 装饰者模式
- IntelliJ IDEA 2017激活(Mac版本)
- 利用MFC绘制Excel数据的波形
- 循环:念整数
- 【TED】The psychology of self-motivation--Scott Geller
- 模拟实现printf
- 装饰者模式
- 【Caffe的C++接口使用说明(一)】caffe_windows下的第一个测试程序学习教程
- #300. 【CTSC2017】吉夫特
- caffe学习笔记(八)——binaryproto 转 npy、mat文件
- C语言单向链表的实现
- 【VS插件】Image Watch插件
- 数组:井字棋
- 【脚本语言系列】关于Python网页服务Plone,你需要知道的事
- D3 force(力导向图)研究之二:如何布局超过十万个节点的图谱