【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
随机推荐
- php XML 读写 创建
一 .XML 读 1.1. 首先同目录定义好一个XML文件 : book.xml <?xml version="1.0" encoding="utf-8" ...
- On Caching and Evangelizing SQL
http://www.oracle.com/technetwork/issue-archive/2011/11-sep/o51asktom-453438.html Our technologist ...
- c# double保留2位小数
/ (endIndex - startIndex); interval = Math.Round(interval , );
- HIVE: SerDe应用实例
数据文件内容 id=123,name=steven id=55,name=ray 期望输出格式 123 steven 55 ray 1. 创建表, 用正则表达式的形式指定格式 create table ...
- php -- strstr()字符串匹配函数(备忘)
Learn From: http://blog.csdn.net/morley_wang/article/details/7859922 strstr(string,search) strstr() ...
- 编写高质量JS代码的68个有效方法(十一)
*:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...
- 调用Ajax返回500错误的解决方法
看代码: public ActionResult UserLogin(LogOnModel model) { #region 验证码验证 #endregion OperationResult resu ...
- 【转载】Linux下动态共享库加载时的搜索路径详解
转载自:http://www.eefocus.com/article/09-04/71617s.html 对动态库的实际应用还不太熟悉的读者可能曾经遇到过类似“error while loading ...
- 基于selenium的pyse自动化测试框架
WebUI automation testing framework based on Selenium 介绍: pyse基于selenium(webdriver)进行了简单的二次封装,比seleni ...
- Android 学习笔记多媒体技术之 Drawable类+Tween(补间动画)+Frame(帧动画)
学习内容: 1.了解Drawable类的作用 2.如何使用Drawable... 3.了解Tween动画... 4.如何创建和使用Tween动画... 1.Drawable类... Drawabl ...