Summary: In this lab, you will get a concrete look at another
motivating example of the flyweight pattern.
Background
In class, we discussed that the flyweight pattern is used when we have
a great number of objects of a particular type, but a great deal of
the information in these objects can actually be shared. Storing the
information repeatedly in each of our numerous objects can create a
huge memory (and sometimes computational) burden.
The alternative is to package that sharable information--called
the
extrinsic state of the objects--into its own cohesive
object. Our original objects would then keep the non-shareable
information--called the
intrinsic state--and
maintain
references to other objects containing extrinsic
data.
Preparations
-
Create a directory for this laboratory exercise:
mkdir somewhere/flyweight
cd somewhere/flyweight
-
Copy the date class to your directory:
cp ~weinman/courses/CSC207/code/book/src/Date.java .
-
Browse the date class to remind yourself of its functionality.
-
Compile the date class.
Exercises
Exercise 1
-
Create a driver class called
TestFlyweight.
-
In a
main method, declare, create, and populate an array
of Date objects with 30 dates. Fix the year and the
month to be the same for all dates, and use a value for the day equal to the
index of the Date. For instance:
dates[day] = new Date(2,day,2011);
Be careful to consistently either handle or ignore appropriately
the case when day == 0.
-
Declare, create, and populate another
Date array with
1,000,000 dates using the same year and the month as before, but
generate a random day. You can do that by first creating a random
number generator as follows:
java.util.Random rand = new java.util.Random(42); // Seed random number generator
...
randDate = new Date(2, rand.nextInt(30)+1, 2011);
Note that the random number generated will be between 0 (inclusive) and 30 (exclusive). To exclude 0 and include 30, I have added one.
-
Compile and run your program, fixing any errors.
Exercise 2
-
After you have populated both arrays, add a "breakpoint" to your
program so that it pauses execution while waiting for user input:
System.out.println("Press enter to continue");
(new java.util.Scanner(System.in)).nextLine();
-
Compile and run your program. While it is paused, use
the
ps command from another Terminal to examine the
memory footprint of the Java Virtual Machine (JVM):
$ ps -ao rss,command | grep java
The first column is the memory usage of the program in
bytes. Record this value.
Exercise 3
To truly be a flyweight, all the date references that are "equal"
should refer to the same object. Rather than generate a new date
object, let us refer to the original canonical set of dates. Change
the line that creates the random day to something like the following:
randDates = dates[rand.nextInt(30)];
Pay careful attention to the indexing implicit in the command and
make sure it agrees with your own arrays, making modifications as necessary.
Compile and run your program, fixing any errors you may encounter.
Exercise 4
While your new flyweight-enabled program is paused, use
the
ps command from another Terminal to examine the
memory footprint of the Java Virtual Machine (JVM):
$ ps -ao rss,command | grep java
How does this compare to the original memory usage?