Thursday, July 28, 2016

Flyweight vs Singleton

Implementations seems to be virtually identical, differing only in style, where the flyweight object is created and held by associated objects (containers: pointer objects, caches; factories; or cache-and-factory objects like Spring's BeanFactory) while the Singleton produces and carries the object within the class object.

Other than that, the difference appears to be one of intent: the Singleton expressly denotes one-and-only-one object for whatever reason (e.g. entry-points to be shared concurrently for caching, logging, accessing network resources where it's intended or simply ok to serialize access) while the Flyweight pattern is expressly to cut down on memory usage and does not strictly enforce the notion of one-and-only-one object.

No comments:

Post a Comment