设计模式笔记之四 装饰模式
?
?
/** * 被装饰者接口 */public interface composite {public void methoda();public void methodb();}/** * 被装饰对象 */public class contretecomposite implements composite {@overridepublic void methoda() {system.out.println("contretedecorator methoda");}@overridepublic void methodb() {system.out.println("contretedecorator methodb");}}/** * 具体装饰者1 */public class concretedecorator1 implements composite {private composite composite;public concretedecorator1(composite composite) {this.composite = composite;}@overridepublic void methoda() {//这里实现装饰功能system.out.println("concretedecorator1 methoda");//这里调用被装饰者的原来功能composite.methoda();}@overridepublic void methodb() {//这里实现装饰功能system.out.println("concretedecorator1 methodb");//这里调用被装饰者的原来功能composite.methodb();}}/** * 具体装饰者2 */public class concretedecorator2 implements composite {private composite composite;public concretedecorator2(composite composite) {this.composite = composite;}@overridepublic void methoda() {//这里实现装饰功能system.out.println("concretedecorator2 methoda");//这里调用被装饰者的原来功能composite.methoda();}@overridepublic void methodb() {//这里实现装饰功能system.out.println("concretedecorator2 methodb");//这里调用被装饰者的原来功能composite.methodb();}}/** * 装饰者模式的使用 */public class testdecorator {/** * @param args */public static void main(string[] args) {composite composite = new concretedecorator2(new concretedecorator1(new contretecomposite()));composite.methoda();composite.methodb();}}