javadesign-patternsstate-patternflyweight-pattern

State and FlyWeight patterns


Can anybody share example of using state pattern with flyweight pattern (flyweight pattern is for creating state objects to save memory)?

UPDATE: How to use a combination of state and fw patterns?


Solution

  • Autoboxing uses the flyweight pattern to minimise object creation (for small values of Integer)

    e.g. for Boolean and Byte all possible values are cached.

    Java uses states for many components, however a state machine also includes functionality switched by state.

    Here is an example I wrote using enum http://vanillajava.blogspot.com/2011/06/java-secret-using-enum-as-state-machine.html