iulian.manda@gmail evaluation article "A quick and practical example of Hexagonal Architecture in Java"
This commit is contained in:
parent
442e5c1a7f
commit
5cb49ba8a9
|
@ -0,0 +1,17 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<artifactId>hexagonal</artifactId>
|
||||||
|
<version>1.0</version>
|
||||||
|
<name>hexagonal</name>
|
||||||
|
<description>A quick and practical example of Hexagonal Architecture in Java</description>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>parent-java</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<relativePath>../../parent-java</relativePath>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
</project>
|
|
@ -0,0 +1,17 @@
|
||||||
|
package com.baeldung.pattern.hexagonal;
|
||||||
|
|
||||||
|
import com.baeldung.pattern.hexagonal.repository.RepositoryFactory;
|
||||||
|
import com.baeldung.pattern.hexagonal.repository.WeatherRepository;
|
||||||
|
import com.baeldung.pattern.hexagonal.service.WeatherForecasterService;
|
||||||
|
import com.baeldung.pattern.hexagonal.ui.WeatherForecasterConsoleUI;
|
||||||
|
import com.baeldung.pattern.hexagonal.ui.WeatherForecasterUI;
|
||||||
|
|
||||||
|
public class WeatherForecasterApp {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
WeatherRepository repository = RepositoryFactory.getMockWeatherRepository();
|
||||||
|
WeatherForecasterService service = new WeatherForecasterService(repository);
|
||||||
|
WeatherForecasterUI ui = new WeatherForecasterConsoleUI(service);
|
||||||
|
|
||||||
|
ui.start();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,17 @@
|
||||||
|
package com.baeldung.pattern.hexagonal.repository;
|
||||||
|
|
||||||
|
public class MockWeatherRepository implements WeatherRepository {
|
||||||
|
@Override
|
||||||
|
public double getTemperature(String location) {
|
||||||
|
switch (location) {
|
||||||
|
case "cluj-napoca":
|
||||||
|
return 17;
|
||||||
|
case "bucharest":
|
||||||
|
return 22;
|
||||||
|
case "new york":
|
||||||
|
return 15;
|
||||||
|
default:
|
||||||
|
return 20;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
package com.baeldung.pattern.hexagonal.repository;
|
||||||
|
|
||||||
|
public class RepositoryFactory {
|
||||||
|
private RepositoryFactory() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static WeatherRepository getMockWeatherRepository() {
|
||||||
|
return new MockWeatherRepository();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,5 @@
|
||||||
|
package com.baeldung.pattern.hexagonal.repository;
|
||||||
|
|
||||||
|
public interface WeatherRepository {
|
||||||
|
double getTemperature(String location);
|
||||||
|
}
|
|
@ -0,0 +1,20 @@
|
||||||
|
package com.baeldung.pattern.hexagonal.service;
|
||||||
|
|
||||||
|
import com.baeldung.pattern.hexagonal.repository.WeatherRepository;
|
||||||
|
|
||||||
|
public class WeatherForecasterService {
|
||||||
|
private final WeatherRepository repository;
|
||||||
|
|
||||||
|
public WeatherForecasterService(WeatherRepository repository) {
|
||||||
|
this.repository = repository;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double forecast(String location) {
|
||||||
|
String normalizedLocation = getNormalizedLocation(location);
|
||||||
|
return repository.getTemperature(normalizedLocation);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getNormalizedLocation(String location) {
|
||||||
|
return location == null ? "" : location.toLowerCase();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,48 @@
|
||||||
|
package com.baeldung.pattern.hexagonal.ui;
|
||||||
|
|
||||||
|
import com.baeldung.pattern.hexagonal.service.WeatherForecasterService;
|
||||||
|
|
||||||
|
import java.io.PrintStream;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class WeatherForecasterConsoleUI implements WeatherForecasterUI {
|
||||||
|
private WeatherForecasterService service;
|
||||||
|
private Scanner in;
|
||||||
|
private PrintStream out;
|
||||||
|
|
||||||
|
public WeatherForecasterConsoleUI(WeatherForecasterService service) {
|
||||||
|
this.service = service;
|
||||||
|
this.in = new Scanner(System.in);
|
||||||
|
this.out = new PrintStream(System.out, true, StandardCharsets.UTF_8);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void start() {
|
||||||
|
String input = getInput();
|
||||||
|
while (isNotExit(input)) {
|
||||||
|
showTemperature(input);
|
||||||
|
input = getInput();
|
||||||
|
}
|
||||||
|
stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getInput() {
|
||||||
|
out.print("Location: ");
|
||||||
|
return in.nextLine();
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isNotExit(String input) {
|
||||||
|
return !input.equalsIgnoreCase("exit");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void showTemperature(String input) {
|
||||||
|
double temperature = service.forecast(input);
|
||||||
|
out.printf("%.2f\u00B0C%n", temperature);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void stop() {
|
||||||
|
in.close();
|
||||||
|
out.close();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,5 @@
|
||||||
|
package com.baeldung.pattern.hexagonal.ui;
|
||||||
|
|
||||||
|
public interface WeatherForecasterUI {
|
||||||
|
void start();
|
||||||
|
}
|
|
@ -1,5 +1,5 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<artifactId>patterns</artifactId>
|
<artifactId>patterns</artifactId>
|
||||||
|
@ -22,6 +22,7 @@
|
||||||
<module>cqrs-es</module>
|
<module>cqrs-es</module>
|
||||||
<module>front-controller</module>
|
<module>front-controller</module>
|
||||||
<module>hexagonal-architecture</module>
|
<module>hexagonal-architecture</module>
|
||||||
|
<module>hexagonal</module>
|
||||||
<module>intercepting-filter</module>
|
<module>intercepting-filter</module>
|
||||||
<module>solid</module>
|
<module>solid</module>
|
||||||
<module>clean-architecture</module>
|
<module>clean-architecture</module>
|
||||||
|
|
Loading…
Reference in New Issue