Skip to content

Commit fb31ba2

Browse files
authored
Merge pull request dubesar#592 from cdhiraj40/ProxyPattern
Added Proxy Pattern
2 parents dddccf9 + f81715e commit fb31ba2

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
### Proxy Pattern
2+
3+
### Introduction
4+
5+
In proxy pattern, a class represents functionality of another class. This type of design pattern comes under structural pattern.
6+
Proxy pattern is used when we need to create a wrapper to cover the main object’s complexity from the client.
7+
In proxy pattern, we create object having original object to interface its functionality to outer world.
8+
9+
### Advantages
10+
11+
- One of the advantages of Proxy pattern is security.
12+
13+
- This pattern avoids duplication of objects which might be huge size and memory intensive. This in turn increases the performance of the application.
14+
15+
- The remote proxy also ensures about security by installing the local code proxy (stub) in the client machine and then accessing the server with help of the remote code.
16+
17+
### Disadvantages
18+
19+
- This pattern introduces another layer of abstraction which sometimes may be an issue if the RealSubject code is accessed by some of the clients directly and some of them might access the Proxy classes. This might cause disparate behaviour.
20+
21+
### Programming
22+
23+
- We are going to create an Image interface and concrete classes implementing the Image interface.
24+
25+
- ProxyImage is a a proxy class to reduce memory footprint of RealImage object loading.
26+
27+
- ProxyPatternDemo, our demo class, will use ProxyImage to get an Image object to load and display as it needs.
28+
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
Reference: https://www.tutorialspoint.com/design_pattern/proxy_pattern.htm
3+
*/
4+
5+
6+
interface Image {
7+
void display();
8+
}
9+
class RealImage implements Image {
10+
11+
private String fileName;
12+
13+
public RealImage(String fileName){
14+
this.fileName = fileName;
15+
loadFromDisk(fileName);
16+
}
17+
18+
@Override
19+
public void display() {
20+
System.out.println("Displaying " + fileName);
21+
}
22+
23+
private void loadFromDisk(String fileName){
24+
System.out.println("Loading " + fileName);
25+
}
26+
}
27+
class ProxyImage implements Image{
28+
29+
private RealImage realImage;
30+
private String fileName;
31+
32+
public ProxyImage(String fileName){
33+
this.fileName = fileName;
34+
}
35+
36+
@Override
37+
public void display() {
38+
if(realImage == null){
39+
realImage = new RealImage(fileName);
40+
}
41+
realImage.display();
42+
}
43+
}
44+
public class ProxyPatternDemo {
45+
46+
public static void main(String[] args) {
47+
Image image = new ProxyImage("test_10mb.jpg");
48+
49+
//image will be loaded from disk
50+
image.display();
51+
System.out.println("");
52+
53+
//image will not be loaded from disk
54+
image.display();
55+
}
56+
}
57+
58+

0 commit comments

Comments
 (0)