Java 设计模式 之 装饰模式
http://www.verejava.com/?id=16999090833167
package com.decorator.theory;
public class TestDecorator
{
public static void main(String[] args)
{
Person p=new Man();
p.show();
p=new ManagerDecorator(p);
p.show();
p=new ActorDecorator(p);
p.show();
}
}
package com.decorator.theory;
public interface Person
{
public void show();
}
package com.decorator.theory;
public class Man implements Person
{
@Override
public void show()
{
System.out.println("我是一个男人");
}
}
package com.decorator.theory;
public class ManagerDecorator extends Decorator
{
public ManagerDecorator(Person p)
{
super(p);
}
@Override
public void show()
{
p.show();
System.out.println("我还是一个经理");
}
}
package com.decorator.theory;
public abstract class Decorator implements Person
{
protected Person p;
public Decorator(Person p)
{
this.p=p;
}
}
package com.decorator.theory;
public class ActorDecorator extends Decorator
{
public ActorDecorator(Person p)
{
super(p);
}
@Override
public void show()
{
p.show();
System.out.println("我还是国际演员");
}
}