Decorator Pattern focuses on dynamically adding functions to an object, while Proxy Pattern focuses on controlling access to an object.
Relationship between a Proxy and the real subject is typically set at compile time, Proxy instantiates it in some way, whereas Decorator is assigned to the subject at runtime, knowing only subject’s interface.
private Subject subject; public(){ //the relationship is set at compile time subject = new RealSubject(); } publicvoiddoAction(){ …. subject.doAction(); …. } }
//client for Decorator publicclassClient{ publicstaticvoidmain(String[] args){ //the client designate which class the decorator decorates Component component = new Decorator(new ConcreteComponent()); … } }
近期评论