Concrete Visitor Class (ShoppingCart):?
Short answer: 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
Example code
{
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;
}
Real-world example (ShopNest)
Use a GoF pattern in ShopNest only when it removes duplication or makes a change safer—explain the problem it solves in the interview.
Say this in the interview
- Define — one clear sentence (the short answer above).
- Example — relate it to a project like ShopNest or your real work.
- Trade-off — when you would not use it.
Share this Q&A
Share preview image: https://www.toolliyo.com/images/toolliyo-logo.png