装饰设计模式
来源:互联网 发布:正则表达式大全 python 编辑:程序博客网 时间:2024/05/23 19:21
package cn.itcast.decorator;public class DecoratorDemo {public static void main(String[] args) {Person p = new Person();p.eat();}}class Person{public void eat() {System.out.println("我正在吃···········");}}
首先我们有了人这个类,创建了他的对象,然后人是需要吃饭的,但是吃饭之前他需要先吃一点甜点。但是这回人的对象p已经创建出来了。之中并没有先吃甜点的方法。这个时候如果我们是用继承,创建一个他的子类。拓展它的功能,显然非常的不合适。于是便有了下面一个巧妙的解决方法。
package cn.itcast.decorator;public class DecoratorDemo {/** * 装饰设计模式 * 解决的问题是:给已有的对象提供额外增强的功能。还不对原有对象进行修改 * 对对象而言是透明的 就像什么都没有发生过一样 * @param args */public static void main(String[] args) {Person p = new Person();SuperPerson sp = new SuperPerson(p);sp.eat();}}class Person{public void eat() {System.out.println("我正在吃···········");}}class SuperPerson{private Person p;public SuperPerson(Person p) {this.p = p;}public void eat() {System.out.println("先吃点甜点");p.eat();}}这就是装饰设计模式,java的IO流中大量的用到了这个模式。
0 0
- 设计模式---装饰模式
- 设计模式:装饰模式
- 设计模式------装饰模式
- 设计模式--装饰模式
- 设计模式--装饰模式
- 设计模式--装饰模式
- 设计模式 装饰模式
- 设计模式----装饰模式
- 设计模式-【装饰模式】
- 设计模式--装饰模式
- 设计模式装饰模式
- 设计模式 - 装饰模式
- 设计模式-----装饰模式.
- 设计模式-----装饰模式.
- [ 设计模式 ] 装饰模式!
- 设计模式:装饰模式
- 设计模式 - 装饰模式
- 设计模式 - 装饰模式
- NOIP提高组模拟 百团大战
- 学无止境:7月最热免费优秀资源推荐
- ceph存储 Google perftools工具内存检测以及性能分析
- 使用Nexus搭建Maven私服(2)
- Android基于PinnedSectionListView实现联系人通讯录
- 装饰设计模式
- Java设计模式(四) 之 模板方法模式
- AngularJS Tutorial(1)from w3school
- Java删除ArrayList中的重复元素的2种方法
- JS 模块化使用 [Require JS]
- Android studio 修改包名
- c++ 指针
- Java设计模式(五) 之 工厂模式
- 线程的那些事