3

I am confused between the difference of these states for the Flyweight pattern.

I am aware that intrinsic state is the state that is shared and that Extrinsic is not.

However I do not see the importance that extrinsic state has within the pattern or within the example below:

public static void main(String[] args) {
        // Create the flyweight factory...
        EngineFlyweightFactory factory = new EngineFlyweightFactory();
        // Create the diagnostic tool
        DiagnosticTool tool = new EngineDiagnosticTool();
        // Get the flyweights and run diagnostics on them
        Engine standard1 = factory.getStandardEngine(1300); //intrinsic
        standard1.diagnose(tool); //extrinsic

        Engine standard2 = factory.getStandardEngine(1300); //intrinsic
        standard2.diagnose(tool); //extrinsic

        Engine standard3 = factory.getStandardEngine(1300); //intrinsic
        standard3.diagnose(tool); //extrinsic

        Engine standard4 = factory.getStandardEngine(1600); //intrinsic
        standard4.diagnose(tool); //extrinsic

        Engine standard5 = factory.getStandardEngine(1600); //intrinsic
        standard5.diagnose(tool); //extrinsic

In reference to the example given in Wikipedia about a text editior. Is the intrinsic state the letter, and its extrinsic state the font, colour etc?

Dave Schweisguth
  • 36,475
  • 10
  • 98
  • 121

1 Answers1

6

The flyweight pattern is a way to handle a situation where what you're trying to accomplish uses a lot of the same type of object. Rather than create a new instance of an object every time, the flyweight pattern reuses objects, only requiring that you track the differences in their usage. The word processor is the classic example. From your Wikipedia example:

It might be desirable to have, for each character in a document, a glyph object containing its font outline, font metrics, and other formatting data, but this would amount to hundreds or thousands of bytes for each character. Instead, for every character there might be a reference to a flyweight glyph object shared by every instance of the same character in the document; only the position of each character (in the document and/or the page) would need to be stored internally.

This is an accurate description of how the pattern would be used. Following your intrinsic/extrinsic distinction, the glyph objects would be the intrinsic state and the placement of those objects (e.g. what page, paragraph, and line they appear on) would be the extrinsic state/usage of the object.

From your question, where I think you get confused in the example is as to what can be considered an extrinsic/intrinsic attribute. This is likely because the attributes you describe, font style, font colors, and typeface would all be reused as part of the flyweight pattern and so would be "intrinsic" in the sense that they are part of the flyweight pattern but "extrinsic" in the sense that you would account for their application to specific words of letters. For example, in a word processor you likely would have a set of glyphs for the different typefaces in bold, italic, etc; a collection of available colors could also be applied and reused. The program would then track where these attributes were applied but they would not be pure extrinsic states as you describe above.

As for your code above I am not sure how it fits into the flyweight example without seeing any of the code behind it. But from what I can tell it looks similar to the coffee shop example from the Wikipedia article you linked to. The number, whatever that value represents, is unique to the object that is reused.

You should try to get a copy of the original gang of four book on design patterns. The book goes through the word processor example in great detail and that my help you understand more how the pattern works.

Rarw
  • 7,645
  • 3
  • 28
  • 46
  • thanks, is it fair to say that intrinsic state is always used to share and create objects. Whereas extrinsic state is an external state that is used to do something to an object after is has been created? –  May 18 '14 at 16:38
  • Is there no extrinsic state within the coffee shop example? –  May 18 '14 at 16:39
  • Kind of. You're right in that the intrinsic state is what is shared and used in creating objects. I don't think saying the extrinsic state is used to "do something" to an object is fair because in a flyweight pattern all objects are immutable. To change the object you swap it with one of a different type. Rather id say the extrinsic state is the use or application of the object's intrinsic state. – Rarw May 18 '14 at 16:40
  • thanks, can you give me a quick example for "id say the extrinsic state is the use or application of the object's intrinsic state"? –  May 18 '14 at 16:41
  • Sure - in the word processor - a red, italic, times new roman "a" is the intrinsic state of a glyph object, which is built from a collection of reusable color, font, and style objects. The extrinsic state is - page 21, paragraph 2, line 4, third letter from the right. – Rarw May 18 '14 at 16:43
  • ahh i.e. the characters location? –  May 18 '14 at 16:47
  • Right, and the characters are all build from a collection of intrinsic attributes (font, style, typeface, size) that are shared among them. So rather than having a new character instance with its own attributes for each character used on the screen, you would have one character instance for each possible combination - i.e. a red, italic, 12 point, times new roman "a" vs. a blue, bold, strike through, 14 point times new roman "b." You then just store the location of the letter - e.g. italic "a" at positions x, y, z. This way regardless the size of the document there's a finite number of objects – Rarw May 18 '14 at 20:51