【GOF23设计模式】装饰模式
来源:http://www.bjsxt.com/
一、【GOF23设计模式】_装饰模式、IO流底层架构、装饰和桥接模式的区别
package com.test.decorator;
/**
* Component抽象构件角色
*/
public interface ICar {
void move();
} //ConcreteComponent 具体构件角色(真实对象)
class Car implements ICar {
@Override
public void move() {
System.out.println("陆地上跑!");
}
} //Decorator装饰角色
class SuperCar implements ICar {
protected ICar car; public SuperCar(ICar car) {
super();
this.car = car;
} @Override
public void move() {
car.move();
}
} //ConcreteDecorator具体装饰角色
class FlyCar extends SuperCar { public FlyCar(ICar car) {
super(car);
} public void fly() {
System.out.println("天上飞!");
} @Override
public void move() {
super.move();
fly();
}
}
//ConcreteDecorator具体装饰角色
class WaterCar extends SuperCar { public WaterCar(ICar car) {
super(car);
} public void swim() {
System.out.println("水上游!");
} @Override
public void move() {
super.move();
swim();
}
}
//ConcreteDecorator具体装饰角色
class AICar extends SuperCar { public AICar(ICar car) {
super(car);
} public void autoMove() {
System.out.println("自动跑!");
} @Override
public void move() {
super.move();
autoMove();
}
}
package com.test.decorator; public class Client {
public static void main(String[] args) {
Car car = new Car();
car.move(); System.out.println("增加新的功能,飞行--------------");
FlyCar flyCar = new FlyCar(car);
flyCar.move(); System.out.println("增加新的功能,水上游--------------");
WaterCar waterCar = new WaterCar(car);
waterCar.move(); System.out.println("增加两个新的功能:飞行,水上游----------------");
WaterCar waterCar2 = new WaterCar(new FlyCar(car));
waterCar2.move();
}
}
控制台输出:
陆地上跑!
增加新的功能,飞行--------------
陆地上跑!
天上飞!
增加新的功能,水上游--------------
陆地上跑!
水上游!
增加两个新的功能:飞行,水上游----------------
陆地上跑!
天上飞!
水上游!
【GOF23设计模式】装饰模式的更多相关文章
- GOF23设计模式之适配器模式
GOF23设计模式之适配器模式 结构型模式: 核心作用:是从程序的结构上实现松耦合,从而可以扩大整体的类结构,用来解决更大的问题. 分类:适配器模式.代理模式.桥接模式.装饰模式.组合模式.外观模式. ...
- 【GOF23设计模式】工厂模式
来源:http://www.bjsxt.com/ 一.[GOF23设计模式]_简单工厂模式详解.面向对象设计原则.开闭原则.依赖反转原则.迪米特法则 没有工厂模式的情况 package com.te ...
- 【GOF23设计模式】单例模式
来源:http://www.bjsxt.com/ 一.[GOF23设计模式]_单例模式.应用场景.饿汉式.懒汉式 1.GOF23设计模式 2.单例模式 3.饿汉式 1 package com.t ...
- 【GOF23设计模式】备忘录模式
来源:http://www.bjsxt.com/ 一.[GOF23设计模式]_备忘录模式.多点备忘.事务操作.回滚数据底层架构 package com.test.memento; /** * 源发器类 ...
- 【GOF23设计模式】观察者模式
来源:http://www.bjsxt.com/ 一.[GOF23设计模式]_观察者模式.广播机制.消息订阅.网络游戏对战原理 package com.test.observer; import ja ...
- 【GOF23设计模式】状态模式
来源:http://www.bjsxt.com/ 一.[GOF23设计模式]_状态模式.UML状态图.酒店系统房间状态.线程对象状态切换 package com.test.state; public ...
- 【GOF23设计模式】模板方法模式
来源:http://www.bjsxt.com/ 一.[GOF23设计模式]_模板方法模式.钩子函数.方法回调.好莱坞原则 package com.test.templateMethod; publi ...
- 【GOF23设计模式】策略模式
来源:http://www.bjsxt.com/ 一.[GOF23设计模式]_策略模式.CRM中报价策略.GUI编程中布局管理器底层架构 package com.test.strategy; /** ...
- 【GOF23设计模式】解释器模式 & 访问者模式
来源:http://www.bjsxt.com/ 一.[GOF23设计模式]_解释器模式.访问者模式.数学表达式动态解析库式 1.解释器模式Interpreter 2.访问者模式Visitor
随机推荐
- 图解 & 深入浅出 JavaWeb:Servlet 再说几句
Writer :BYSocket(泥沙砖瓦浆木匠) 微 博:BYSocket 豆 瓣:BYSocket FaceBook:BYSocket Twitter ...
- python 字符串比较
cmp方法比较两个对象,并根据结果返回一个整数.cmp(x,y)如果X< Y,返回值是负数 如果X>Y 返回的值为正数. sStr1 = 'strch'sStr2 = 'strchr'pr ...
- 一点一滴之NHibernate
之前介绍了Dapper,速度很快,很轻量,很好用. 但是Dapper其实有自己的弊端,比如在数据关系复杂,数据库表非常多,多数据库支持,数据库结构变动频繁的时候总是很无奈.尽管有代码生成器,但是代码生 ...
- 线程安全集合 ConcurrentDictionary<TKey, TValue> 类
ConcurrentDictionary<TKey, TValue> 类 [表示可由多个线程同时访问的键/值对的线程安全集合.] 支持 .NET Framework 4.0 及以上. 示例 ...
- D3D的绘制
一.D3D中的绘制 顶点缓存和索引缓存:IDirect3DVertexBuffer9 IDirect3DIndexBuffer 使用这两缓存而不是用数组来存储数据的原因是,缓存可以被放置在显存中,进行 ...
- Linux磁盘操作命令
查看本地磁盘使用情况:df或者df -l单位为k 容量便于查看,以1024单位换算为M或者G等:df -h或者df -lh 以1000为单位换算:df -H 显示文件系统类型:df -T 显示指定文件 ...
- Android 学习笔记之Volley(六)实现获取服务器的字符串响应...
学习内容: 1.使用StringRequest实现获取服务器的字符串响应... 前几篇一直都在对服务——响应过程的源码进行分析,解析了整个过程,那么Volley中到底实现了哪些请求才是我们在开发中 ...
- 使用自带的JavaScriptSerializer序列化实体 指定的属性如何不序列化
public class GridConfig { public string width = "100%"; public string source = "dataA ...
- redis在centOS的安装
1.安装tcl支持 yum install tcl 2.安装redis我们以最新的2.8.9为例 $ wget http://download.redis.io/releases/redis-2.8. ...
- 转载:第二弹!全球首个微信小程序(应用号)开发教程!通宵吐血赶稿!每日更新!
今天一波三折,承受了超出预料的压力和煎熬,最后还是决定继续放出我的更新教程.我想我一没有泄露公司的代码,二没有提供泄露开发工具下载,只是从程序猿角度写了篇开发日志.我已经做好了最坏的准备,就算放弃这份 ...