Java Reference
In-Depth Information
public double getAtomicWeight()
{
return atomicWeight;
}
}
public static Chemical getChemical(String name)
{
return ??
}
}
CHALLENGE 13.5
Complete the Java code for a ChemicalFactory version that defines
ChemicalImpl as a private, static member class. The inner class implements a
Chemical interface, and the getChemical() method of ChemicalFactory
returns an object of type Chemical .
Summary
The F LYWEIGHT pattern lets you share access to objects, such as characters, chemicals, and
borders, that may appear in large quantities. The flyweight objects must be immutable,
a feature you can establish by extracting the immutable part of the class that you want to
share. To ensure that your flyweight objects are shared, you have to provide a factory in
which clients can find flyweights, and you have to enforce the use of this factory. Visibility
modifiers give you some control over how other developers access your code. Inner classes
take this further, letting you guarantee that a class is accessible by only its containing class.
By ensuring that clients use your flyweight factory properly, you can provide safe, shared
access to what would otherwise be a multitude of fine-grained objects.
Search WWH ::




Custom Search