small example of hexagonal architecture in java
This commit is contained in:
3
ddd-modules/hexagonalarchitecture/README.md
Normal file
3
ddd-modules/hexagonalarchitecture/README.md
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
This module contains articles about hexagonal architecture in java
|
||||||
|
|
||||||
|
### Relevant articles
|
||||||
35
ddd-modules/hexagonalarchitecture/pom.xml
Normal file
35
ddd-modules/hexagonalarchitecture/pom.xml
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
<?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" 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>
|
||||||
|
<parent>
|
||||||
|
<artifactId>ddd-modules</artifactId>
|
||||||
|
<groupId>com.baeldung.dddmodules</groupId>
|
||||||
|
<version>1.0</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<groupId>com.baeldung.dddmodules.hexagonalarchitecture</groupId>
|
||||||
|
<artifactId>hexagonalarchitecture</artifactId>
|
||||||
|
<version>1.0</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<name>hexagonalarchitecture</name>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<source>${maven.compiler.source}</source>
|
||||||
|
<target>${maven.compiler.target}</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<maven.compiler.source>1.8</maven.compiler.source>
|
||||||
|
<maven.compiler.target>1.8</maven.compiler.target>
|
||||||
|
</properties>
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package com.baeldung.dddmodules.hexagonalarchitecture.adapters;
|
||||||
|
|
||||||
|
import com.baeldung.dddmodules.hexagonalarchitecture.ports.UserPersistence;
|
||||||
|
import com.baeldung.dddmodules.hexagonalarchitecture.core.User;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
class InMemoryUserPersistenceImpl implements UserPersistence {
|
||||||
|
@Override
|
||||||
|
public User createUser(User user) {
|
||||||
|
user.setId(UUID.randomUUID());
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package com.baeldung.dddmodules.hexagonalarchitecture.adapters;
|
||||||
|
|
||||||
|
import com.baeldung.dddmodules.hexagonalarchitecture.core.User;
|
||||||
|
import com.baeldung.dddmodules.hexagonalarchitecture.core.UserServiceImpl;
|
||||||
|
import com.baeldung.dddmodules.hexagonalarchitecture.ports.UserService;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
UserService userService = new UserServiceImpl(new InMemoryUserPersistenceImpl());
|
||||||
|
User user = userService.createUser(new User("John", "Doe"));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
package com.baeldung.dddmodules.hexagonalarchitecture.core;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public class User {
|
||||||
|
private UUID id;
|
||||||
|
private String firstName;
|
||||||
|
private String lastName;
|
||||||
|
|
||||||
|
public User(String firstName, String lastName) {
|
||||||
|
this.firstName = firstName;
|
||||||
|
this.lastName = lastName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public UUID getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(UUID id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getFirstName() {
|
||||||
|
return firstName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFirstName(String firstName) {
|
||||||
|
this.firstName = firstName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLastName() {
|
||||||
|
return lastName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLastName(String lastName) {
|
||||||
|
this.lastName = lastName;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "User{" + "id=" + id + ", firstName='" + firstName + '\'' + ", lastName='" + lastName + '\'' + '}';
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package com.baeldung.dddmodules.hexagonalarchitecture.core;
|
||||||
|
|
||||||
|
import com.baeldung.dddmodules.hexagonalarchitecture.ports.UserPersistence;
|
||||||
|
import com.baeldung.dddmodules.hexagonalarchitecture.ports.UserService;
|
||||||
|
|
||||||
|
public class UserServiceImpl implements UserService {
|
||||||
|
private UserPersistence userPersistence;
|
||||||
|
|
||||||
|
public UserServiceImpl(UserPersistence userPersistence) {
|
||||||
|
this.userPersistence = userPersistence;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public User createUser(User userToCreate) {
|
||||||
|
return userPersistence.createUser(userToCreate);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package com.baeldung.dddmodules.hexagonalarchitecture.ports;
|
||||||
|
|
||||||
|
import com.baeldung.dddmodules.hexagonalarchitecture.core.User;
|
||||||
|
|
||||||
|
public interface UserPersistence {
|
||||||
|
User createUser(User user);
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package com.baeldung.dddmodules.hexagonalarchitecture.ports;
|
||||||
|
|
||||||
|
import com.baeldung.dddmodules.hexagonalarchitecture.core.User;
|
||||||
|
|
||||||
|
public interface UserService {
|
||||||
|
User createUser(User userToCreate);
|
||||||
|
}
|
||||||
@@ -1,8 +1,5 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||||
<project
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
||||||
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>
|
||||||
<groupId>com.baeldung.dddmodules</groupId>
|
<groupId>com.baeldung.dddmodules</groupId>
|
||||||
<artifactId>ddd-modules</artifactId>
|
<artifactId>ddd-modules</artifactId>
|
||||||
@@ -23,7 +20,8 @@
|
|||||||
<module>shippingcontext</module>
|
<module>shippingcontext</module>
|
||||||
<module>ordercontext</module>
|
<module>ordercontext</module>
|
||||||
<module>mainapp</module>
|
<module>mainapp</module>
|
||||||
</modules>
|
<module>hexagonalarchitecure</module>
|
||||||
|
</modules>
|
||||||
|
|
||||||
<dependencyManagement>
|
<dependencyManagement>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
|||||||
Reference in New Issue
Block a user