首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

装饰器形式

2013-11-09 
装饰器模式public interface ISource {public void operation()}?public class Source implements ISourc

装饰器模式

public interface ISource {    public void operation();   }

?

public class Source implements ISource {    public void operation() {        System.out.println("原始类方法");    }}

?

public class Decorator1 implements ISource {    private ISource source;        public Decorator1(ISource source){        this.source = source;    }        public void operation() {        System.out.println("第一个装饰器前");        source.operation();        System.out.println("第一个装饰器后");    }}

?

public class Decorator2 implements ISource {    private ISource source;        public Decorator2(ISource source){        this.source = source;    }        public void operation() {        System.out.println("第二个装饰器前");        source.operation();        System.out.println("第二个装饰器后");    }}

?

public class Decorator3 implements ISource {    private ISource source;        public Decorator3(ISource source){        this.source = source;    }        public void operation() {        System.out.println("第三个装饰器前");        source.operation();        System.out.println("第三个装饰器后");    }}

?

public class Client {    public static void main(String[] args) {        ISource source = new Decorator3(new Decorator2(new Decorator1(new Source())));        source.operation();    }}

?

?

热点排行