- Mastering Microservices with Java
- Sourabh Sharma
- 465字
- 2021-07-02 13:03:40
Service implementation
Using the preceding approach, you could divide the abstraction of the domain service into two parts—the main service abstraction and a read-only service abstraction:
public abstract class ReadOnlyBaseService<TE, T> { private final Repository<TE, T> repository; ReadOnlyBaseService(ReadOnlyRepository<TE, T> repository) { this.repository = repository; } ... }
The following diagram contains the OTRS services and their relationships:

Now, we could use this ReadOnlyBaseService to create BaseService. Here, we are using the dependency injection pattern via a constructor to map concrete objects with abstraction:
public abstract class BaseService<TE, T> extends ReadOnlyBaseService<TE, T> { private final Repository<TE, T> _repository; BaseService(Repository<TE, T> repository) { super(repository); _repository = repository; } public void add(TE entity) throws Exception { _repository.add(entity); } public Collection<TE> getAll() { return _repository.getAll(); } }
Now, after defining the service abstraction services, we could implement the RestaurantService in the following way:
public class RestaurantService extends BaseService<Restaurant, BigInteger> { private final RestaurantRepository<
Restaurant, String> restaurantRepository; public RestaurantService(RestaurantRepository repository) { super(repository); restaurantRepository = repository; } public void add(Restaurant restaurant) throws Exception { if (restaurantRepository.ContainsName(restaurant.getName())) { throw new Exception(String.format("There is already a
product with the name - %s", restaurant.getName())); } if (restaurant.getName() == null ||
"".equals(restaurant.getName())) { throw new Exception("Restaurant name cannot be null or
empty string."); } super.add(restaurant); } @Override public Collection<Restaurant> getAll() { return super.getAll(); } }
Similarly, you could write the implementation for other entities. This code is a basic implementation, and you might add various implementations and behaviors to the production code.
We can write an application class that would execute and test the sample domain model code that we have just written.
The RestaurantApp.java file will look something like this:
public class RestaurantApp {
public static void main(String[] args) {
try {
// Initialize the RestaurantService
RestaurantService restaurantService = new RestaurantService(new InMemRestaurantRepository());
// Data Creation for Restaurants
List<Table> tableList = Arrays.asList(
new Table("Table 1", BigInteger.ONE, 6),
new Table("Table 2", BigInteger.valueOf(2), 4),
new Table("Table 3", BigInteger.valueOf(3), 2)
);
// Add few restaurants using Service
// Note: To raise an exception give same restaurant name to one of the below restaurant
restaurantService
.add(new Restaurant("Big-O Restaurant", "1", Optional.ofNullable(tableList)));
restaurantService.add(new Restaurant("Pizza Shops", "2", Optional.empty()));
restaurantService.add(new Restaurant("La Pasta", "3", Optional.empty()));
// Retrieving all restaurants using Service
Collection<Restaurant> restaurants = restaurantService.getAll();
// Print the retrieved restaurants on console
System.out.println("Restaurants List:");
restaurants.stream()
.map(r -> String.format("Restaurant: %s", r))
.forEach(System.out::println);
} catch (Exception ex) {
System.out.println(String.format("Exception: %s", ex.getMessage()));
// Exception Handling Code
}
}
}
To execute this program, either execute it directly from the IDE, or run it using Maven. It prints the following output:
Scanning for projects... ------------------------------------------------------------------------ Building 11537_chapter3 1.0-SNAPSHOT ------------------------------------------------------------------------ Restaurants List: Restaurant: {id: 3, name: La Pasta, tables: null} Restaurant: {id: 2, name: Pizza Shops, tables: null} Restaurant: {id: 1, name: Big-O Restaurant, tables: [{id: 1, name: Table 1, capacity: 6}, {id: 2, name: Table 2, capacity: 4}, {id: 3, name: Table 3, capacity: 2}]} ------------------------------------------------------------------------ BUILD SUCCESS ------------------------------------------------------------------------