/**
* 目标接口:房子
*/
public interface house {
void output();
}
具体的房子:donghaohouse
/**
* 房子实现类
*/
public class donghaohouse implements house {
@override
public void output() {
system.out.println("这是董浩的房子");
}
}
具体的房子:donglianghouse
/**
* 房子实现类
*/
public class donglianghouse implements house {
@override
public void output() {
system.out.println("这是董量的房子");
}
}
装饰器:decorator
public class decorator implements house {
private house house;
public decorator(house house){
this.house = house;
}
@override
public void output() {
system.out.println("这是针对房子的前段装饰增强");
house.output();
system.out.println("这是针对房子的后段装饰增强");
}
}
测试类:
public class clienter {
public static void main(string[] args) {
house donghaohouse = new donghaohouse();
house decorator = new decorator(donghaohouse);
decorator.output();
}
}
public class proxy implements house {
private house house;
public decorator(){
this.house = new donghaohouse();
}
@override
public void output() {
system.out.println("这是针对目标的前段增强");
house.output();
system.out.println("这是针对目标的后段增强");
}
}
装饰模式
public class decorator implements house {
private house house;
public decorator(house house){
this.house = house;
}
@override
public void output() {
system.out.println("这是针对房子的前段装饰增强");
house.output();
system.out.println("这是针对房子的后段装饰增强");
}
}