Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6ad2c9f

Browse files
authored
Merge pull requestTheAlgorithms#1178 from ali4j/Development
Adding Proxy Design Pattern
2 parents6c8151f +45fee18 commit6ad2c9f

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
packagecom.designpatterns.structural.proxy.president;
2+
3+
importcom.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+
publicclassPresident {
10+
11+
privatevolatilestaticPresidentinstance =null;
12+
13+
privatePresident() {}
14+
15+
staticPresidentgetInstance() {
16+
if (instance ==null) {
17+
synchronized (Singleton.class) {
18+
if (instance ==null) {
19+
instance =newPresident();
20+
}
21+
}
22+
}
23+
returninstance;
24+
}
25+
26+
27+
28+
voidtalkToThePresident(Stringmessage){
29+
System.out.println("President: I have received the message:" +message);
30+
}
31+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
packagecom.designpatterns.structural.proxy.president;
2+
3+
publicclassPresidentSecretary {
4+
5+
privatePresidentpresident;
6+
7+
publicPresidentSecretary() {
8+
this.president =President.getInstance();
9+
}
10+
11+
publicvoidleaveValidMessageForPresident(Stringmessage){
12+
13+
if(!isMessageValid(message))
14+
thrownewRuntimeException("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+
privatebooleanisMessageValid(Stringmessage) {
23+
returnmessage !=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+
packagecom.designpatterns.structural.proxy;
2+
3+
importcom.designpatterns.structural.proxy.president.PresidentSecretary;
4+
importorg.junit.jupiter.api.Assertions;
5+
importorg.junit.jupiter.api.Test;
6+
7+
publicclassCitizen {
8+
9+
10+
privatePresidentSecretarypresidentSecretary =newPresidentSecretary();
11+
12+
@Test
13+
publicvoidtalkToPresident_secretaryShouldRejectTooShortMessage() {
14+
Stringmessage ="Hi there.";
15+
16+
Assertions.assertThrows(RuntimeException.class, () -> {
17+
presidentSecretary.leaveValidMessageForPresident(message);
18+
});
19+
}
20+
21+
@Test
22+
publicvoidtalkToPresident_secretaryShouldRejectTooLongMessage() {
23+
Stringmessage ="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+
publicvoidtalkToPresident_secretaryShouldAcceptTheMessage() {
32+
Stringmessage ="Hello Mr.President";
33+
34+
presidentSecretary.leaveValidMessageForPresident(message);
35+
Assertions.assertTrue(true);
36+
}
37+
38+
39+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp