访问者设计模式——返回类型 [英] Visitor Design Pattern - return type

查看:28
本文介绍了访问者设计模式——返回类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用访问者设计模式来解决我们系统中的一个问题.作为如何实现它的参考,我使用了 DoFactory site这个 YouTube 视频.

I used the Visitor design pattern to solve one of the issues within our system. As a reference how to implement it I used DoFactory site and This YouTube video.

在 DoFactory 示例中,Visitor 使用返回类型为void"的方法,而在 YouTube 视频中,作者使用double".

In DoFactory example, the Visitor uses a method with return type of "void" and in YouTube video, author uses "double".

我为什么问:
在向公司 CTO 展示解决方案后,他接受将其称为访问者,但他声称如果访问者不是 GoF,而不是像滥用真正的访问者模式.

Why I ask:
After presenting the solution to company CTO, he accepted to call it Visitor, but he claims like if Visitor is not "void" as stated in GoF, than it like abuse the real Visitor pattern.

问题:
访问者模式是否需要返回void"?
我的意思是为了成为 DoFactory (GoF) 描述的真正的访客模式",或者它可以是任何返回类型并且仍然可以称为真正的访客模式"?

Question:
Is it required for Visitor pattern to return "void"?
I mean in order to be "real Visitor pattern" as DoFactory (GoF) describes it, or it can be any return type and can still be called "real Visitor pattern"?

推荐答案

设计模式旨在用作展示如何解决常见计算机科学问题的指南.欢迎您以任何您希望的方式偏离真实"实现.

Design Patterns are meant to be used as a guide to show how to solve common computer science problems. You are welcome to deviate from the "real" implementation any way you wish.

至于您的示例 youtube 视频,该视频的作者展示了如何使用访问者模式来计算不同类型商品的税费.每个 visit 方法返回双倍的金额,包括每件商品的税费.然后进行了不同的访问者实现,以展示如何在不更改代码的情况下使用不同的方法来计算税收(正常与免税期等).

As for your example youtube video, the author of that shows a how you can use the visitor pattern to compute taxes for different types of items. Each visit method returned a double for the amount of money including taxes each item would cost. Different Visitor implementations were then made to show how you could have different ways to compute taxes (normal vs tax holiday etc) without changing your code.

这个例子是一个玩具"问题,旨在以一种易于理解的方式教授访问者模式的工作原理 - 并且它做得很好.

This example was a "toy" problem and was meant to teach how the visitor pattern worked in an easy to understand way- and it does a good job.

虽然我说欢迎您偏离 GoF 实现,但模仿此视频中的代码可能不是一个好主意.视频中有一些东西在实际程序中使用是个坏主意.例如使用 double 来赚钱.我认为双倍回报(为了金钱)只是一种快速展示访问者工作方式的方式,您可能不应该使用它.

While I say you are welcome to deviate from the GoF implementation, it might not be a good idea to mimic the code from this video. There were a few things in the video that would be a bad idea to use in a real program. For example using double for money. I think the return double (for money) was just a way to quickly show how the visitor worked and you probably shouldn't use it.

如果您想修改视频中的代码以返回 void.最简单的解决方案是在 TaxVisitor 中有一个私有字段,用于累积总值并在每个访问方法中增加它.然后有一个 getter 来获得最终的总数.

If you wanted to modify the code in the video to return void instead. The easiest solution would be to have a private field in the TaxVisitor that accumulates the total value and increment it inside each visit method. Then have a getter to get the final total.

作者还明确调用了他的访问者示例中的每个食物项目,这并没有展示访问者模式的力量.我会有一个可访问的杂货容器对象,它的 accept 方法将访问收据中的每个项目.

The author also explicitly invokes each food item in his visitor example which doesn't show off the power of the Visitor Pattern. I would have had a container object of the grocery items that would be visitable and its accept method would visit each item in the receipt.

GroceryList groceries = new GroceryList();

groceries.add(new Milk(...));
groceries.add(new Liquor(...));
   ...


 TaxVisitor visitor = new TaxVisitor();

 visitor.accept(groceries);


 Money tax = visitor.getTax();

 Money preTaxTotal = groceries.getTotalPreTax();

 Money total = preTaxTotal.plus(tax);

//or compute tax during tax holiday
TaxVisitor holidayVisitor = new TaxHolidayVisitor();
  holidayVisitor.accept(groceries);


 Money holidayTax = holidayVisitor.getTax();

  Money holidayTotal = preTaxTotal.plus(holidayTax);

这篇关于访问者设计模式——返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆