从语意上讲,代理模式的目标是控制对被代理对象的访问,而装饰模式是给原对象增加额外功能。
代理模式类图如下
装饰模式类图如下
从上面可以看到,代理模式和装饰模式的类图非常类似。下图结合具体的代码讲解两者的不同。
本文所有代码均可从 作者Github 下载
代理模式和装饰模式都包含ISubject和ConcreteSubject,并且这两种模式中这两个Component的实现没有任何区别。
ISubject代码如下
package com.jasongj.subject;
public interface ISubject {
void action();
}
ConcreteSubject代码如下
package com.jasongj.subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ConcreteSubject implements ISubject {
private static final Logger LOG = LoggerFactory.getLogger(ConcreteSubject.class);
@Override
public void action() {
LOG.info("ConcreteSubject action()");
}
}
代理类实现方式如下
package com.jasongj.proxy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Random;
import com.jasongj.subject.ConcreteSubject;
import com.jasongj.subject.ISubject;
public class ProxySubject implements ISubject {
private static final Logger LOG = LoggerFactory.getLogger(ProxySubject.class);
private ISubject subject;
public ProxySubject() {
subject = new ConcreteSubject();
}
@Override
public void action() {
preAction();
if((new Random()).nextBoolean()){
subject.action();
} else {
LOG.info("Permission denied");
}
postAction();
}
private void preAction() {
LOG.info("ProxySubject.preAction()");
}
private void postAction() {
LOG.info("ProxySubject.postAction()");
}
}
从上述代码中可以看到,被代理对象由代理对象在编译时确定,并且代理对象可能限制对对被代理对象的访问。
代理模式使用方式如下
package com.jasongj.client;
import com.jasongj.proxy.ProxySubject;
import com.jasongj.subject.ISubject;
public class StaticProxyClient {
public static void main(String[] args) {
ISubject subject = new ProxySubject();
subject.action();
}
}
从上述代码中可以看到,调用方直接调用代理而不需要直接操作被代理对象甚至都不需要知道被代理对象的存在。同时,代理类可代理的具体被代理类是确定的,如本例中ProxySubject只可代理ConcreteSubject。
装饰类实现方式如下
package com.jasongj.decorator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.jasongj.subject.ISubject;
public class SubjectPreDecorator implements ISubject {
private static final Logger LOG = LoggerFactory.getLogger(SubjectPreDecorator.class);
private ISubject subject;
public SubjectPreDecorator(ISubject subject) {
this.subject = subject;
}
@Override
public void action() {
preAction();
subject.action();
}
private void preAction() {
LOG.info("SubjectPreDecorator.preAction()");
}
}
package com.jasongj.decorator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.jasongj.subject.ISubject;
public class SubjectPostDecorator implements ISubject {
private static final Logger LOG = LoggerFactory.getLogger(SubjectPostDecorator.class);
private ISubject subject;
public SubjectPostDecorator(ISubject subject) {
this.subject = subject;
}
@Override
public void action() {
subject.action();
postAction();
}
private void postAction() {
LOG.info("SubjectPostDecorator.preAction()");
}
}
装饰模式使用方法如下
package com.jasongj.client;
import com.jasongj.decorator.SubjectPostDecorator;
import com.jasongj.decorator.SubjectPreDecorator;
import com.jasongj.subject.ConcreteSubject;
import com.jasongj.subject.ISubject;
public class DecoratorClient {
public static void main(String[] args) {
ISubject subject = new ConcreteSubject();
ISubject preDecorator = new SubjectPreDecorator(subject);
ISubject postDecorator = new SubjectPostDecorator(preDecorator);
postDecorator.action();
}
}
从上述代码中可以看出,装饰类可装饰的类并不固定,并且被装饰对象是在使用时通过组合确定。如本例中SubjectPreDecorator装饰ConcreteSubject,而SubjectPostDecorator装饰SubjectPreDecorator。并且被装饰对象由调用方实例化后通过构造方法(或者setter)指定。
装饰模式的本质是 动态组合 。动态是手段,组合是目的。每个装饰类可以只负责添加一项额外功能,然后通过组合为被装饰类添加复杂功能。由于每个装饰类的职责比较简单单一,增加了这些装饰类的可重用性,同时也更符合单一职责原则。