diff --git a/.travis.yml b/.travis.yml index 4760fea..16205ac 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,3 +1,3 @@ language: java jdk: - - oraclejdk8 \ No newline at end of file + - openjdk8 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(); + } + +}