一、装饰者模式:运行时动态地将责任附加到对象上。
1、装饰者与被装饰对象有相同的超类型
2、一个或多个装饰者包装一个对象
3、装饰者在所委托被装饰者的行为之前与/或之后加上自己的行为
4、可在运行时动态地不限量地装饰
二、装饰者缺点:大量小类、增加实例化难度
三、java.io中装饰者应用
四、示例:
1、设计uml图
2、coding
1>.抽象组件类
//component:abstract class
public abstract class beverage{
string description = "unknown beverage";
public string getdescription(){
return description;
}
public abstract double cost();
}
2>.具体组件类
//具体组件类:espresso
public class espresso extends beverage{
public espresso(){
description = "espresso";
public double cost(){
return 1.99;
//具体组件类:houseblend
public class houseblend extends beverage{
public houseblend(){
description = "houseblend";
return .89;
//具体组件类:darkroast
public class darkroast extends beverage{
public darkroast(){
description = "darkroast";
return 2.59;
//具体组件类:decaf
public class decaf extends beverage{
public decaf(){
description = "decaf";
return 4.00;
3>.装饰者
//装饰者:抽象类condimentdecorator
public abstract class condimentdecorator extends beverage{
public abstract string getdescription();
//装饰者:milk
public class milk extends condimentdecorator{
beverage beverage;
public milk(beverage beverage){
this.beverage = beverage;
return beverage.getdescription() + ",milk";
return .55 + beverage.cost();
//装饰者:mocha
public class mocha extends condimentdecorator{
public mocha(beverage beverage){
return beverage.getdescription() + ",mocha";
return .20 + beverage.cost();
//装饰者:soy
public class soy extends condimentdecorator{
public soy(beverage beverage){
return beverage.getdescription() + ",soy";
return 1.56 + beverage.cost();
//装饰者:whip
public class whip extends condimentdecorator{
public whip(beverage beverage){
return beverage.getdescription() + ",whip";
return .99 + beverage.cost();
4>.测试
//test class
public class testd{
public static void main(string[] args){
beverage beverage = new espresso();
system.out.println(beverage.getdescription() + "$" + beverage.cost());
beverage beverage2 = new darkroast();
beverage2 = new mocha(beverage2);
beverage2 = new mocha(beverage2);
beverage2 = new whip(beverage2);
system.out.println(beverage2.getdescription() + "$" + beverage2.cost());
beverage beverage3 = new houseblend();
beverage3 = new soy(beverage3);
beverage3 = new mocha(beverage3);
beverage3 = new whip(beverage3);
system.out.println(beverage3.getdescription() + "$" + beverage3.cost());
5>.测试结果
五、java.io装饰者:大写转小写
1、java.io.filterinputstream装饰者
//java.io decorator:change uppercase to lowercase
import java.io.filterinputstream;
import java.io.inputstream;
import java.io.ioexception;
import java.lang.character;
public class lowercaseinputstream extends filterinputstream{
public lowercaseinputstream(inputstream is){
super(is);
public int read() throws ioexception{
int c = super.read();
return (c == -1?c:character.tolowercase((char)c));
public int read(byte[] b, int offset, int len) throws ioexception{
int c = super.read(b,offset,len);
for(int i = offset; i < offset + c; i++){
b[i] = (byte)character.tolowercase((char)b[i]);
}
return c;
2、资源testl.txt
//testl.txt
import java.lang.string;
import java.io.bufferedinputstream;
import java.io.fileinputstream;
public class testl{
public static void main(string[] arg) throws ioexception{
int c;
inputstream is = new lowercaseinputstream(new bufferedinputstream(new fileinputstream("testl.txt")));
while((c = is.read()) >= 0){
system.out.print((char)c);
is.close();
3、测试类
int c;
inputstream is = new lowercaseinputstream(new bufferedinputstream(new fileinputstream("testl.txt")));
4、测试结果