I have the following package and class structure in my project,
package de.mycompany.jakarta.order;
import de.mycompany.ordermanagement.order.OrderCancellationService;
public class DrugOrderManager {
private static final DrugOrderManager INSTANCE = new DrugOrderManager();
private DrugOrderManager() {
}
public static DrugOrderManager getInstance() {
return INSTANCE;
}
public void cancelOrder() {
OrderCancellationService.getInstance().process();
} }
package de.mycompany.ordermanagement.order;
public class OrderCancellationService {
private static OrderCancellationService INSTANCE = new OrderCancellationService();
private OrderCancellationService() {
}
public static OrderCancellationService getInstance() {
return INSTANCE;
}
public void process() {
} }
My intention is that the OrderCancellationService should be only called by the DrugOrderManager and none of any other class/service should call it directly. I am trying to make DrugOrderManager as a gateway to all the services. How do I restrict this visibility? Please advise
To do what you want, you can simply create OrderCancellationService
as private static class inside DrugOrderManager
.
In your case though, I imagine you will have more similiar classes to OrderCancellationService
- so putting all those classes in a single package and making only DrugOrderManager
public, while making all other services package-private, may be a better approach. You expose only single entrypoint for the consumer, it's manageable and easy to understand the code.
If you can't move the classes then I guess there is no solution in Java.