图1 装饰者模式【点击查看图片】
1,一个简单的以人为主体的装饰者模式
被装饰者
public interface Human {//被装饰者
public void wearClothes();
public void walkToWhere();
} //定义被装饰者,被装饰者初始状态有些自己的装饰
class Person implements Human { @Override
public void wearClothes() {
// TODO Auto-generated method stub
System.out.println("穿什么呢。。");
} @Override
public void walkToWhere() {
// TODO Auto-generated method stub
System.out.println("去哪里呢。。");
}
}
装饰者
public abstract class Decorator implements Human {//定义装饰者
private Human human; public Decorator(Human human) {//有参构造
this.human = human;
} @Override
public void wearClothes() {
human.wearClothes();
} @Override
public void walkToWhere() {
human.walkToWhere();
}
} class Decorator_zero extends Decorator { public Decorator_zero(Human human) {
super(human);
} public void goHome() {
System.out.println("进房子。。");
} public void findMap() {
System.out.println("书房找找Map。。");
} @Override
public void wearClothes() {
// TODO Auto-generated method stub
super.wearClothes();
goHome();
} @Override
public void walkToWhere() {
// TODO Auto-generated method stub
super.walkToWhere();
findMap();
}
} class Decorator_first extends Decorator { public Decorator_first(Human human) {
super(human);
} public void goClothespress() {
System.out.println("去衣柜找找看。。");
} public void findPlaceOnMap() {
System.out.println("在Map上找找。。");
} @Override
public void wearClothes() {
// TODO Auto-generated method stub
super.wearClothes();
goClothespress();
} @Override
public void walkToWhere() {
// TODO Auto-generated method stub
super.walkToWhere();
findPlaceOnMap();
}
} class Decorator_two extends Decorator { public Decorator_two(Human human) {
super(human);
} public void findClothes() {
System.out.println("找到一件D&G。。");
} public void findTheTarget() {
System.out.println("在Map上找到神秘花园和城堡。。");
} @Override
public void wearClothes() {
// TODO Auto-generated method stub
super.wearClothes();
findClothes();
} @Override
public void walkToWhere() {
// TODO Auto-generated method stub
super.walkToWhere();
findTheTarget();
}
}
测试验证
public class Client {
public static void main(String[] args) {
Human person = new Person();
Decorator decorator = new Decorator_two(new Decorator_first(new Decorator_zero(person))); decorator.wearClothes();
decorator.walkToWhere();
}
}
2,一个以汉堡添为主体的装饰者模式
被装饰者
public abstract class Humburger {//被装饰者,主体,被修补的物件
protected String name ;
public String getName(){
return name;
}
public abstract double getPrice(); } class ChickenBurger extends Humburger {//被修饰者的原始状态
public ChickenBurger(){
name = "鸡腿堡";
}
@Override
public double getPrice() {
return 10;
}
}
装饰者
public abstract class Condiment extends Humburger {//装饰者【修修补补的材料】
public abstract String getName();
} class Lettuce extends Condiment {
Humburger humburger;
public Lettuce(Humburger humburger){
this.humburger = humburger;
}
@Override
public String getName() {
return humburger.getName()+" 加生菜";
}
@Override
public double getPrice() {
return humburger.getPrice()+1.5;
}
} class Chilli extends Condiment {
Humburger humburger;
public Chilli(Humburger humburger){
this.humburger = humburger;
}
@Override
public String getName() {
return humburger.getName()+" 加辣椒";
}
@Override
public double getPrice() {
return humburger.getPrice(); //辣椒是免费的哦
} }
测试验证
public class Client {
public static void main(String[] args) {
Humburger humburger = new ChickenBurger();//鸡腿堡 价钱:10.0
System.out.println(humburger.getName()+" 价钱:"+humburger.getPrice()); Lettuce lettuce = new Lettuce(humburger);//鸡腿堡 加生菜 价钱:11.5
System.out.println(lettuce.getName()+" 价钱:"+lettuce.getPrice()); Chilli chilli = new Chilli(humburger);//鸡腿堡 加辣椒 价钱:10.0
System.out.println(chilli.getName()+" 价钱:"+chilli.getPrice()); Chilli chilli2 = new Chilli(lettuce);//鸡腿堡 加生菜 加辣椒 价钱:11.5
System.out.println(chilli2.getName()+" 价钱:"+chilli2.getPrice());
} }