设计模式笔记之四 装饰模式

     
 
/** * 被装饰者接口 */public interface composite {	public void methoda();	public void methodb();	}/** * 被装饰对象 */public class contretecomposite implements composite {	@override	public void methoda() {				system.out.println("contretedecorator methoda");	}	@override	public void methodb() {				system.out.println("contretedecorator methodb");	}	}/** * 具体装饰者1 */public class concretedecorator1 implements composite {	private composite composite;		public concretedecorator1(composite composite) {				this.composite = composite;	}		@override	public void methoda() {				//这里实现装饰功能		system.out.println("concretedecorator1 methoda");				//这里调用被装饰者的原来功能		composite.methoda();	}	@override	public 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;	}		@override	public void methoda() {				//这里实现装饰功能		system.out.println("concretedecorator2 methoda");				//这里调用被装饰者的原来功能		composite.methoda();	}	@override	public 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();	}}
 

你可能感兴趣的:(java,工作)