Mid
From PDF
GoF Patterns
Gang of Four Patterns
Concrete Visitor Class (ShoppingCart):?
- The concrete visitor ShoppingCart implements the IShoppingCartVisitor
interface. It performs specific operations on each element, like calculating the total
cost with any applicable discounts. For instance, it applies a 10% discount to books
but no discount to fruits.
public class ShoppingCart : IShoppingCartVisitor
{
private double _total;
public void Visit(Book book) => _total += book.Price * 0.9; //
10% discount on books
public void Visit(Fruit fruit) => _total += fruit.Price; // No
discount on fruits
public double GetTotal() => _total;
}Share this Q&A
Share preview image: https://www.toolliyo.com/images/toolliyo-logo.png