4/22/2016

Delegation from ConvexPolygon to Vector

In the convex polygon problem, we are asked to compute the perimeter of a polygon. We have two obvious options.

Option 1: Perimeter of a convex polygon as a C function 

A convex polygon is an argument passed to this function, which calls the convex polygon’s member function vertex(int) to get vertices to compute distance. The vertex, in turn, is a Vector. So the perimeter function computes distance by getting the components of the two vectors, and then does the square root of sums of square of difference in corresponding components. While all this is going on, perimeter needs to check that the two vectors are of the same dimension.

The perimeter function we just described is busy, nosy, and doing all the work.

Option 2: Perimeter of a convex polygon as a member function 

We can do better. With classes ConvexPolygon and Vector, what perimeter needs to do has been done to a large degree. So, give the perimeter function to Polygon to have direct accesses to the vertices. Have the vector representing two adjacent vertices (i.e., the difference of the two vectors representing the adjacent vertices) compute its own length, checking dimensionality, etc. So, in computing perimeter, ConvexPolygon delegates much of the computation to Vector:


 

The reason that ConvexPolygon is able to delegate computing work to Vector is partly because Vector encapsulates the data and operations Polygon needs. In this case, the operations include Vector's overloaded operator - and the length function; see lines 22 and 23 above. Thus, encapsulation is a foundation for delegation: think about representing a vector with array on numbers and functions all scattered around; it would have been impossible for ConvexPolygon to delegate the computation. 

This delegation is one-way collaboration because Vector does not delegate computing tasks to ConvexPolygon. In contrast, collaboration is two-way or more; more on this later.