Comp 212 Lab 08: Design Patterns for Koch curves


Introduction

This tutorial will lead you through the process of modeling the Koch curves through a series of questions.  By answering these questions, you will have come up with appropriate design patterns to model the Koch curves and complete a major portion of milestone 1 for programming project #2.  It is vital that you familiarize yourself with the general discussion of Koch curves before embarking on this process.


Koch Curve States

Successive iterations through the construction of a Koch curve.
  1. A Koch curve is defined between two points.  The figure above represents the simplest of Koch curves.
    1. What should a Koch curve have as fields?
    2. What is the simplest constructor a Koch curve should then have?
  2. Let K be the initial straight line segment Koch curve in the above figure.  Now, click the Next Iteration button once.  Note that K now contains a bunch of Koch curves.
    1. How many Koch curves does K now have?
    2. What kind of Koch curves are those inside of K?
  3. Now, click the Next Iteration button once again.
    1. How many Koch curves does K now have?
    2. What kind of Koch curves are those inside of K?
    3. How many Koch curves does each of them have?
    4. And what kind of Koch curves are those? (Those that are inside of those that are inside of K!)
    5. Has the behavior of K fundamentally changed from that in step 2?
  4. Now, click the Next Iteration button once again.
    1. How many Koch curves does K now have?
    2. What kind of Koch curves are those inside of K?
    3. Has the behavior of K fundamentally changed from that in step 3?
  5. Let's re-examine the behavior of K.  In the beginning, K appears as a straight line segment.  When we click on the Next Iteration button (step 2), we are basically "asking" K to "grow."  K responds by "changing" itself into something that contains N Koch curves.  When we ask K to grow again (step 3), K responds by asking each of the N Koch curves it contains to grow!  The result is that K appears to have changed shape.  In step 4 when we ask K to grow, K responds in exactly the same way as in step 3: it asks its N Koch curves to grow.  K has stopped changing its "grow" behavior.  Only its shape appears to have changed.  What we have here is the classic phenomenon of "dynamic re-classification": a object appears to have changed its behavior dynamically at run-time as if it has changed classes.  In fact, what happens is the object actually has states, behaves differently in different states, and internally changes its state at run-time.
    1. What design patterns can we use to model the behavior of Koch curves?
    2. How many states does a Koch curve have?
  6. The snow flake Koch curve is a Koch curve whose initial state consists of a set of Koch curves in their initial states.
    1. What class/interface can we use to represent a set of Koch curves?
    2. What Koch curve constructor do we need in order to create snow flake Koch curves?

Koch Curve Abstract Factory

We see from the above discussion that a Koch curve can grow from a "basic" state to a "composite" state where it contains N Koch curves in their own "basic" states.  To endow the Koch curve the capability of growing different kinds of Koch curves dynamically at each growth step, we relegate the task of manufacturing Koch curves to an abstract factory similar to the one for the immutable list structure IList.  Such an abstract factory can be represented either by an interface or an abstract class.

Let's run the demo program again and follow the sequences of actions described below.

Initial State:

When you reset, you are telling the system to manufacture (and display) a Koch curve in its initial state using the currently selected factory.  The initial state of a Koch Curve may be basic but may also be composite.  For example, the initial state of the simple Koch curve is basic, while the initial state of a SnowFlake Koch curve is a composite.

Do the following:

Now, try this sequence instead:

Answering the following questions will help you design the appropriate factories to do the task of manufacturing Koch curves.

  1. What methods should the abstract factory have?
  2. What are the methods' parameters?
  3. How does the abstract factory work with the Koch curve in the growing process?

To answer the above questions, read the hints page for the Koch curve programming project.

 
D. X. Nguyen, 03/12/2007
dxnguyen at rice.edu