Java設計模式之代理模式

[color=red]PROXY[/color] (Object Structural)
[color=red]Purpose[/color]
Allows for object level access control by acting as a pass through
entity or a placeholder object.
[color=red]Use When[/color]
1 The object being represented is external to the system.
2 Objects need to be created on demand.
3 Access control for the original object is required.
4 Added functionality is required when an object is accessed.
[color=red]Example[/color]
Ledger applications often provide a way for users to reconcile
their bank statements with their ledger data on demand, automating
much of the process. The actual operation of communicating
with a third party is a relatively expensive operation that should be
limited. By using a proxy to represent the communications object
we can limit the number of times or the intervals the communication
is invoked. In addition, we can wrap the complex instantiation
of the communication object inside the proxy class, decoupling
calling code from the implementation details.

package javaPattern.proxy;

public interface Subject {
public void request();
}
class RealSubject implements Subject{

@Override
public void request() {
System.out.println("實際類的方法");

}

}
class Proxy implements Subject{
private Subject real;
public Proxy(Subject real){
this.real = real;
}
@Override
public void request() {
System.out.println("代理以下方法");
real.request();

}

}
class Client{
public static void main(String[] args) {
Proxy proxy = new Proxy(new RealSubject());
proxy.request();
}
}
發佈了40 篇原創文章 · 獲贊 0 · 訪問量 840
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章