设计模式学习--Decorator模式

(一)Decorator模式——包装器模式
使用典型:(1)JAVA的IO包的应用 BufferedInputStream(DataInputStream)..
(2)sitemesh的页面装饰布局框架组件 decorators.xml就是它的配置文件
作用:它能通过创建一个包装对象,也就是装饰来包裹的对象。不必改变原类文件和使用集成的情况下,动态地扩展一个对象的功能。
1)使用方法:
一般是一个父类或接口
而下面有一些透明的我们可以实现的类
而再下面就是我们的包装类
在包装类的构造方法里面传入接口/父类
而且在包装类覆盖父类的方法 扩展和修改那个方法的特性作用
eg:
  new BufferedInputStream(DataInputStream)中InputStream类是DataInputStream和BufferedInputStream的父类
但BufferedInputStream 覆盖了 write() read()等方法 并假如缓存的功能




2) 应用eg:
sitemesh的核心就是一个Filter
在WEB-INF/web.xml中加入
 <filter>
<filter-name>sitemesh</filter-name>
<filter-class>
com.opensymphony.module.sitemesh.filter.PageFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
同时 你在
在WEB-INF下建立decorators.xml描述各装饰器页面。加入
 <decorators defaultdir="/decorators">
<decorator name="template" page="template.jsp">
<pattern>*</pattern>
</decorator>
</decorators>

表明定义了 要把什么模板样式(共同的页面头啊 什么版权信息啊 写在template.jsp中)<decorator:title /> <decorator:head /> <decorator:body/>。。。定义放置的位置
那么你以后在这个web应用中使用任何的页面 都会自动包装上template.jsp 在对应位置上 ... 就有 你在模板上定义的东西

猜你喜欢

转载自blog.csdn.net/pzk417/article/details/83600457
今日推荐