Java Reference
In-Depth Information
CHALLENGE 13.3
Complete the class diagram in Figure 13.3 to show a refactored Substance class
and a new, immutable Chemical_1 class. Include a getMoles() method in
the Chemical_1 class, so that classes other than Substance can check
a chemical batch's molality.
Figure 13.3. Complete this diagram to extract the immutable aspects of
Substance into the Chemical_1 class.
Sharing Flyweights
Extracting the immutable part of an object is half the battle in applying the F LYWEIGHT
pattern. The remaining work includes creating a flyweight factory that instantiates flyweights
and that arranges for clients to share them. You also have to ensure that clients will use your
factory instead of constructing instances of the flyweight class themselves.
To make chemicals flyweights, you need a factory, perhaps a ChemicalFactory_1 class,
with a static method that returns a chemical given its name. You might store chemicals in
a hash map, creating known chemicals as part of the factory's initialization. Figure 13.4 shows
a design for a ChemicalFactory_1 . The code for ChemicalFactory_1 can use a static
initializer to store Chemical_1 objects in a hash map:
Search WWH ::




Custom Search