Java Flyweight pattern: Extrinsic and Intrinsic states?

1.7k Views Asked by At

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?

1

There are 1 best solutions below

7
On BEST ANSWER

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.