- Immutability
- Extracting the Immutable Part of a Flyweight
- Sharing Flyweights
- Summary
Sharing Flyweights
Extracting the immutable part of an object is half the battle in applying the Flyweight 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 some kind of factory, perhaps a _ChemicalFactory class, with a static method that returns a chemical given its name. You might store chemicals in a hash table, creating known chemicals as part of the factory’s initialization. Figure 13.4 shows a design for a ChemicalFactory.
Figure 13.4 The ChemicalFactory class is a flyweight factory that returns Chemical objects.
The code for ChemicalFactory can use a static constructor to store Chemical objects in a hash map.
using System; using System.Collections; namespace Chemicals { public class ChemicalFactory { private static Hashtable _chemicals = new Hashtable(); static ChemicalFactory () { _chemicals["carbon"] = new Chemical("Carbon", "C", 12); _chemicals["sulfur"] = new Chemical("Sulfur", "S", 32); _chemicals["saltpeter"] = new Chemical("Saltpeter", "KN03", 101); //... } public static Chemical GetChemical(String name) { return (Chemical) _chemicals[name.ToLower()]; } } }
Having created a factory for chemicals, you now have to take steps to ensure that other developers use this factory instead of instantiating the Chemical class themselves. A simple approach is to rely on the accessibility of the Chemical class.
Challenge 13.3</p>
How can you use the accessibility of the Chemical class to discourage other developers from instantiating Chemical objects?
A solution appears on page 380.
Access modifiers do not supply the complete control over instantiation that you might want. You might like to ensure that _ChemicalFactory is absolutely the only class that can create new _Chemical instances. To achieve this level of control, you can apply a nested class, defining the Chemical class within ChemicalFactory.
To access a nested type, clients must specify the enclosing type with expressions such as the following:
ChemicalFactory.Chemical c = ChemicalFactory.GetChemical("saltpeter");
You can simplify the use of a nested class by introducing an IChemical interface and making the name of the class ChemicalImpl. The IChemical interface can specify three readable properties as follows:
public interface IChemical { string Name { get; } string Symbol { get; } double AtomicWeight { get; } }
Clients will never reference the inner class directly, so you can make it private, ensuring that only _ChemicalFactory2 has access to it.
using System; using System.Collections; namespace Chemicals { public class ChemicalFactory2 { private static Hashtable _chemicals = new Hashtable(); /* challenge! */ : IChemical { private String _name; private String _symbol; private double _atomicWeight; internal ChemicalImpl ( String name, String symbol, double atomicWeight) { _name = name; _symbol = symbol; _atomicWeight = atomicWeight; } public string Name { get { return _name; } } public string Symbol { get { return _symbol; } } public double AtomicWeight { get { return _atomicWeight; } } } /* challenge! */ ChemicalFactory2 () { _chemicals["carbon"] = new ChemicalImpl("Carbon", "C", 12); _chemicals["sulfur"] = new ChemicalImpl("Sulfur", "S", 32); _chemicals["saltpeter"] = new ChemicalImpl("Saltpeter", "KN03", 101); //... } public static IChemical GetChemical(String name) { return /* challenge! */; } } }
Challenge 13.4
Complete the code for the ChemicalFactory.cs.
A solution appears on page 380.