设计模式12 - 代理模式 Proxy


Proxy design pattern allows you to create a wrapper class over real object.Wrapper class which is proxy,controls access to real object so in turn you can add extra functionalities to real object without changing real object's code.

As described by GoF:

"Provide a surrogate or placeholder for another object to control access over it."

Real life example may be proxy server used in IT companies for internet access.It blocks access to social networking sites like facebook,twitter and other contents which are not work related.

When to use it:

Proxy is required whenever there is need for more sophisticated or versatile reference to an object than a simple pointer.Here are some situations when proxy pattern is applicable.

  1. remote proxy provides a local representative for an object in a different address space.Providing interface for remote resources such as web service or REST resources.
  2. virtual proxy creates expensive object on demand.
  3. protection proxy controls access to the original object.Protection proxies are useful when objects should have different access rights.
  4. smart reference is a replacement for a bare pointer that performs additional actions when an object is accessed.
  5. Adding a thread-safe feature to an existing class without changing the existing class's code. 

Elements:

  • Proxy
    • maintains a reference that lets the proxy access the real subject.Proxy may refer to a subject if the RealSubject and Subject interface are the same.
    • provides an interface identical to Subject's so that proxy can be substituted for the real subject.
    • controls access to the real subject and may be responsible for creating and deleting it.
  • Subject
    • defines the common interface for RealSubject and proxy so that a Proxy can be used anywhere a RealSubject is expected.
  • RealSubject
    • defines the real object that proxy represents. 

WorkFlow:

Proxy forwards request to RealSubject when appropriate ,depending on the kind of proxy.

Example: 

Let's see an example.I am taking example of protection proxy. In our example, we have a folder in which you can perform various operation such as copy,paste file or subfolder.In OOP terms,we have IFolder interface and Folder class which provides performOperatons() method, and these are the existing class and interface that we cannot change. We want to further specify that only user with authorization can access it and perform operations such as cut or copy files and sub folder.

Comparing with above generic elements,we have

  • FolderProxy(Proxy)
  • Folder(RealSubject)
  • IFolder(Subject)

Java codes for above classes:

IFolder.java (Subject):

public interface IFolder {
    public void performOperations();                                                                     
}

Following class is our realSubject class.Lets say we can not change it but we want to provide authorization on it.

Folder.java (RealSubject):

public class Folder implements IFolder {
    public void performOperations() {                                                                    
        //Access folder and perform various operations like copy or cut files
        System.out.println("Performing operation on folder...");
    }
}


Following class provides authorization to Folder class.It checks for userName and password and if matched then only it gives access to folder.

FolderProxy.java(Proxy):

public class FolderProxy implements IFolder {
    Folder folder;
    User user;

    public FolderProxy(User user) {
        this.user = user;
    }

    public void performOperations() {
        if (user.getUserName().equals("songuo") &&                                                       
            user.getPassword().equals("123")) {
            folder = new Folder();
            folder.performOperations();
        } else {
            System.out.println("You do not have access to this folder");
        }
    }
}

User.java:

public class User {
    private String userName;
    private String password;

    public User(String name, String pass) {
        userName = name;
        password = pass;
    }
    public String getUserName() {
        return userName;
    }
    public String getPassword() {
        return password;                                                                                 
    }
}

FolderProxyTest.java 

public class FolderProxyTest {                                                                           
    public static void main(String[] args) {
        //When you click on folder,Lets say a GUI form will ask for userName and password.
        //and this GUI will create this user object

        // If we give correct userName and password
        User user = new User("songuo", "123");
        FolderProxy folderProxy = new FolderProxy(user);
        System.out.println("When userName and password are correct:");
        folderProxy.performOperations();

         // if we give wrong userName and Password
         User userWrong=new User("abc","abc");
         FolderProxy folderProxyWrong=new FolderProxy(userWrong);
         System.out.println("When userName and password are incorrect:");
         folderProxyWrong.performOperations();
    }
}

Output:







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值