# 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:

• Select KochCurveFactory: the currently selected factory is KochCurveFactory.
• Select SnowFlakeFactory: the currently selected factory is SnowFlakeFactory.  This factory uses the previously selected factory (in this case, KochCurveFactory) to manufacture a snow flake Koch curve in its initial state.
• Click Reset: the currently selected factory (SnowFlakeFactory) manufactures a snow flake Koch curve in its initial state, and the result is displayed.  You get a triangle of 3 straight line segments.
• Select  RectangleSnowFlakeFactory: the currently selected factory is RectangleSnowFlakeFactory.  This factory uses the previously selected factory (in this case, SnowFlakeFactory) to manufacture a rectangle snow flake Koch curve in its initial state.
• Click Reset: the currently selected factory (RectangleSnowFlakeFactory) manufactures a snow flake Koch curve in its initial state, and the result is displayed.  You get a rectangle of triangles, where each triangle consists of 3 straight line segments!

### Now, try this sequence instead:

• select KochCurveFactory
• select RectangleSnowFlakeFactory
• select SnowFlakeFactory
• click Reset: you should get a triangle consisting of 3 rectangle snow flake Koch curves, each consist of 4 straight line segments!