java设计模式之------装饰者模式
2016-07-04 15:20
411 查看
java设计模式之------装饰者模式
============================================
装饰模式(Decorator),动态的给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活。
可以有效的把类的核心职责和装饰功能区分开。
举例:
Cooking炒菜
CookingEgg炒鸡蛋
Condimen调料基类(只是抽象了一层行为,跟装饰者模式没关系)
Oil油
Salt盐
Mybatis缓存设计和I/O流的设计,就是应用装饰者模式思想设计的
1.Mybatis缓存设计
Executor执行类
public Executor newExecutor(Transaction transaction, ExecutorType executorType, boolean autoCommit) {
Executor executor;
if (ExecutorType.BATCH == executorType) {
executor = new BatchExecutor(this, transaction);
} else if (ExecutorType.REUSE == executorType) {
executor = new ReuseExecutor(this, transaction);
} else {
executor = new SimpleExecutor(this, transaction);
}
if (cacheEnabled) {
executor = new CachingExecutor(executor, autoCommit);//装饰
}
return executor;
}
结构类图:
executor.query()[BaseExecutor.query()-->SimpleExecutor.doQuery()]
executor.query()[CachingExecutor.query()-->BaseExecutor.query()-->SimpleExecutor.doQuery()]
CachingExecutor包装BaseExecutor
2.I/O流
============================================
装饰模式(Decorator),动态的给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活。
可以有效的把类的核心职责和装饰功能区分开。
举例:
Cooking炒菜
CookingEgg炒鸡蛋
Condimen调料基类(只是抽象了一层行为,跟装饰者模式没关系)
Oil油
Salt盐
abstract class Cooking { public abstract void doCooking(); } public class CookingEgg extends Cooking { public void doCooking(){ System.out.println("do Egg Cooking"); } } public abstract class Condimen extends Cooking { public abstract void doCooking(); } public class Oil extends Condimen { Cooking cooking; public Oil(Cooking cooking){ this.cooking = cooking; } public void doCooking(){ cooking.doCooking(); //一些功能扩展 System.out.println("add Oil"); } } public class Salt extends Condimen { Cooking cooking; public Salt(Cooking cooking){ this.cooking = cooking; } public void doCooking(){ cooking.doCooking(); //一些功能扩展 System.out.println("add Salt"); } } public abstract class Test { public static void main(String[] args) { Cooking cooking = new CookingEgg(); Oil oil = new Oil(cooking); Salt salt = new Salt(oil); salt.doCooking(); //addCondimen("Oil"); //addCondimen("Salt"); addCondimen("OilSalt"); } public static void addCondimen(String condimen) { Cooking cooking = new CookingEgg(); if(condimen.contains("Oil")){ cooking = new Oil(cooking); } if(condimen.contains("Salt")){ cooking = new Salt(cooking); } cooking.doCooking(); } }
Mybatis缓存设计和I/O流的设计,就是应用装饰者模式思想设计的
1.Mybatis缓存设计
Executor执行类
public Executor newExecutor(Transaction transaction, ExecutorType executorType, boolean autoCommit) {
Executor executor;
if (ExecutorType.BATCH == executorType) {
executor = new BatchExecutor(this, transaction);
} else if (ExecutorType.REUSE == executorType) {
executor = new ReuseExecutor(this, transaction);
} else {
executor = new SimpleExecutor(this, transaction);
}
if (cacheEnabled) {
executor = new CachingExecutor(executor, autoCommit);//装饰
}
return executor;
}
结构类图:
executor.query()[BaseExecutor.query()-->SimpleExecutor.doQuery()]
executor.query()[CachingExecutor.query()-->BaseExecutor.query()-->SimpleExecutor.doQuery()]
CachingExecutor包装BaseExecutor
2.I/O流
相关文章推荐
- 七、设计模式——装饰模式
- PHP设计模式之装饰者模式代码实例
- JavaScript设计模式之装饰者模式介绍
- python self,cls,decorator的理解
- 学习JavaScript设计模式之装饰者模式
- java设计模式系列之装饰者模式
- Java设计模式之装饰者模式详解和代码实例
- python装饰器decorator介绍
- Python中decorator使用实例
- Python使用logging结合decorator模式实现优化日志输出的方法
- 举例讲解Java设计模式编程中Decorator装饰者模式的运用
- Python使用logging结合decorator模式实现优化日志输出的方法
- Python Decorators
- 装饰模式-设计模式(三)
- 装饰者模式-常用设计模式
- 装饰者模式里面必须要知道的事情
- Display Tag Lib——Struts
- 高级.NET工程师必须掌握的知识点
- GOF设计模式学习笔记 --辨析各种模式的要点和相似模式之间的区别