Extracting the Immutable Part of a Flyweight
Around Oozinoz, chemicals are as prevalent as characters in a document. The purchasing, engineering, manufacturing, and safety departments are all concerned with directing the flow of thousands of chemicals through the factory. Batches of chemicals are often modeled with instances of the Substance class, shown in Figure 13.1.
Figure 13.1 A Substance object models a physical batch of chemical material.
The Substance class has get- properties for its attributes and also has a Moles property that returns the number of moles—a count of molecules—in the substance. A Substance object represents a quantity of a particular molecule. Oozinoz uses a Mixture class to model combinations of substances. For example, Figure 13.2 shows an object diagram of a batch of black powder.
Figure 13.2 A batch of black powder contains saltpeter, sulfur, and carbon.
Suppose that, given the proliferation of chemicals at Oozinoz, you decide to apply Flyweight to reduce the number of Substance objects in Oozinoz applications. To treat Substance objects as flyweights, a first step is to separate its mutable and immutable parts. Suppose that you decide to refactor the Substance class, extracting its immutable part into a Chemical class.
Complete the class diagram in Figure 13.3 to show a refactored Substance2 class and a new, immutable Chemical class.
A solution appears on page 379.
Figure 13.3 Complete this diagram to extract the immutable aspects of Substance2 into the Chemical class.