mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-14 08:58:26 +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:
@@ -61,7 +61,7 @@ jobs:
|
||||
run: sudo apt-get install -y xvfb
|
||||
|
||||
- name: Build with Maven and run SonarQube analysis
|
||||
run: xvfb-run ./mvnw clean -Dsonar.host.url=https://sonarcloud.io -Dsonar.organization=iluwatar -Dsonar.projectKey=iluwatar_java-design-patterns -Dsonar.pullrequest.branch=${{ github.head_ref }} -Dsonar.pullrequest.base=${{ github.base_ref }} -Dsonar.pullrequest.key=${{ github.event.pull_request.number }}
|
||||
run: xvfb-run ./mvnw clean verify -Dsonar.host.url=https://sonarcloud.io -Dsonar.organization=iluwatar -Dsonar.projectKey=iluwatar_java-design-patterns -Dsonar.pullrequest.branch=${{ github.head_ref }} -Dsonar.pullrequest.base=${{ github.base_ref }} -Dsonar.pullrequest.key=${{ github.event.pull_request.number }}
|
||||
env:
|
||||
# These two env variables are needed for sonar analysis
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
@startuml
|
||||
package com.iluwatar.client.session {
|
||||
class App {
|
||||
+ App()
|
||||
+ main(args : String[]) {static}
|
||||
}
|
||||
class Request {
|
||||
- data : String
|
||||
- session : Session
|
||||
+ Request(data : String, session : Session)
|
||||
# canEqual(other : Object) : boolean
|
||||
+ equals(o : Object) : boolean
|
||||
+ getData() : String
|
||||
+ getSession() : Session
|
||||
+ hashCode() : int
|
||||
+ setData(data : String)
|
||||
+ setSession(session : Session)
|
||||
+ toString() : String
|
||||
}
|
||||
class Server {
|
||||
- LOGGER : Logger {static}
|
||||
- host : String
|
||||
- port : int
|
||||
+ Server(host : String, port : int)
|
||||
# canEqual(other : Object) : boolean
|
||||
+ equals(o : Object) : boolean
|
||||
+ getHost() : String
|
||||
+ getPort() : int
|
||||
+ getSession(name : String) : Session
|
||||
+ hashCode() : int
|
||||
+ process(request : Request)
|
||||
+ setHost(host : String)
|
||||
+ setPort(port : int)
|
||||
+ toString() : String
|
||||
}
|
||||
class Session {
|
||||
- clientName : String
|
||||
- id : String
|
||||
+ Session(id : String, clientName : String)
|
||||
# canEqual(other : Object) : boolean
|
||||
+ equals(o : Object) : boolean
|
||||
+ getClientName() : String
|
||||
+ getId() : String
|
||||
+ hashCode() : int
|
||||
+ setClientName(clientName : String)
|
||||
+ setId(id : String)
|
||||
+ toString() : String
|
||||
}
|
||||
}
|
||||
Request --> "-session" Session
|
||||
@enduml
|
||||
@@ -179,6 +179,7 @@ Use the Context Object pattern for:
|
||||
|
||||
## Credits
|
||||
|
||||
* [J2EE Design Patterns](http://corej2eepatterns.com/ContextObject.htm)
|
||||
* [Core J2EE Design Patterns](https://amzn.to/3IhcY9w)
|
||||
* [Core J2EE Design Patterns website - Context Object](http://corej2eepatterns.com/ContextObject.htm)
|
||||
* [Allan Kelly - The Encapsulate Context Pattern](https://accu.org/journals/overload/12/63/kelly_246/)
|
||||
* [Arvid S. Krishna et al. - Context Object](https://www.dre.vanderbilt.edu/~schmidt/PDF/Context-Object-Pattern.pdf)
|
||||
* [Arvid S. Krishna et al. - Context Object](https://www.dre.vanderbilt.edu/~schmidt/PDF/Context-Object-Pattern.pdf)
|
||||
|
||||
@@ -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)");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#
|
||||
# This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
|
||||
#
|
||||
# The MIT License
|
||||
# Copyright © 2014-2021 Ilkka Seppälä
|
||||
# Copyright © 2014-2022 Ilkka Seppälä
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
@@ -20,6 +22,5 @@
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
# THE SOFTWARE.
|
||||
#
|
||||
#spring.thymeleaf.mode=HTML
|
||||
|
||||
server.port=51515
|
||||
|
||||
@@ -1,3 +1,29 @@
|
||||
<!--
|
||||
|
||||
This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
|
||||
|
||||
The MIT License
|
||||
Copyright © 2014-2022 Ilkka Seppälä
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
|
||||
-->
|
||||
<!DOCTYPE HTML>
|
||||
<html xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
|
||||
@@ -1,3 +1,29 @@
|
||||
<!--
|
||||
|
||||
This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
|
||||
|
||||
The MIT License
|
||||
Copyright © 2014-2022 Ilkka Seppälä
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
|
||||
-->
|
||||
<!DOCTYPE HTML>
|
||||
<html xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
|
||||
@@ -1,3 +1,27 @@
|
||||
/*
|
||||
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
|
||||
*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2022 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.page.controller;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@@ -1,3 +1,27 @@
|
||||
/*
|
||||
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
|
||||
*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2022 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.page.controller;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
Reference in New Issue
Block a user