Skip to content

Commit 6ad2c9f

Browse files
authored
Merge pull request #1178 from ali4j/Development
Adding Proxy Design Pattern
2 parents 6c8151f + 45fee18 commit 6ad2c9f

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.designpatterns.structural.proxy.president;
2+
3+
import com.designpatterns.creational.singleton.Singleton;
4+
5+
/**
6+
* This is a class which is gonna be proxied by PresidentSecretary.
7+
* Whenever any citizen decides to contact the President, they have to talk to the Secretary.
8+
*/
9+
public class President {
10+
11+
private volatile static President instance = null;
12+
13+
private President() {}
14+
15+
static President getInstance() {
16+
if (instance == null) {
17+
synchronized (Singleton.class) {
18+
if (instance == null) {
19+
instance = new President();
20+
}
21+
}
22+
}
23+
return instance;
24+
}
25+
26+
27+
28+
void talkToThePresident(String message){
29+
System.out.println("President: I have received the message:" + message);
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.designpatterns.structural.proxy.president;
2+
3+
public class PresidentSecretary {
4+
5+
private President president;
6+
7+
public PresidentSecretary() {
8+
this.president = President.getInstance();
9+
}
10+
11+
public void leaveValidMessageForPresident(String message){
12+
13+
if(!isMessageValid(message))
14+
throw new RuntimeException("invalid message");
15+
16+
System.out.println("Secretary: message is being sent to the President...");
17+
president.talkToThePresident(message);
18+
System.out.println("Secretary: message is sent to the President.");
19+
20+
}
21+
22+
private boolean isMessageValid(String message) {
23+
return message != null && !message.isEmpty() && message.length() >= 10 && message.length() <= 100;
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.designpatterns.structural.proxy;
2+
3+
import com.designpatterns.structural.proxy.president.PresidentSecretary;
4+
import org.junit.jupiter.api.Assertions;
5+
import org.junit.jupiter.api.Test;
6+
7+
public class Citizen {
8+
9+
10+
private PresidentSecretary presidentSecretary = new PresidentSecretary();
11+
12+
@Test
13+
public void talkToPresident_secretaryShouldRejectTooShortMessage() {
14+
String message = "Hi there.";
15+
16+
Assertions.assertThrows(RuntimeException.class, () -> {
17+
presidentSecretary.leaveValidMessageForPresident(message);
18+
});
19+
}
20+
21+
@Test
22+
public void talkToPresident_secretaryShouldRejectTooLongMessage() {
23+
String message = "Hi there. this is a message about some personal issue which I have decided to share with Mr.President.";
24+
25+
Assertions.assertThrows(RuntimeException.class, () -> {
26+
presidentSecretary.leaveValidMessageForPresident(message);
27+
});
28+
}
29+
30+
@Test
31+
public void talkToPresident_secretaryShouldAcceptTheMessage() {
32+
String message = "Hello Mr.President";
33+
34+
presidentSecretary.leaveValidMessageForPresident(message);
35+
Assertions.assertTrue(true);
36+
}
37+
38+
39+
}

0 commit comments

Comments
 (0)