[英]Calling service method from another service class
I have two service classes. 我有两个服务类。 WarehouseManagementService provides methods to administrate warehouses and stocks. WarehouseManagementService提供管理仓库和库存的方法。 SalesManagementService provides methods to manage customers and their orders. SalesManagementService提供管理客户及其订单的方法。
To create an order, it is necessary to check, if the order item quantity is sufficient. 要创建订单,必须检查订单商品数量是否足够。 For this I would use the method availableStock(Product p) in the WarehouseManagementService. 为此,我将使用WarehouseManagementService中的availableStock(Product p)方法。 But I do not know, how to call this method properly. 但我不知道,如何正确地调用这种方法。
Do I have to create an instance of WarehouseManagementService in SalesManagementService? 我是否必须在SalesManagementService中创建WarehouseManagementService的实例? Or should I add the WarehouseManagementServiceInterface to the SalesManagementService constructor (dependency injection)? 或者我应该将WarehouseManagementServiceInterface添加到SalesManagementService构造函数(依赖注入)?
What could a good architecture look like, to achieve a loose coupling of these two classes? 一个好的架构是什么样的,实现这两个类的松散耦合?
Thanks in advance. 提前致谢。
public class WarehouseManagementService implements WarehouseManagementServiceInterface {
private DatabaseReadWarehouseInterface dbRead;
private DatabaseWriteWarehouseInterface dbWrite;
public int availableStock(Product p) {
// returns available quantity of product
}
}
public class SalesManagementService implements SalesManagementServiceInterface {
private DatabaseReadSalesInterface dbRead;
private DatabaseWriteSalesInterface dbWrite;
public void addOrder(Order o) {
// creates order, if product quantity is sufficient
}
}
You have the right feeling: The dependency should be injected. 你有正确的感觉:应该注入依赖。 Just remember the Single Responsibility Principle : "A class should have only one reason to change". 只要记住单一责任原则 :“一个班级应该只有一个改变的理由”。
By constructing WarehouseManagementService
in SalesManagementService
, you would add a second reason for SalesManagementService
to change: When the way of constructing your WarehouseManagementService
changes. 通过构建WarehouseManagementService
在SalesManagementService
,你会添加第二个原因SalesManagementService
改变:构建你的当的方式WarehouseManagementService
变化。
To solve it, you could either use a full-blown Dependency Injection framework, or simply start by adding a constructor parameter to SalesManagementService
: 要解决这个问题,您可以使用完整的依赖注入框架,也可以直接向SalesManagementService
添加构造函数参数:
public class SalesManagementService implements SalesManagementServiceInterface {
private DatabaseReadSalesInterface dbRead;
private DatabaseWriteSalesInterface dbWrite;
private WarehouseManagementServiceInterface warehouseManagementService;
constructor(DatabaseReadSalesInterface dbRead,
DatabaseWriteSalesInterface dbWrite,
WarehouseManagementServiceInterface warehouseManagementService) {
this.dbRead = dbRead;
this.dbWrite = dbWrite;
this.warehouseManagementService = warehouseManagementService;
}
public void addOrder(Order o) {
// creates order, if product quantity is sufficient
}
}
Then in your main
method, take care of instantiating WarehouseManagementService
and pass it to SalesManagementService
. 然后在main
方法中,负责实例化WarehouseManagementService
并将其传递给SalesManagementService
。
As a sidenote, you might want to move the construction of SalesManagementService
and WarehouseManagementService
into factories which give you a SalesManagementServiceInterface
and a WarehouseManagementServiceInterface
rather than the concrete classes ("program to interface, not implementation"). 作为旁注,您可能希望将SalesManagementService
和WarehouseManagementService
的构造移动到工厂中,这些工厂为您提供SalesManagementServiceInterface
和WarehouseManagementServiceInterface
而不是具体的类(“程序到接口,而不是实现”)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.