|
Post by account_disabled on Feb 1, 2024 4:45:06 GMT -5
可能会出现个问题这样的解决方案有什么优点?让我们想象下我们希望将 模型映射到学说实体并使用 将其添加到数据库中。在第个示例中要做到这点我们必须更改 的实现以及使用 模型数据访问的所有其他地方在大型系统中可能有很多。在第二个保留 原则的示例中只需添加 接口的另个实现 然后只需添加个配置文件其中包含映射到学说实体的 模型并更改依赖项注入配置以便选择 实现作为 。请注意我们不必以任何方式接触我们的域服务。 我们示例的另个要求是向用户发送订单已成功创建的通知。通知可以通过多种方式发送例如通过电子邮件短信或网站通知。与前面向数据库添加订单的示例不同让我们从抽象开始对流程进行建模使其符合 原则。这给了我们很大的自由度以至于在建模时我们可能还不知道如何 电话数据 向用户发送通知。 首先我们来实现发送消息的模型 现在我们需要个向用户发送消息的服务但正如我之前提到的我们从抽象开始建模所以让我们实现这个服务的接口 我们还需要个类该类将根据订单对象构建 对象并将其。 送到 我们可以以 的形式实现它用于发送订单通知 剩下的就是在 中使用 以便在添加订单时向用户发送条消息。 我们可以看到通户发送通知的整个过程进行了建模而无需指定该消息的物理传递方式因为高级类对此无所知。做出此决定后只需使用适当的类实现 接口例如概括 根据所提供的示例可以看出遵循依赖倒置原则在流程建模方面具有非常大的优势因为只要建模正确的抽象我们实际上可以独立实现高层和低层元素。由于这种抽象我们的低级元素可以自由互换并且更改其实现不会对高级流程产生直接影响。
|
|