Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Proxy Pattern

Proxy Pattern

In the Proxy Pattern (Proxy Pattern), one class represents the functionality of another class; this type of design pattern belongs to structural patterns.

The Proxy Pattern controls access to the original object by introducing a proxy object. The proxy object acts as an intermediary between the client and the target object, responsible for forwarding the client's requests to the target object, while it can perform additional processing before and after forwarding the request.

In the Proxy Pattern, we create an object with an existing object in order to provide a functional interface to the outside world.

Introduction

Intent

Provide a proxy for another object to control access to this object.

Main problem solved

Use Cases

Implementation

Key code

Application Example

Advantages

Disadvantages

Usage recommendation

Notes

structures

Mainly involves the following core roles:

- Defines a common interface for the real subject and the proxy subject, so that the proxy subject can be used wherever the real subject is used. - Implements the abstract subject interface and is the real object represented by the proxy object. The client accesses the real subject directly, but in some cases it can be accessed indirectly through the proxy subject. - Implements the abstract subject interface and holds a reference to the real subject. The proxy usually provides some additional functions on top of the real subject, such as lazy loading, access control, logging, etc. - Use the abstract subject interface to operate on the real subject or the proxy subject without needing to know which implementation class it is.

Create a class that implements

We will create a Image interface as the bridge implementation and concrete classes that implement the Image interface.ProxyImage is a proxy class that reduces RealImage the memory usage of object loading.

ProxyPatternDemo Class uses ProxyImage to obtain the object to be Image loaded, and display it as needed.

Step 1

Create an interface.

Image.java

public

interface

Image

{

void

display

(

)

;

}

Step 2

Create concrete classes that implement the interface.

RealImage.java

public

class

RealImage

implements

Image

{

private

String

fileName

;

public

RealImage

(

String

fileName

)

{

this

.

fileName

=

fileName

;

loadFromDisk

(

fileName

)

;

}

@

Override

public

void

display

(

)

{

System

.

out

.

println

(

"

Displaying

"

fileName

)

;

}

private

void

loadFromDisk

(

String

fileName

)

{

System

.

out

.

println

(

"

Loading

"

fileName

)

;

}

}

ProxyImage.java

public

class

ProxyImage

implements

Image

{

private

RealImage

realImage

;

private

String

fileName

;

public

ProxyImage

(

String

fileName

)

{

this

.

fileName

=

fileName

;

}

@

Override

public

void

display

(

)

{

if

(

realImage

==

null

)

{

realImage

=

new

RealImage

(

fileName

)

;

}

realImage

.

display

(

)

;

}

}

Step 3

When requested, use ProxyImage to get RealImage the object of the class.

ProxyPatternDemo.java

public

class

ProxyPatternDemo

{

public

static

void

main

(

String

[

]

args

)

{

Image

image

=

new

ProxyImage

(

"

test_10mb.jpg

"

)

;

//

The image will be loaded from disk

image

.

display

(

)

;

System

.

out

.

println

(

"

"

)

;

//

The image does not need to be loaded from disk

image

.

display

(

)

;

}

}

Step 4

Run the program, output result:



Loading test_10mb.jpg

Displaying test_10mb.jpg



Displaying test_10mb.jpg