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

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

问题描述

我使用了Visitor设计模式来解决系统中的问题之一.作为参考,我使用了 DoFactory网站此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示例中,访问者使用返回类型为"void"的方法,而在YouTube视频中,作者使用"double".

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

我为什么要问:
向公司CTO提出解决方案后,他接受了将其称为访客"的说法,但是他声称如果访客不是

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中拥有一个私有字段,用于累积总值并在每种访问方法内将其递增.然后用吸气剂得到最终总量.

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.

在访问者示例中,作者还显式调用了每个食品,但没有显示出访问者模式的强大功能.我本来有一个可以访问的杂货物品的容器对象,它的接受方法将访问收据中的每个物品.

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天全站免登陆