Skip to content

Commit bba5a99

Browse files
committed
Merge branch 'feature/design-pattern-v2.6.0-ft-211014-implement-proxy-pattern' into develop
2 parents 40e8742 + 32a2f85 commit bba5a99

File tree

10 files changed

+143
-4
lines changed

10 files changed

+143
-4
lines changed

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ Reference: https://gpcoder.com/4554-huong-dan-java-design-pattern-composite/
5656
Reference: https://gpcoder.com/4574-huong-dan-java-design-pattern-decorator/
5757

5858
> :warning: Note: <br/>
59-
> In this code, we can only add new features to the object. We cannot remove the previous features of this object.<br/>
60-
> _Example_: Manager becomes TeamLeader, TeamLeader still doTask of Manager. **This is Problem**
59+
> In this code, we can only add new features to the object. We cannot remove the previous features of this object.<br/> > _Example_: Manager becomes TeamLeader, TeamLeader still doTask of Manager. **This is Problem**
6160
6261
### Facade
6362

@@ -66,4 +65,11 @@ Reference: https://gpcoder.com/4604-huong-dan-java-design-pattern-facade/
6665
### Flyweight
6766

6867
Reference: https://gpcoder.com/4626-huong-dan-java-design-pattern-flyweight/
69-
Review: Good for create character in game
68+
69+
> Review: Good for create character in game
70+
71+
### Proxy
72+
73+
https://gpcoder.com/4644-huong-dan-java-design-pattern-proxy/
74+
75+
> Review: Useful for lazy loading

design-pattern/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.phint2</groupId>
88
<artifactId>design-pattern</artifactId>
9-
<version>2.5.0</version>
9+
<version>2.6.0</version>
1010

1111
<properties>
1212
<maven.compiler.source>8</maven.compiler.source>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.phint2.pattern.structural.proxy.protection;
2+
3+
public class Client {
4+
5+
public static void main(String[] args) {
6+
UserService admin = new UserServiceProxy("phint", "admin");
7+
admin.load();
8+
admin.insert();
9+
10+
UserService customer = new UserServiceProxy("customer", "guest");
11+
customer.load();
12+
customer.insert();
13+
}
14+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.phint2.pattern.structural.proxy.protection;
2+
3+
public interface UserService {
4+
5+
void load();
6+
void insert();
7+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.phint2.pattern.structural.proxy.protection;
2+
3+
public class UserServiceImpl implements UserService {
4+
5+
private String name;
6+
7+
public UserServiceImpl(String name) {
8+
this.name = name;
9+
}
10+
11+
@Override
12+
public void load() {
13+
System.out.println(name + " loaded");
14+
}
15+
16+
@Override
17+
public void insert() {
18+
System.out.println(name + " inserted");
19+
}
20+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.phint2.pattern.structural.proxy.protection;
2+
3+
public class UserServiceProxy implements UserService {
4+
5+
private static final String ADMIN = "admin";
6+
private String role;
7+
private UserService userService;
8+
9+
public UserServiceProxy(String name, String role) {
10+
this.role = role;
11+
this.userService = new UserServiceImpl(name);
12+
}
13+
14+
@Override
15+
public void load() {
16+
userService.load();
17+
}
18+
19+
@Override
20+
public void insert() {
21+
if (isAdmin()) {
22+
userService.insert();
23+
} else {
24+
throw new IllegalAccessError("Access denied");
25+
}
26+
}
27+
28+
private boolean isAdmin() {
29+
return ADMIN.equalsIgnoreCase(this.role);
30+
}
31+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.phint2.pattern.structural.proxy.virtual;
2+
3+
public class Client {
4+
5+
public static void main(String[] args) {
6+
System.out.println("Init proxy image: ");
7+
ProxyImage proxyImage = new ProxyImage("abc.com");
8+
9+
System.out.println("---");
10+
System.out.println("Call real service 1st: ");
11+
proxyImage.showImage();
12+
13+
System.out.println("---");
14+
System.out.println("Call real service 2nd: ");
15+
proxyImage.showImage();
16+
}
17+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.phint2.pattern.structural.proxy.virtual;
2+
3+
public interface Image {
4+
5+
void showImage();
6+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.phint2.pattern.structural.proxy.virtual;
2+
3+
public class ProxyImage implements Image {
4+
5+
private RealImage realImage;
6+
private String url;
7+
8+
public ProxyImage(String url) {
9+
this.url = url;
10+
System.out.println("Image unloaded: " + this.url);
11+
}
12+
13+
@Override
14+
public void showImage() {
15+
if (null == realImage) {
16+
realImage = new RealImage(this.url);
17+
} else {
18+
System.out.println("Image already existed: " + this.url);
19+
}
20+
realImage.showImage();
21+
}
22+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.phint2.pattern.structural.proxy.virtual;
2+
3+
public class RealImage implements Image {
4+
5+
private String url;
6+
7+
public RealImage(String url) {
8+
this.url = url;
9+
System.out.println("Image loaded: " + this.url);
10+
}
11+
12+
@Override
13+
public void showImage() {
14+
System.out.println("Image showed: " + this.url);
15+
}
16+
}

0 commit comments

Comments
 (0)