一、定义:
动态地给一个对象添加一些额外的职责,就增加功能而言,装饰模式比生成子类更加灵活。
二、组成部分:
抽象构件(Component)角色:给出一个抽象接口,以规范准备接收附加责任的对象。
具体构件(Concrete Component)角色:实现组件对象接口,通常就是被装饰器装饰的对象。
装饰(Decorator)角色:持有一个构件(Component)对象的实例,并定义一个与抽象构件接口一致的接口。
具体装饰(Concrete Decorator)角色:负责给构件对象"贴上"附加的责任。
三、优缺点:
优点:相比继承更灵活;
缺点:会产生很多细粒度对象。
四、实例
1、小菜穿衣服:
UML图:(自动生成的少可了点内容。。。装饰器抽象类要包含Person的一个引用)
代码实现:
public class xiaoCai extends Person {
public xiaoCai(String n) {
super(n);
}
public void show() {
System.out.println("�����"+super.getName()+"����װ��");
}
}
public abstract class Person {
private String name;
public Person(String name) {
this.name=name;
}
public String getName() {
return name;
}
public Person() {
}
public abstract void show() ;
}
public abstract class DecorateAbstract extends Person {
private Person per=null;
public Person decorate(Person p) {
per=p;
return this;
}
public void show() {
per.show();
}
}
public class KuaKu extends DecorateAbstract {
public void show() {
System.out.println("��� ");
super.show();
}
}
public class Tshirt extends DecorateAbstract {
public void show() {
System.out.println("Tshirt ");
super.show();
}
}
public class Test {
public static void main(String[] args) {
Person p=new xiaoCai("xiaoCai");
Tshirt t=new Tshirt();
KuaKu k=new KuaKu();
p=t.decorate(p);
p=k.decorate(p);
p.show();
}
}
2、IO流:
代码实现:
package 装饰模式IO流;
import java.io.IOException;
import java.io.OutputStream;
public class WjvjeOutputStream extends OutputStream {
OutputStream ops=null;
public WjvjeOutputStream(OutputStream ops) {
this.ops=ops;
}
@Override
public void write(int b) throws IOException {
//小写转换为大写;
if(b>=(int)('a')&&b<=(int)('z')) {
b+=(int)('A'-'a');
}
this.ops.write(b);
}
}
package 装饰模式IO流;
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Client {
public static void main(String[] args) throws IOException {
System.out.println(System.getProperty("user.dir"));
DataOutputStream cout = new DataOutputStream(
new BufferedOutputStream(new WjvjeOutputStream
(new FileOutputStream("IOTest.txt"))));
cout.write("Wahaha,hello,world".getBytes());
cout.close();
}
}
3、复杂的奖金计算:
UNL图:
代码实现:
package 复杂的奖金计算;
public abstract class Person {
public abstract double calMoney();
}
package 复杂的奖金计算;
public class Manager extends Person {
@Override
public double calMoney() {
return 0;
}
}
package 复杂的奖金计算;
public class xiaoCai extends Person {
@Override
public double calMoney() {
return 0;
}
}
package 复杂的奖金计算;
//package ����;
public abstract class DectorAb extends Person{
private Person p;
public Person dec(Person pp) {
p=pp;
return this;
}
public double calMoney() {
return p.calMoney();
}
}
package 复杂的奖金计算;
public class Praise1 extends DectorAb {
public double calMoney() {
double res=super.calMoney()+100;
System.out.println("加成方案一操作后"+res+":+¥100");
return res;
}
}
package 复杂的奖金计算;
public class Praise2 extends DectorAb{
public double calMoney() {
double tmp=super.calMoney();
double res=0.2*tmp+tmp;
System.out.println("加成方案二操作后"+res+"增加20%。");
return res;
}
}
package 复杂的奖金计算;
public class Client {
public static void main(String[] args) {
Person people=new xiaoCai();
baseMoney bm=new baseMoney();
Praise1 p1=new Praise1();
Praise2 p2=new Praise2();
people=bm.dec(people);
people=p1.dec(people);
people.calMoney();
System.out.println();
Person man=new Manager();
man=bm.dec(man);
man=p1.dec(man);
man=p2.dec(man);
man.calMoney();
}
}
THE END;