Skip to content

Commit

Permalink
Chain Of Responsibility
Browse files Browse the repository at this point in the history
  • Loading branch information
dhsim86 committed Nov 12, 2019
1 parent 82fe1b2 commit 03499d6
Show file tree
Hide file tree
Showing 10 changed files with 204 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.dongho.df.domain.behavioral.chain_of_responsibility;

public class ResultSet {

private String value;

public ResultSet(String value) {
this.value = value;
}

public String getValue() {
return value;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.dongho.df.domain.behavioral.chain_of_responsibility.handler;

import com.dongho.df.domain.behavioral.chain_of_responsibility.ResultSet;
import com.dongho.df.domain.behavioral.chain_of_responsibility.request.Request;
import com.dongho.df.domain.behavioral.chain_of_responsibility.request.RequestType;

public class AnchorChainHandler extends ChainHandler {

@Override
public ResultSet handleRequest(Request request) {
if (request.getRequestType().equals(RequestType.ANCHOR_CLICK)) {
return new ResultSet(RequestType.ANCHOR_CLICK.name());
}
return delegateRequest(request);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.dongho.df.domain.behavioral.chain_of_responsibility.handler;

import com.dongho.df.domain.behavioral.chain_of_responsibility.ResultSet;
import com.dongho.df.domain.behavioral.chain_of_responsibility.request.Request;
import com.dongho.df.domain.behavioral.chain_of_responsibility.request.RequestType;

public class ButtonChainHandler extends ChainHandler {

@Override
public ResultSet handleRequest(Request request) {
if (request.getRequestType().equals(RequestType.BUTTON_CLICK)) {
return new ResultSet(RequestType.BUTTON_CLICK.name());
}
return delegateRequest(request);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.dongho.df.domain.behavioral.chain_of_responsibility.handler;

import com.dongho.df.domain.behavioral.chain_of_responsibility.ResultSet;
import com.dongho.df.domain.behavioral.chain_of_responsibility.request.Request;

public abstract class ChainHandler {

private ChainHandler chainHandler = null;

public abstract ResultSet handleRequest(Request request);

public void linkDelegator(ChainHandler handler) {
chainHandler = handler;
}

protected ResultSet delegateRequest(Request request) {
if (chainHandler == null) {
return new ResultSet("");
}

return chainHandler.handleRequest(request);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.dongho.df.domain.behavioral.chain_of_responsibility.request;

public class AnchorRequest implements Request {

@Override
public RequestType getRequestType() {
return RequestType.ANCHOR_CLICK;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.dongho.df.domain.behavioral.chain_of_responsibility.request;

public class ButtonRequest implements Request {

@Override
public RequestType getRequestType() {
return RequestType.BUTTON_CLICK;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.dongho.df.domain.behavioral.chain_of_responsibility.request;

public class LinkRequest implements Request {

@Override
public RequestType getRequestType() {
return RequestType.LINK_CLICK;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.dongho.df.domain.behavioral.chain_of_responsibility.request;

public interface Request {

RequestType getRequestType();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.dongho.df.domain.behavioral.chain_of_responsibility.request;

public enum RequestType {

BUTTON_CLICK,
ANCHOR_CLICK,
LINK_CLICK

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.dongho.df.domain.behavioral.chain_of_responsibility;

import com.dongho.df.domain.behavioral.chain_of_responsibility.handler.AnchorChainHandler;
import com.dongho.df.domain.behavioral.chain_of_responsibility.handler.ButtonChainHandler;
import com.dongho.df.domain.behavioral.chain_of_responsibility.handler.ChainHandler;
import com.dongho.df.domain.behavioral.chain_of_responsibility.request.*;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import static org.assertj.core.api.Assertions.assertThat;

@RunWith(JUnit4.class)
public class ChainOfResponsibilityTest {

private ChainHandler root;

@Before
public void before() {
ChainHandler buttonChainHandler = new ButtonChainHandler();
ChainHandler anchorChainHandler = new AnchorChainHandler();

buttonChainHandler.linkDelegator(anchorChainHandler);
root = buttonChainHandler;
}


@Test
public void buttonClickRequestTest() {
Request request;
ResultSet resultSet;

given: {
request = new ButtonRequest();
}

when: {
resultSet = root.handleRequest(request);
}

then: {
assertThat(resultSet.getValue()).isEqualTo(RequestType.BUTTON_CLICK.name());
}
}


@Test
public void anchorClickRequest() {
Request request;
ResultSet resultSet;

given: {
request = new AnchorRequest();
}

when: {
resultSet = root.handleRequest(request);
}

then: {
assertThat(resultSet.getValue()).isEqualTo(RequestType.ANCHOR_CLICK.name());
}
}


@Test
public void linkClickRequestDefault() {
Request request;
ResultSet resultSet;

given: {
request = new LinkRequest();
}

when: {
resultSet = root.handleRequest(request);
}

then: {
assertThat(resultSet.getValue()).isEqualTo("");
}
}


}

0 comments on commit 03499d6

Please sign in to comment.