Java设计模式之代理模式

PROXY (Object Structural) 
Purpose 
Allows for object level access control by acting as a pass through 
entity or a placeholder object. 
Use When 
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. 
Example 
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. 

Java代码 
  1. package javaPattern.proxy;  
  2.   
  3. public interface Subject {  
  4.     public void request();  
  5. }  
  6. class RealSubject implements Subject{  
  7.   
  8.     @Override  
  9.     public void request() {  
  10.         System.out.println("实际类的方法");  
  11.           
  12.     }  
  13.       
  14. }  
  15. class Proxy implements Subject{  
  16.     private Subject real;  
  17.     public Proxy(Subject real){  
  18.         this.real = real;  
  19.     }  
  20.     @Override  
  21.     public void request() {  
  22.         System.out.println("代理以下方法");  
  23.         real.request();  
  24.           
  25.     }  
  26.       
  27. }  
  28. class Client{  
  29.     public static void main(String[] args) {  
  30.         Proxy proxy = new Proxy(new RealSubject());  
  31.         proxy.request();  
  32.     }  
  33. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值