DesignPatterns-装饰器模式
2019独角兽企业重金招聘Python工程师标准>>>
package com.zhiru;/** 装饰器模式* 实现动态的为对象添加功能* 是从一个对象外部给一个对象添加功能。* 基于对象组合的方式给对象添加功能* 组成要素:Component,ConcreteComponent,Decorator,ConcreteDecorator1,ConcreteDecorator2,...*/
//http://blog.163.com/michael_yu/blog/static/173853371201010365749187///Component
//学生接口
abstract class Student {public abstract void student();
}// concretecomponent
// 具体的学生接口
class GoodStudent extends Student {@Overridepublic void student() {// TODO Auto-generated method stubSystem.out.println("good student");}}// Decorator
// 学生接口装饰器,对学生抽象类进行组合.
class DecoratorStudent extends Student {private Student stu;public DecoratorStudent(Student stu) {this.stu = stu;}@Overridepublic void student() {// TODO Auto-generated method stubstu.student();}}// ConcreteDecorator class
// 三好学生
class ThreeGoodStudent extends DecoratorStudent {public ThreeGoodStudent(Student stu) {super(stu);}public void student() {super.student();System.out.println("three good student");}
}// 乐于助人的学生
class WarmHeartStudent extends DecoratorStudent {public WarmHeartStudent(Student stu) {super(stu);// TODO Auto-generated constructor stub}public void student() {super.student();System.out.println("warm hearted student");}
}// 擅长编程的学生
class BeginningAbilityStudent extends DecoratorStudent {public BeginningAbilityStudent(Student stu) {super(stu);// TODO Auto-generated constructor stub}public void student() {super.student();System.out.println("be good at programming student");}
}public class DecoratorPattern {public static void main(String[] args) {// TODO Auto-generated method stub// 擅长编程的,三好好学生new BeginningAbilityStudent(new ThreeGoodStudent(new GoodStudent())).student();// 热心,善于编程,三好,好学生。new WarmHeartStudent(new BeginningAbilityStudent(new ThreeGoodStudent(new GoodStudent()))).student();}}
/** good student three good student be good at programming student * good student* three good student be good at programming student warm hearted student*/
参考博客:http://blog.163.com/michael_yu/blog/static/173853371201010365749187/
转载于:https://my.oschina.net/pangzhuzhu/blog/313000
DesignPatterns-装饰器模式相关推荐
- java设计模式----装饰器模式
Decorator装饰器,顾名思义,就是动态地给一个对象添加一些额外的职责,就好比为房子进行装修一样.因此,装饰器模式具有如下的特征: 它必须具有一个装饰的对象. 它必须拥有与被装饰对象相同的接口. ...
- Python设计模式-装饰器模式
Python设计模式-装饰器模式 代码基于3.5.2,代码如下; #coding:utf-8 #装饰器模式class Beverage():name = ""price = 0.0 ...
- 3年工作必备 装饰器模式
故事 古话说的好:人靠衣裳马靠鞍.下面先带大家来熟悉这句话的背景: 人靠衣装马靠鞍,狗配铃铛跑的欢出自沈自晋<望湖亭记>第十出:"虽然如此,佛靠金装,人靠衣装,打扮也是很要紧的. ...
- 装饰者模式 php,PHP设计模式之装饰器模式
装饰器设计模式 什么是装饰器模式 装饰器模式就是对一个已有的结构增加装饰.装饰模式是在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能.它是通过创建一个包装对象,也就是装饰来包裹真实的对象 ...
- 漫画设计模式:什么是 “装饰器模式” ?
作者 | 小灰 来源 | 程序员小灰(ID:chengxuyuanxiaohui) ----- 第二天 ----- ------------ 装饰器模式都包含哪些核心角色呢? 1. Compone ...
- java设计模式之装饰器模式
一.装饰器模式简介 装饰器模式可以动态给一个对象添加一些额外的职责,同时又不改变其结构.就象在墙上刷油漆.使用Decorator模式相比用生成子类方式达到功能的扩充显得更为灵活.这种模式创建了一个装饰 ...
- java外观设计修改_Java设计模式之外观模式和装饰器模式的设计(精选)
前言 本篇来学习下结构型模式的外观模式和装饰器模式. 外观模式 简介 外观模式隐藏系统的复杂性,并向客户端提供了一个客户端可以访问系统的接口.这种类型的设计模式属于结构型模式,它向现有的系统添加一个接 ...
- decorator java_装饰器模式-Decorator(Java实现)
装饰器模式-Decorator(Java实现) 装饰器模式允许向一个现有的对象添加新的功能, 同时又不改变其结构. 其中 "现有对象"在本文中是StringDisplay类. 添加 ...
- php设计模式八-----装饰器模式
1.介绍: 装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构.这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装. 这种模式创建了一个装饰 ...
- 适配器模式、代理模式、装饰器模式使用场景区别
适配器模式 适配器模式是提供和原先接口不一样的接口,做一个二次转换,原来的接口还是可以继续使用的. 代理模式 原先的接口是不能访问的(无权使用),只能通过代理来去访问原先接口.代理过程中可能会对原有接 ...
最新文章
- centos7下安装docker(11容器操作总结)
- PHP/TP5 接口设计中异常处理
- 从千团大战到网贷战国,烧钱千亿背后的底层用户增长逻辑
- 基本的MySQL操作
- python2 openpyxl 复制excel内容到新的excel文件中 告警问题
- C语言对stm32f103程序,STM32F103WIFI程序C语言
- P1601高精度加法
- vue页面回显数据_解决vue表单回显数据无法修改的问题
- CMAKE出现: undefined reference to
- Android 中的长度单位详解 dp、sp、px、in、pt、mm 转载
- oracle 数据库为nomount状态,oracle 数据库为nomount状态
- 影视】100种说爱你的方式~
- AD19---彻底解决原理图转PCB时,出现failed to add class member及Unknown Pin的问题
- ENVI国产卫星插件
- JAVA NIO 实现群聊
- MCU低功耗设计(一)理论
- 2D游戏案例:雷霆战机
- Photoshop十种抠图方法
- gromacs 安装_gromacs cpu版安装
- twitter推文案例