黑匣子
满天星
Fork me on GitHub

代理模式

Proxy design pattern

Provide a surrogate or placeholder for another object to control access to it.-GOF Design Patterns

Proxy design pattern

provides an object of a class with the functionality of another class with having it. This pattern comes under the structural design pattern of GOF Design Patterns. The intent of this design pattern is to provide an alternate class for another class , along with its functionality, to the outside world.

Purpose of the Proxy pattern

Let’s look at the following points:

  • This pattern hides the actual object from the outside world.
  • This pattern can improve the performance because it is creating an object on demand.

UML structure for the Proxy design pattern

Let’s see the following UML diagram for this pattern:

upload successful

UML diagram for Proxy design pattern

Now let’s look at the different components of this UML diagram:

  • Subject: Actual interface to be implemented by Proxy and RealSubject.
  • RealSubject: Real implementation of Subject. It is a real object that represented by the proxy.
  • Proxy: It is a proxy object and it is also the implementation of the real object Subject. It maintains the references to the real object.

Implementing the Proxy design pattern

Let’s look into following code to demonstrate the Proxy pattern.

Create a Subject.
Following is the Account.java file:

1
2
3
public interface Account {      
void accountType();
}

Create a RealSubject class that implements Subject, let’s see the following class as RealSubject class for the Proxy design pattern.

Following is the SavingAccount.java file:

1
2
3
4
5
6
7
public class SavingAccount implements Account
{
public void accountType() {
System.out.println("SAVING ACCOUNT");

}
}

Create a Proxy class which implements Subject and having the Real Subject

Following is the ProxySavingAccount.java file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.packt.patterninspring.chapter2.proxy.pattern;    
import com.packt.patterninspring.chapter2.model.Account;
import com.packt.patterninspring.chapter2.model.SavingAccount;
public class ProxySavingAccount implements Account{
private Account savingAccount;
public void accountType()
{
if(savingAccount == null)
{
savingAccount = new SavingAccount();

}
savingAccount.accountType();
}


}

Proxy pattern in the Spring Framework

Spring Frameworks ues the Proxy design pattern in the Spring AOP module transparently. As I have discussed in Chapter 1, Getting Started with Spring Framework 5.0 and Design Patterns. In Spring AOP, you create proxies of the object to apply cross cutting concern across the point cut in the Spring application. In the Spring, other modules also implement the Proxy pattern, such as RMI, Spring’s HTTP Invoker, Hessian, and Burlap.Let’s see the next section about Behavioral design pattern with its underlying patterns and example.le.e.

-------------The End-------------

本文标题:代理模式

文章作者:rebortxu

发布时间:2019年07月26日 - 06:07

最后更新:2019年08月01日 - 11:08

原始链接:https://rebortxu.github.io/代理模式.html

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。