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
- A Koch curve is defined between two points. The figure above
represents the simplest of Koch curves.
- What should a Koch curve have as fields?
- What is the simplest constructor a Koch curve should then have?
- 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.
- How many Koch curves does K now have?
- What kind of Koch curves are those inside of K?
- Now, click the Next Iteration button once again.
- How many Koch curves does K now have?
- What kind of Koch curves are those inside of K?
- How many Koch curves does each of them have?
- And what kind of Koch curves are those? (Those that are inside of
those that are inside of K!)
- Has the behavior of K fundamentally changed from that in step 2?
- Now, click the Next Iteration button once again.
- How many Koch curves does K now have?
- What kind of Koch curves are those inside of K?
- Has the behavior of K fundamentally changed from that in step 3?
- 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.
- What design patterns can we use to model the behavior of Koch curves?
- How many states does a Koch curve have?
- The snow flake Koch curve is a Koch curve whose initial state consists of
a set of Koch curves in their initial states.
- What class/interface can we use to represent a set of Koch curves?
- 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!
Answering the following questions will help you design the appropriate
factories to do the task of manufacturing Koch curves.
- What methods should the abstract factory have?
- What are the methods' parameters?
- 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, Mar. 06, 2006
dxnguyen at rice.edu