Design Patterns Handbook
  • Introduction
  • Creational Patterns
    • Builder
    • Factory
    • Abstract Factory
    • Factory Method
    • Prototype
    • Singleton
    • Object Pool
    • Revealing Constructor
  • Structural Patterns
    • Adapter
    • Composite
    • Proxy
    • Flyweight
    • Facade
    • Bridge
    • Decorator
    • Private Class Data
  • Behavioral Patterns
    • Template Method
    • Mediator
    • Chain Of Responsibility
    • Observer
    • Strategy
    • Command
    • State
    • Visitor
    • Memento
    • Interpreter
    • Null Object
    • Iterator
    • Middleware
  • Clean Code Patterns
    • Extract Method
    • Clarify Responsibility
    • Remove Duplications
    • Keep Refactoring
    • Always Unit Test
    • Create Data Type
    • Comment to Better Name
    • Consistent Naming
    • If-else over ternary operator
    • Composition over Inheritance
    • Too Many Returns
    • Private to Interface
  • Anti Patterns
    • Big Ball of Mud
    • Singleton
    • Mad Scientist
    • Spaghetti Code
    • It Will Never Happen
    • Error Codes
    • Commented Code
    • Abbreviations
    • Prefixes
    • Over Patternized
    • Generic Interface over Function
Powered by GitBook
On this page

Was this helpful?

  1. Behavioral Patterns

Visitor

Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates.

Example - Shopping cart

We will implement a shopping cart full of vegetables that we will be able to visit and get its weight (so we can then get the price for it).

interface Visitorable {
    int accept(Visitor visitor);
}

interface Visitor {
    int getWeight(Apple apple);
    int getWeight(Banana banana);
}

class VisitorImpl implements Visitor {

    @Override
    public int getWeight(Apple apple) {
        return apple.getWeight();
    }

    @Override
    public int getWeight(Banana banana) {
        return banana.getWeight();
    }
}

class Apple implements Visitorable {

    private int weight;

    public Apple(int weight) {
        this.weight = weight;
    }

    public int getWeight() {
        return weight;
    }

    @Override
    public int accept(Visitor visitor) {
        return visitor.getWeight(this);
    }
}

class Banana implements Visitorable {

    private int weight;

    public Banana(int weight) {
        this.weight = weight;
    }

    public int getWeight() {
        return weight;
    }

    @Override
    public int accept(Visitor visitor) {
        return visitor.getWeight(this);
    }
}

Now we are going to create apple and banana and visit them in order to get its weight. It is going to return sum equal to 250.

Visitor visitor = new VisitorImpl();
Visitorable[] visitorables = {new Apple(100), new Banana(150)};
Long sum = Arrays
                .stream(visitorables)
                .collect(Collectors.summarizingInt(it -> it.accept(visitor)))
                .getSum();
System.out.println(sum);
PreviousStateNextMemento

Last updated 5 years ago

Was this helpful?