But doing so unnecessarily sacrifices flexibility. If, on the other hand, you are unsure about which combination you wish to use and whether that mapping will be applied globally to the product, then you need to control the mappings at runtime. The wonderful Gang of Four comes to our aid again. The pattern we need this time is the Factory Method. The black box is a factory.
Factories simply produce objects, usually of a single specific type but with a generic interface.
Just as a toy factory produces toys, a SessionWrapperFactory manufactures SessionWrappers . The SessionWrapper 's creation and function are invisible to the RequestWrapper . The RequestWrapper simply asks the factory for a SessionWrapper instance, and the factory obliges. By now you might be thoroughly confused, so let's jump straight into the code. The RequestWrapper requires a handle to the raw request and the wrapped response.