mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-15 18:59:10 +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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user