package com.designpattern.decoratorpattern;
/**
* component 组件 抽象角色
* @author zhongtao on 2018/10/23
*/
public interface house {
/**
* 装饰风格
*/
void style();
}
具体组件:
/**
* 具体组件
* @author zhongtao on 2018/10/23
*/
public class chinesestyle implements house {
@override
public void style() {
system.out.println("中式风格装修");
}
}
/**
* 具体组件
* @author zhongtao on 2018/10/23
*/
public class europeanstyle implements house {
@override
public void style() {
system.out.println("欧式风格装修");
}
}
抽象装饰类:
package com.designpattern.decoratorpattern;
/**
* 抽象装饰类
*
* @author zhongtao on 2018/10/23
*/
public class housedecorator implements house {
public house house;
public housedecorator(house house) {
this.house = house;
}
@override
public void style() {
house.style();
}
}
具体装饰类:
package com.designpattern.decoratorpattern;
/**
* 具体装饰类
*
* @author zhongtao on 2018/10/23
*/
public class reddecorator extends housedecorator {
public reddecorator(house house) {
super(house);
}
public void style() {
this.house.style();
system.out.println("红色装饰墙");
}
}
测试装饰器模式:
package com.designpattern.decoratorpattern;
import org.junit.test;
/**
* 测试装饰器模型
*
* @author zhongtao on 2018/10/22
*/
public class decoratorpatterntest {
/**
* 测试装饰器模型
*/
@test
public void testdecoratorpattern(){
chinesestyle chinesestyle = new chinesestyle();
house redchinesestyle = new reddecorator(new chinesestyle());
house redeuropeanstyle = new reddecorator(new europeanstyle());
system.out.println("中式装修");
chinesestyle.style();
system.out.println("\n中式加红色墙");
redchinesestyle.style();
system.out.println("\n欧式加红色墙");
redeuropeanstyle.style();
}
}