Skip to content

Adding Proxy Design Pattern #1178

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

Merged
merged 3 commits into from
Nov 28, 2019
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
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;

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("President: I have received the message:" + message);
}
}
Original file line number Diff line number Diff line change
@@ -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("Secretary: message is being sent to the President...");
president.talkToThePresident(message);
System.out.println("Secretary: message is sent to the President.");

}

private boolean isMessageValid(String message) {
return message != null && !message.isEmpty() && message.length() >= 10 && message.length() <= 100;
}
}
39 changes: 39 additions & 0 deletions src/test/java/com/designpatterns/structural/proxy/Citizen.java
Original file line number Diff line number Diff line change
@@ -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);
}


}