Visitor Design Pattern - return type

前端 未结 1 1816
生来不讨喜
生来不讨喜 2021-01-05 09:47

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.

In D

相关标签:
1条回答
  • 2021-01-05 10:03

    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.

    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.

    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.

    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);
    
    0 讨论(0)
提交回复
热议问题