Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sample usage of proxy pattern added #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
language: java
jdk:
- oraclejdk8
- openjdk8
Original file line number Diff line number Diff line change
@@ -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...");
}

}
15 changes: 15 additions & 0 deletions src/main/java/com/gazbert/patterns/structural/proxy/GuestUser.java
Original file line number Diff line number Diff line change
@@ -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...");
}

}
10 changes: 10 additions & 0 deletions src/main/java/com/gazbert/patterns/structural/proxy/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.gazbert.patterns.structural.proxy;

/**
* User Interface
*
* @author ramazansakin
*/
public interface User {
void checkOut();
}
10 changes: 10 additions & 0 deletions src/main/java/com/gazbert/patterns/structural/proxy/UserProxy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.gazbert.patterns.structural.proxy;

/**
* User Proxy Interface
*
* @author ramazansakin
*/
public interface UserProxy {
User getUser();
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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();
}

}