设计模式 – 我应该使用工厂来更新对象吗?

设计模式 – 我应该使用工厂来更新对象吗?,第1张

概述我使用工厂通过命令对象创建实体,但是当我想从命令对象更新实体时,我找不到一个好的模式来执行此 *** 作.我应该只使用工厂来更新实体,或者如果没有,那么什么是好的模式? interface ProductFactory { Product create(ProductCommand command); Product update(Product product, ProductComma 我使用工厂通过命令对象创建实体,但是当我想从命令对象更新实体时,我找不到一个好的模式来执行此 *** 作.我应该只使用工厂来更新实体,或者如果没有,那么什么是好的模式?

interface ProductFactory {    Product create(ProductCommand command);    Product update(Product product,ProductCommand command);}

我的服务:

class ProductServiceImpl {     public Product updateProduct(long productID,ProductCommand command) {         Product product = productRepository.findOne(productID);         product = productFactory.update(product,productCommand);         return productRepository.save(product);     }}
解决方法 在DDD上,其中一个战略模式是在代码中使用无处不在的语言.因此,在您的特定情况下,类方法应根据它们的作用命名,例如Product :: changeTitle或PrIDuct :: changePrice:

此外,还有多种建筑风格.其中一个没有命令对象,但有多个参数,如下所示:

class ProductService {     public voID changeProductPrice(long productID,double newPrice) {         Product product = productRepository.findOne(productID);         product.changePrice(product,newPrice);        productRepository.save(product);     }}

这种风格跟随着无处不在的语言.

另一种样式是使用命令对象参数:

class ProductCommandHandler {     public voID handleChangeProductPrice(ChangeProductPrice command) {         Product product = productRepository.findOne(command.getAggregateID ());         product.handleChangeProductPrice(command);        productRepository.save(product);     }}

第二种风格非常适合CQRS事件源,你几乎可以通过提取一个通用的命令处理程序来消除Application层,该命令处理程序从存储库中识别并加载聚合,它向命令发送命令,它收集事件然后将它们保存到活动商店.我用了很多这种风格.

总结

以上是内存溢出为你收集整理的设计模式 – 我应该使用工厂来更新对象吗?全部内容,希望文章能够帮你解决设计模式 – 我应该使用工厂来更新对象吗?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1085142.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-27
下一篇2022-05-27

发表评论

登录后才能评论

评论列表(0条)

    保存