quiz image

Software Design and Development

SelfSufficientRadon avatar
SelfSufficientRadon
·
·
Download

Start Quiz

Study Flashcards

16 Questions

What allows the visitor class to execute the proper method on an object without using conditionals?

Double Dispatch

What is the main advantage of using the Visitor pattern?

It allows adding new behaviors to nodes without changing the code

What do nodes do when they 'accept' a visitor?

They tell the visitor what method to execute

What is a common interface extracted for all visitors?

Visitor interface

Why can't method overloading be used to solve the problem of calling the correct visitor method?

Because the exact class of the node object is unknown in advance

What pattern is related to the idea of adding new behaviors to objects without changing their code?

Decorator pattern

What is the main difference between the Visitor pattern and the Composite pattern?

The Visitor pattern is used for behavior, the Composite pattern is used for structure

What is the main disadvantage of using the Visitor pattern?

It makes the code more complex

What is the main concern of the system architect when it comes to altering existing node classes?

The changes might break the application due to potential bugs.

What design pattern is suggested as a solution to the problem of adding XML export functionality to the node classes?

Visitor pattern

What is the primary job of the node classes in the given system?

To work with geodata.

Why was the initial solution of adding an export method to each node class not ideal?

Because it would require changing the existing node classes.

What is the main advantage of using polymorphism in the initial solution?

It decouples the code that calls the export method from concrete classes of nodes.

What is the relationship between the nodes in the graph?

The nodes are connected if there's a road between the real objects they represent.

Which design pattern is most similar to the Visitor pattern in terms of decoupling behavior from classes?

Bridge pattern

What is the main difference between the Visitor pattern and the Proxy pattern?

The Visitor pattern adds new behavior, while the Proxy pattern adds a layer of abstraction.

Study Notes

The Problem with Adding XML Export to Node Classes

  • The system architect refused to allow changes to existing node classes because they were already in production and he didn't want to risk breaking them.
  • The proposed solution to add an export method to each node class and use recursion to export the graph into XML format was rejected.
  • The primary job of node classes was to work with geodata, and adding XML export behavior would be unrelated to their main purpose.

The Visitor Pattern Solution

  • The Visitor pattern suggests placing new behavior into a separate class called visitor, instead of integrating it into existing classes.
  • The visitor class defines a set of methods, each of which can take arguments of different types, allowing for different implementation for various node classes.
  • The visitor pattern addresses the problem of calling methods with different signatures by using a technique called Double Dispatch.

Double Dispatch

  • Double Dispatch is a technique that helps execute the proper method on an object without cumbersome conditionals.
  • It delegates the choice of method to the objects being passed as an argument, allowing them to pick a proper method on the visitor.
  • Objects "accept" a visitor and tell it what visiting method should be executed.

Benefits of the Visitor Pattern

  • The change to the node classes is trivial, and it allows for adding further behaviors without altering the code again.
  • Extracting a common interface for all visitors allows all existing nodes to work with any visitor.
  • Introducing a new behavior related to nodes only requires implementing a new visitor class.

A software development scenario where a feature is refused due to concerns about future changes and code organization.

Make Your Own Quizzes and Flashcards

Convert your notes into interactive study material.

Get started for free

More Quizzes Like This

Use Quizgecko on...
Browser
Browser