File tree Expand file tree Collapse file tree 3 files changed +95
-0
lines changed
main/java/com/designpatterns/structural/proxy/president
test/java/com/designpatterns/structural/proxy Expand file tree Collapse file tree 3 files changed +95
-0
lines changed Original file line number Diff line number Diff line change 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 number Diff line number Diff line change 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 number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments