Skip to content

Commit 5d79026

Browse files
committed
adds proxy design pattern.
1 parent 6c8151f commit 5d79026

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.designpatterns.structural.proxy;
2+
3+
import com.designpatterns.structural.proxy.president.PresidentSecretary;
4+
5+
public class Citizen {
6+
7+
public static void main(String[] args) {
8+
PresidentSecretary presidentSecretary = new PresidentSecretary();
9+
presidentSecretary.leaveValidMessageForPresident("Hello president.");
10+
}
11+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.designpatterns.structural.proxy.president;
2+
3+
import com.designpatterns.creational.singleton.Singleton;
4+
5+
public class President {
6+
7+
private volatile static President instance = null;
8+
9+
private President() {}
10+
11+
static President getInstance() {
12+
if (instance == null) {
13+
synchronized (Singleton.class) {
14+
if (instance == null) {
15+
instance = new President();
16+
}
17+
}
18+
}
19+
return instance;
20+
}
21+
22+
23+
24+
void talkToThePresident(String message){
25+
System.out.println("I, the President, have received this message:" + message);
26+
}
27+
}
Lines changed: 25 additions & 0 deletions
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("message is being sent to the President...");
17+
president.talkToThePresident(message);
18+
System.out.println("message is received by 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+
}
Lines changed: 39 additions & 0 deletions
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)