3

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?

jaco0646
  • 15,303
  • 7
  • 59
  • 83
drifter
  • 683
  • 1
  • 11
  • 24

2 Answers2

3

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

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • *"Autoboxing is an example of a flyweight pattern"* [sic]... Isn't that a bit of a weird wording and an even weirder example!? One could say that the caching/reuse for small values is really a detail compared to what autoboxing does. Moreover the goal of the flyweight pattern is to save memory and autoboxing is the **last** thing to save memory. I won't even mention the waste of memory when storing integer {key,values} in a default Java Map compared to the equivalent in a Trove TIntIntHashMap. So I think saying "Autoboxing is an example of a flyweight pattern" may be a bit weird. – TacticalCoder Feb 04 '12 at 12:18
  • 1
    Flyweights are usually just details compared to whatever the function actually does. This is because they are designed to be largely transparent. i.e. done purely for performance reasons. – Peter Lawrey Feb 04 '12 at 12:45
  • The difference between using HashMap and TIntIntHashMap is in the way entries are organised. If the integers are cached the size would otherwise be the same. e.g. if you compare `TIntArrayList` and `ArrayList` for caches valued. – Peter Lawrey Feb 04 '12 at 12:47
  • 1
    +1 I like your reworded version much better: the original wording was a bit weird, which was my point ; ) Yup, on 32-bit VMs if you were to put only entries between -128 and 127 the size would be the same but I'd say that this would be quite a special case. On 64-bit VMs, even the latest Sun ones using compressed pointers by default, it would still be a bit bigger (if I'm not mistaken). But anyway, it wasn't my point: I was just saying that *"Autoboxing is an example of a flyweight pattern"* [sic] sounded a bit weird to me, I like your new edit way better, +1 ! : ) – TacticalCoder Feb 04 '12 at 19:29
  • Sun/Oracle 64-bit JVM 6+ uses compressed refernces by default (unless the heap is 32+ GB) – Peter Lawrey Feb 04 '12 at 19:42
  • *"Sun/Oracle 64-bit JVM 6+ uses compressed references by default"*... Only since 6u23+ (if my memory serves well and if I'm not mistaken ; ), which is exactly why I wrote *"even the latest Sun ones using compressed pointers by default"* ; ) – TacticalCoder Feb 04 '12 at 19:53
1

I usually use state pattern to avoid conditional statements.

instead of using:

switch (state)
{
    case ParserState.BeforeMethod:
        //do some processing
        break;
    case ParserState.InMethod:
        //do some processing
        break;
}

I can just write:

currentState = currentState.process(context);

A sample class can look like

public class SomeClass : ParserState
{
    public ParserState process(IParserContext context)
    {
       //do some proceccing

       if (m_completed)
           return new SomeOtherState();

       return this;
    }

}

i.e. The logic is moved to small classes which are used to handle a specific state. So you get two things:

  • Smaller classes with clear responsibilities
  • Less conditional statements = more readable code.
jgauffin
  • 99,844
  • 45
  • 235
  • 372