From b7f73479148f3c313d46d1299399c10c08026b2d Mon Sep 17 00:00:00 2001 From: Ramazan Sakin Date: Sun, 21 Feb 2021 14:34:05 +0300 Subject: [PATCH 1/4] Sample usage of proxy pattern added --- .../structural/proxy/AuthorizedUser.java | 15 +++++++++++ .../patterns/structural/proxy/GuestUser.java | 15 +++++++++++ .../patterns/structural/proxy/User.java | 10 +++++++ .../patterns/structural/proxy/UserProxy.java | 10 +++++++ .../structural/proxy/UserProxyHandler.java | 26 ++++++++++++++++++ .../structural/proxy/TestProxyPattern.java | 27 +++++++++++++++++++ 6 files changed, 103 insertions(+) create mode 100644 src/main/java/com/gazbert/patterns/structural/proxy/AuthorizedUser.java create mode 100644 src/main/java/com/gazbert/patterns/structural/proxy/GuestUser.java create mode 100644 src/main/java/com/gazbert/patterns/structural/proxy/User.java create mode 100644 src/main/java/com/gazbert/patterns/structural/proxy/UserProxy.java create mode 100644 src/main/java/com/gazbert/patterns/structural/proxy/UserProxyHandler.java create mode 100644 src/test/java/com/gazbert/patterns/structural/proxy/TestProxyPattern.java diff --git a/src/main/java/com/gazbert/patterns/structural/proxy/AuthorizedUser.java b/src/main/java/com/gazbert/patterns/structural/proxy/AuthorizedUser.java new file mode 100644 index 0000000..1b30e83 --- /dev/null +++ b/src/main/java/com/gazbert/patterns/structural/proxy/AuthorizedUser.java @@ -0,0 +1,15 @@ +package com.gazbert.patterns.structural.proxy; + +/** + * Proxy for authorized user + * + * @author ramazansakin + */ +public class AuthorizedUser implements User { + + @Override + public void checkOut() { + System.out.println("User authorized. Logging in..."); + } + +} \ No newline at end of file diff --git a/src/main/java/com/gazbert/patterns/structural/proxy/GuestUser.java b/src/main/java/com/gazbert/patterns/structural/proxy/GuestUser.java new file mode 100644 index 0000000..37bd48b --- /dev/null +++ b/src/main/java/com/gazbert/patterns/structural/proxy/GuestUser.java @@ -0,0 +1,15 @@ +package com.gazbert.patterns.structural.proxy; + +/** + * Proxy for guest user + * + * @author ramazansakin + */ +public class GuestUser implements User { + + @Override + public void checkOut() { + System.out.println("User unauthorized. Delegeting to Registration Page..."); + } + +} \ No newline at end of file diff --git a/src/main/java/com/gazbert/patterns/structural/proxy/User.java b/src/main/java/com/gazbert/patterns/structural/proxy/User.java new file mode 100644 index 0000000..cad974a --- /dev/null +++ b/src/main/java/com/gazbert/patterns/structural/proxy/User.java @@ -0,0 +1,10 @@ +package com.gazbert.patterns.structural.proxy; + +/** + * User Interface + * + * @author ramazansakin + */ +public interface User { + void checkOut(); +} \ No newline at end of file diff --git a/src/main/java/com/gazbert/patterns/structural/proxy/UserProxy.java b/src/main/java/com/gazbert/patterns/structural/proxy/UserProxy.java new file mode 100644 index 0000000..bf4a647 --- /dev/null +++ b/src/main/java/com/gazbert/patterns/structural/proxy/UserProxy.java @@ -0,0 +1,10 @@ +package com.gazbert.patterns.structural.proxy; + +/** + * User Proxy Interface + * + * @author ramazansakin + */ +public interface UserProxy { + User getUser(); +} diff --git a/src/main/java/com/gazbert/patterns/structural/proxy/UserProxyHandler.java b/src/main/java/com/gazbert/patterns/structural/proxy/UserProxyHandler.java new file mode 100644 index 0000000..361799c --- /dev/null +++ b/src/main/java/com/gazbert/patterns/structural/proxy/UserProxyHandler.java @@ -0,0 +1,26 @@ +package com.gazbert.patterns.structural.proxy; + +/** + * Authorization handling via user proxy + * + * @author ramazansakin + */ +public class UserProxyHandler implements UserProxy{ + + private boolean isLoggedIn; + + public UserProxyHandler(boolean isLoggedIn) { + this.isLoggedIn = isLoggedIn; + } + + @Override + public User getUser() { + User user; + if (isLoggedIn) { + user = new AuthorizedUser(); + } else { + user = new GuestUser(); + } + return user; + } +} \ No newline at end of file diff --git a/src/test/java/com/gazbert/patterns/structural/proxy/TestProxyPattern.java b/src/test/java/com/gazbert/patterns/structural/proxy/TestProxyPattern.java new file mode 100644 index 0000000..b1f7b87 --- /dev/null +++ b/src/test/java/com/gazbert/patterns/structural/proxy/TestProxyPattern.java @@ -0,0 +1,27 @@ +package com.gazbert.patterns.structural.proxy; + +import org.junit.Test; + +/** + * Demonstrates use of Proxy pattern. + * + * @author ramazansakin + */ +public class TestProxyPattern { + + /** + * Detecting authorized user via proxy sample test + * + * @author ramazansakin + */ + @Test + public void testUserProxy() { + + User authUser = new AuthorizedUser(); + authUser.checkOut(); + + User guest = new GuestUser(); + guest.checkOut(); + } + +} From d7f08174e4662c407f9546d99e7b7c98f3c71149 Mon Sep 17 00:00:00 2001 From: Ramazan Sakin Date: Sun, 21 Feb 2021 22:31:57 +0300 Subject: [PATCH 2/4] Update .travis.yml --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 4760fea..5d180da 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,3 +1,3 @@ language: java jdk: - - oraclejdk8 \ No newline at end of file + - oraclejdk9 From a4f125c4a64d392f6decbd595a3cbd312cc07e0c Mon Sep 17 00:00:00 2001 From: Ramazan Sakin Date: Sun, 21 Feb 2021 22:33:52 +0300 Subject: [PATCH 3/4] Update .travis.yml --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 5d180da..66f6e2d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,3 +1,3 @@ language: java jdk: - - oraclejdk9 + - oraclejdk11 From b15f7c40cd6bb1582f4936a443deb77c56db55d2 Mon Sep 17 00:00:00 2001 From: Ramazan Sakin Date: Sun, 21 Feb 2021 22:36:42 +0300 Subject: [PATCH 4/4] Update .travis.yml --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 66f6e2d..16205ac 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,3 +1,3 @@ language: java jdk: - - oraclejdk11 + - openjdk8