mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-18 09:25:54 +00:00
fix: Fix context object (#2415)
* fix pr builder goals * update links * add context-object to build * add autogenerated content * fix checkstyle findings
This commit is contained in:
@@ -13,29 +13,30 @@ import lombok.extern.slf4j.Slf4j;
|
||||
@Slf4j
|
||||
public class App {
|
||||
|
||||
private static final String SERVICE = "SERVICE";
|
||||
private static final String SERVICE = "SERVICE";
|
||||
|
||||
/**
|
||||
* Program entry point.
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
//Initiate first layer and add service information into context
|
||||
var layerA = new LayerA();
|
||||
layerA.addAccountInfo(SERVICE);
|
||||
/**
|
||||
* Program entry point.
|
||||
*
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
//Initiate first layer and add service information into context
|
||||
var layerA = new LayerA();
|
||||
layerA.addAccountInfo(SERVICE);
|
||||
|
||||
LOGGER.info("Context = {}",layerA.getContext());
|
||||
LOGGER.info("Context = {}", layerA.getContext());
|
||||
|
||||
//Initiate second layer and preserving information retrieved in first layer through passing context object
|
||||
var layerB = new LayerB(layerA);
|
||||
layerB.addSessionInfo(SERVICE);
|
||||
//Initiate second layer and preserving information retrieved in first layer through passing context object
|
||||
var layerB = new LayerB(layerA);
|
||||
layerB.addSessionInfo(SERVICE);
|
||||
|
||||
LOGGER.info("Context = {}",layerB.getContext());
|
||||
LOGGER.info("Context = {}", layerB.getContext());
|
||||
|
||||
//Initiate third layer and preserving information retrieved in first and second layer through passing context object
|
||||
var layerC = new LayerC(layerB);
|
||||
layerC.addSearchInfo(SERVICE);
|
||||
//Initiate third layer and preserving information retrieved in first and second layer through passing context object
|
||||
var layerC = new LayerC(layerB);
|
||||
layerC.addSearchInfo(SERVICE);
|
||||
|
||||
LOGGER.info("Context = {}",layerC.getContext());
|
||||
}
|
||||
LOGGER.info("Context = {}", layerC.getContext());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,13 +5,13 @@ import lombok.Getter;
|
||||
@Getter
|
||||
public class LayerA {
|
||||
|
||||
private ServiceContext context;
|
||||
private ServiceContext context;
|
||||
|
||||
public LayerA() {
|
||||
context = ServiceContextFactory.createContext();
|
||||
}
|
||||
public LayerA() {
|
||||
context = ServiceContextFactory.createContext();
|
||||
}
|
||||
|
||||
public void addAccountInfo(String accountService) {
|
||||
context.setAccountService(accountService);
|
||||
}
|
||||
public void addAccountInfo(String accountService) {
|
||||
context.setAccountService(accountService);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,13 +5,13 @@ import lombok.Getter;
|
||||
@Getter
|
||||
public class LayerB {
|
||||
|
||||
private ServiceContext context;
|
||||
private ServiceContext context;
|
||||
|
||||
public LayerB(LayerA layerA) {
|
||||
this.context = layerA.getContext();
|
||||
}
|
||||
public LayerB(LayerA layerA) {
|
||||
this.context = layerA.getContext();
|
||||
}
|
||||
|
||||
public void addSessionInfo(String sessionService) {
|
||||
context.setSessionService(sessionService);
|
||||
}
|
||||
public void addSessionInfo(String sessionService) {
|
||||
context.setSessionService(sessionService);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,13 +5,13 @@ import lombok.Getter;
|
||||
@Getter
|
||||
public class LayerC {
|
||||
|
||||
public ServiceContext context;
|
||||
public ServiceContext context;
|
||||
|
||||
public LayerC(LayerB layerB) {
|
||||
this.context = layerB.getContext();
|
||||
}
|
||||
public LayerC(LayerB layerB) {
|
||||
this.context = layerB.getContext();
|
||||
}
|
||||
|
||||
public void addSearchInfo(String searchService) {
|
||||
context.setSearchService(searchService);
|
||||
}
|
||||
public void addSearchInfo(String searchService) {
|
||||
context.setSearchService(searchService);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,5 +12,7 @@ import lombok.ToString;
|
||||
@Setter
|
||||
public class ServiceContext {
|
||||
|
||||
String AccountService, SessionService, SearchService;
|
||||
String accountService;
|
||||
String sessionService;
|
||||
String searchService;
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ package com.iluwatar.context.object;
|
||||
*/
|
||||
public class ServiceContextFactory {
|
||||
|
||||
public static ServiceContext createContext() {
|
||||
return new ServiceContext();
|
||||
}
|
||||
public static ServiceContext createContext() {
|
||||
return new ServiceContext();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,11 +7,11 @@ import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
public class AppTest {
|
||||
|
||||
/**
|
||||
* Test example app runs without error.
|
||||
*/
|
||||
@Test
|
||||
void shouldExecuteWithoutException() {
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
}
|
||||
/**
|
||||
* Test example app runs without error.
|
||||
*/
|
||||
@Test
|
||||
void shouldExecuteWithoutException() {
|
||||
assertDoesNotThrow(() -> App.main(new String[] {}));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,49 +18,52 @@ import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
*/
|
||||
public class ServiceContextTest {
|
||||
|
||||
private static final String SERVICE = "SERVICE";
|
||||
private static final String SERVICE = "SERVICE";
|
||||
|
||||
private LayerA layerA;
|
||||
private LayerA layerA;
|
||||
|
||||
@BeforeEach
|
||||
void initiateLayerA() {
|
||||
this.layerA = new LayerA();
|
||||
}
|
||||
@BeforeEach
|
||||
void initiateLayerA() {
|
||||
this.layerA = new LayerA();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSameContextPassedBetweenLayers() {
|
||||
ServiceContext context1 = layerA.getContext();
|
||||
var layerB = new LayerB(layerA);
|
||||
ServiceContext context2 = layerB.getContext();
|
||||
var layerC = new LayerC(layerB);
|
||||
ServiceContext context3 = layerC.getContext();
|
||||
@Test
|
||||
void testSameContextPassedBetweenLayers() {
|
||||
ServiceContext context1 = layerA.getContext();
|
||||
var layerB = new LayerB(layerA);
|
||||
ServiceContext context2 = layerB.getContext();
|
||||
var layerC = new LayerC(layerB);
|
||||
ServiceContext context3 = layerC.getContext();
|
||||
|
||||
assertSame(context1, context2);
|
||||
assertSame(context2, context3);
|
||||
assertSame(context3, context1);
|
||||
}
|
||||
assertSame(context1, context2);
|
||||
assertSame(context2, context3);
|
||||
assertSame(context3, context1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testScopedDataPassedBetweenLayers() {
|
||||
layerA.addAccountInfo(SERVICE);
|
||||
var layerB = new LayerB(layerA);
|
||||
var layerC = new LayerC(layerB);
|
||||
layerC.addSearchInfo(SERVICE);
|
||||
ServiceContext context = layerC.getContext();
|
||||
@Test
|
||||
void testScopedDataPassedBetweenLayers() {
|
||||
layerA.addAccountInfo(SERVICE);
|
||||
var layerB = new LayerB(layerA);
|
||||
var layerC = new LayerC(layerB);
|
||||
layerC.addSearchInfo(SERVICE);
|
||||
ServiceContext context = layerC.getContext();
|
||||
|
||||
assertEquals(SERVICE,context.getAccountService());
|
||||
assertNull(context.getSessionService());
|
||||
assertEquals(SERVICE,context.getSearchService());
|
||||
}
|
||||
assertEquals(SERVICE, context.getAccountService());
|
||||
assertNull(context.getSessionService());
|
||||
assertEquals(SERVICE, context.getSearchService());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testToString() {
|
||||
assertEquals(layerA.getContext().toString(),"ServiceContext(AccountService=null, SessionService=null, SearchService=null)");
|
||||
layerA.addAccountInfo(SERVICE);
|
||||
assertEquals(layerA.getContext().toString(), "ServiceContext(AccountService=SERVICE, SessionService=null, SearchService=null)");
|
||||
var layerB = new LayerB(layerA);
|
||||
layerB.addSessionInfo(SERVICE);
|
||||
var layerC = new LayerC(layerB);
|
||||
assertEquals(layerC.getContext().toString(), "ServiceContext(AccountService=SERVICE, SessionService=SERVICE, SearchService=null)");
|
||||
}
|
||||
@Test
|
||||
void testToString() {
|
||||
assertEquals(layerA.getContext().toString(),
|
||||
"ServiceContext(AccountService=null, SessionService=null, SearchService=null)");
|
||||
layerA.addAccountInfo(SERVICE);
|
||||
assertEquals(layerA.getContext().toString(),
|
||||
"ServiceContext(AccountService=SERVICE, SessionService=null, SearchService=null)");
|
||||
var layerB = new LayerB(layerA);
|
||||
layerB.addSessionInfo(SERVICE);
|
||||
var layerC = new LayerC(layerB);
|
||||
assertEquals(layerC.getContext().toString(),
|
||||
"ServiceContext(AccountService=SERVICE, SessionService=SERVICE, SearchService=null)");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user