Case Study

Explore the application of design patterns to enhance the efficiency and functionality of the k-NN algorithm's classifier and training data.

Overview of the logical view

The previous chapters of the case study have contained a number of design patterns. We’ll pick a variation on the model and walk through some of the patterns from this chapter and how they were applied.

Here’s an overview of several parts of the application’s classes.

Press + to interact
 The case study Logical view
The case study Logical view

Patterns applied to the classifier algorithm

This involves a number of patterns. We’ll start with the Hyperparameter class, which is a Facade that includes two separate complex components, the classifier algorithm and the training data.

First, we’ll look at the classifier algorithm. In the Iterator Pattern, we saw how the classifier is itself a complex structure. We looked at three alternatives: k_nn_1(), which had a naive sort, k_nn_b(), which used bisection, and k_nn_q(), which used a heap queue. This exploration relied on several design patterns:

  • The classifier depends on the Strategy design pattern to incorporate one of the many distance computations. We defined a class, Distance, and made sure each distance computation was a subclass. The classifier algorithm was given the distance computation as a parameter.

  • The classifier is a Facade that provides a uniform interface for testing and evaluating a sample. Each variation on the classifier used a slightly different data structure for managing the collection of nearest neighbors. We don’t want to sort a large training set; we only want to track the subset of nearest neighbors.

Patterns applied to training data

The training data ...