Creating objects costs time and resources. The best examples are Java constant string creation, Boolean.valueOf(boolean b)
, or Character valueOf(char c)
, since they never create instances; they return immutable cached instances. To speed up (and keep the memory footprint low), applications use object pools. The difference between the object pool pattern and the flyweight pattern is that the first (creation pattern) is a container that keeps mutable domain objects, while the flyweight (structure pattern) is an immutable domain object. Since they're immutable, their internal state is set on creation, and the extrinsic state is given from outside on each method call.
Most web applications use connection pools—a database connection is created/obtained, used, and sent back to the pool. Since this pattern is so common, it has a name: Connection Flyweight (see http://wiki.c2.com/?ConnectionFlyweight). Other resources, such as sockets or threads (thread pool pattern), also make...