Proxy mode:
For some reason you need an object to provide a proxy to control access to the object. In this case, the object does not fit or can not access a direct reference to the target object, a proxy object as an intermediary between the access and target objects.
(Can add that method before and after class method)
main role delegation model are as follows:
- Abstract topics (Subject) categories: real business methods and themes proxy object achieved by the interface or abstract class declaration.
- Real theme (Real Subject) category: abstract achieve specific business topics, real object is represented by a proxy object, the object is to be the ultimate reference.
- Proxy (Proxy) class: provides the same interface with the real theme, the interior contains a reference to the real subject, it can access, control, or extend the functionality of the real theme.
package proxy;
public class ProxyTest
{
public static void main(String[] args)
{
Proxy proxy=new Proxy();
proxy.Request();
}
}
//抽象主题
interface Subject
{
void Request();
}
//真实主题
class RealSubject implements Subject
{
public void Request()
{
System.out.println("访问真实主题方法...");
}
}
//代理
class Proxy implements Subject
{
private RealSubject realSubject;
public void Request()
{
if (realSubject==null)
{
realSubject=new RealSubject();
}
preRequest();
realSubject.Request();
postRequest();
}
public void preRequest()
{
System.out.println("访问真实主题之前的预处理。");
}
public void postRequest()
{
System.out.println("访问真实主题之后的后续处理。");
}
}
Decorative patterns:
Decorative pattern mainly includes the following roles.
- Abstract component (Component) Roles: define an abstract interface to regulate an object ready to receive additional responsibilities.
- Specific components (Concrete Component) role: to achieve abstract components, add some of its responsibilities by decorative role.
- Abstract decorative (the Decorator) Role: inherit the abstract member, and includes examples of specific member, the specific member function can be extended by its subclasses.
- Specific decoration (ConcreteDecorator) Role: implementation-dependent method abstract decoration, and add additional responsibilities to specific member objects.
package decorator;
public class DecoratorPattern
{
public static void main(String[] args)
{
Component p=new ConcreteComponent();
p.operation();
System.out.println("---------------------------------");
Component d=new ConcreteDecorator(p);
d.operation();
}
}
//抽象构件角色
interface Component
{
public void operation();
}
//具体构件角色
class ConcreteComponent implements Component
{
public ConcreteComponent()
{
System.out.println("创建具体构件角色");
}
public void operation()
{
System.out.println("调用具体构件角色的方法operation()");
}
}
//抽象装饰角色
class Decorator implements Component
{
private Component component;
public Decorator(Component component)
{
this.component=component;
}
public void operation()
{
component.operation();
}
}
//具体装饰角色
class ConcreteDecorator extends Decorator
{
public ConcreteDecorator(Component component)
{
super(component);
}
public void operation()
{
super.operation();
addedFunction();
}
public void addedFunction()
{
System.out.println("为具体构件角色增加额外的功能addedFunction()");
}
}
Decorative mode and proxy mode difference: attention to the dynamic decorative patterns Add method on an object; but proxy mode is more concerned about control access to objects that are hidden object specific information.