From 5d790266e7faa7113e3a308ac74ca169ef324351 Mon Sep 17 00:00:00 2001 From: ali4j Date: Sun, 3 Nov 2019 18:05:40 +0330 Subject: [PATCH 1/3] adds proxy design pattern. --- .../structural/proxy/Citizen.java | 11 ++++++ .../structural/proxy/president/President.java | 27 +++++++++++++ .../proxy/president/PresidentSecretary.java | 25 ++++++++++++ .../structural/proxy/Citizen.java | 39 +++++++++++++++++++ 4 files changed, 102 insertions(+) create mode 100644 src/main/java/com/designpatterns/structural/proxy/Citizen.java create mode 100644 src/main/java/com/designpatterns/structural/proxy/president/President.java create mode 100644 src/main/java/com/designpatterns/structural/proxy/president/PresidentSecretary.java create mode 100644 src/test/java/com/designpatterns/structural/proxy/Citizen.java diff --git a/src/main/java/com/designpatterns/structural/proxy/Citizen.java b/src/main/java/com/designpatterns/structural/proxy/Citizen.java new file mode 100644 index 00000000..3f34e5f7 --- /dev/null +++ b/src/main/java/com/designpatterns/structural/proxy/Citizen.java @@ -0,0 +1,11 @@ +package com.designpatterns.structural.proxy; + +import com.designpatterns.structural.proxy.president.PresidentSecretary; + +public class Citizen { + + public static void main(String[] args) { + PresidentSecretary presidentSecretary = new PresidentSecretary(); + presidentSecretary.leaveValidMessageForPresident("Hello president."); + } +} diff --git a/src/main/java/com/designpatterns/structural/proxy/president/President.java b/src/main/java/com/designpatterns/structural/proxy/president/President.java new file mode 100644 index 00000000..18d0e31c --- /dev/null +++ b/src/main/java/com/designpatterns/structural/proxy/president/President.java @@ -0,0 +1,27 @@ +package com.designpatterns.structural.proxy.president; + +import com.designpatterns.creational.singleton.Singleton; + +public class President { + + private volatile static President instance = null; + + private President() {} + + static President getInstance() { + if (instance == null) { + synchronized (Singleton.class) { + if (instance == null) { + instance = new President(); + } + } + } + return instance; + } + + + + void talkToThePresident(String message){ + System.out.println("I, the President, have received this message:" + message); + } +} diff --git a/src/main/java/com/designpatterns/structural/proxy/president/PresidentSecretary.java b/src/main/java/com/designpatterns/structural/proxy/president/PresidentSecretary.java new file mode 100644 index 00000000..fd90a0b0 --- /dev/null +++ b/src/main/java/com/designpatterns/structural/proxy/president/PresidentSecretary.java @@ -0,0 +1,25 @@ +package com.designpatterns.structural.proxy.president; + +public class PresidentSecretary { + + private President president; + + public PresidentSecretary() { + this.president = President.getInstance(); + } + + public void leaveValidMessageForPresident(String message){ + + if(!isMessageValid(message)) + throw new RuntimeException("invalid message"); + + System.out.println("message is being sent to the President..."); + president.talkToThePresident(message); + System.out.println("message is received by the President."); + + } + + private boolean isMessageValid(String message) { + return message != null && !message.isEmpty() && message.length() >= 10 && message.length() <= 100; + } +} diff --git a/src/test/java/com/designpatterns/structural/proxy/Citizen.java b/src/test/java/com/designpatterns/structural/proxy/Citizen.java new file mode 100644 index 00000000..7f2fc459 --- /dev/null +++ b/src/test/java/com/designpatterns/structural/proxy/Citizen.java @@ -0,0 +1,39 @@ +package com.designpatterns.structural.proxy; + +import com.designpatterns.structural.proxy.president.PresidentSecretary; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class Citizen { + + + private PresidentSecretary presidentSecretary = new PresidentSecretary(); + + @Test + public void talkToPresident_secretaryShouldRejectTooShortMessage() { + String message = "Hi there."; + + Assertions.assertThrows(RuntimeException.class, () -> { + presidentSecretary.leaveValidMessageForPresident(message); + }); + } + + @Test + public void talkToPresident_secretaryShouldRejectTooLongMessage() { + String message = "Hi there. this is a message about some personal issue which I have decided to share with Mr.President."; + + Assertions.assertThrows(RuntimeException.class, () -> { + presidentSecretary.leaveValidMessageForPresident(message); + }); + } + + @Test + public void talkToPresident_secretaryShouldAcceptTheMessage() { + String message = "Hello Mr.President"; + + presidentSecretary.leaveValidMessageForPresident(message); + Assertions.assertTrue(true); + } + + +} From e30d11545f7ad66af401a7e843cf2a30727aa93e Mon Sep 17 00:00:00 2001 From: Ehsan Date: Fri, 29 Nov 2019 02:59:56 +0330 Subject: [PATCH 2/3] removes the Citizen class in src directory. changes messages printed by the secretary and the president. --- .../com/designpatterns/structural/proxy/Citizen.java | 11 ----------- .../structural/proxy/president/President.java | 2 +- .../proxy/president/PresidentSecretary.java | 4 ++-- 3 files changed, 3 insertions(+), 14 deletions(-) delete mode 100644 src/main/java/com/designpatterns/structural/proxy/Citizen.java diff --git a/src/main/java/com/designpatterns/structural/proxy/Citizen.java b/src/main/java/com/designpatterns/structural/proxy/Citizen.java deleted file mode 100644 index 3f34e5f7..00000000 --- a/src/main/java/com/designpatterns/structural/proxy/Citizen.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.designpatterns.structural.proxy; - -import com.designpatterns.structural.proxy.president.PresidentSecretary; - -public class Citizen { - - public static void main(String[] args) { - PresidentSecretary presidentSecretary = new PresidentSecretary(); - presidentSecretary.leaveValidMessageForPresident("Hello president."); - } -} diff --git a/src/main/java/com/designpatterns/structural/proxy/president/President.java b/src/main/java/com/designpatterns/structural/proxy/president/President.java index 18d0e31c..50b712b4 100644 --- a/src/main/java/com/designpatterns/structural/proxy/president/President.java +++ b/src/main/java/com/designpatterns/structural/proxy/president/President.java @@ -22,6 +22,6 @@ public class President { void talkToThePresident(String message){ - System.out.println("I, the President, have received this message:" + message); + System.out.println("President: I have received the message:" + message); } } diff --git a/src/main/java/com/designpatterns/structural/proxy/president/PresidentSecretary.java b/src/main/java/com/designpatterns/structural/proxy/president/PresidentSecretary.java index fd90a0b0..fb951ed9 100644 --- a/src/main/java/com/designpatterns/structural/proxy/president/PresidentSecretary.java +++ b/src/main/java/com/designpatterns/structural/proxy/president/PresidentSecretary.java @@ -13,9 +13,9 @@ public class PresidentSecretary { if(!isMessageValid(message)) throw new RuntimeException("invalid message"); - System.out.println("message is being sent to the President..."); + System.out.println("Secretary: message is being sent to the President..."); president.talkToThePresident(message); - System.out.println("message is received by the President."); + System.out.println("Secretary: message is sent to the President."); } From 45fee1868e3f6c67aa74d344811dc1e69edf1d86 Mon Sep 17 00:00:00 2001 From: Ehsan Date: Fri, 29 Nov 2019 03:03:25 +0330 Subject: [PATCH 3/3] adds commit to the President class. --- .../designpatterns/structural/proxy/president/President.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/com/designpatterns/structural/proxy/president/President.java b/src/main/java/com/designpatterns/structural/proxy/president/President.java index 50b712b4..2b3f35ea 100644 --- a/src/main/java/com/designpatterns/structural/proxy/president/President.java +++ b/src/main/java/com/designpatterns/structural/proxy/president/President.java @@ -2,6 +2,10 @@ package com.designpatterns.structural.proxy.president; import com.designpatterns.creational.singleton.Singleton; +/** + * This is a class which is gonna be proxied by PresidentSecretary. + * Whenever any citizen decides to contact the President, they have to talk to the Secretary. + */ public class President { private volatile static President instance = null;