Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between intrinsic and extrinsic state as described in Flyweight Pattern?

From the chapter on FlyWeight Pattern inside Gang of Four the FlyWeight pattern is applicable when most object state can be made extrinsic.

What does extrinsic state mean ? I get the feeling that this pattern is used for sharing of objects . If objects are to be shared , then how can that object even have any state at all ?

like image 519
Geek Avatar asked Jan 20 '13 14:01

Geek


2 Answers

Let's take an example of a Word processor:

A Word processor deals with Character objects. The state of Character objects is the character content, the font, style,location etc (as far as the Word processor is concerned). Different documents use different instances of a character. Assuming we are just dealing with a-z chars, different documents use letters from a-z pool but might apply a different font/style. So, if we separate the content of the character from the font/style we can share these characters and this makes sense because the total different types of characters are less (26 in our case but a constant otherwise) compared to different instances of characters used in different documents. Sharing these character instances would mean to share the Character instances content wise and apply context like font/style externally to these characters. Character content is intrinsic state and font/style is extrinsic state. Separating state into intrinsic and extrinsic states led to huge storage savings in the above example.

like image 65
user1168577 Avatar answered Nov 15 '22 20:11

user1168577


extrinsic - state that belongs to the context of the object (external) or unique to that instance

intrinsic - state that naturally belongs to the 'FlyWeight' object and thus should be permanent or immutable (internal) or context free.

like image 44
user3086298 Avatar answered Nov 15 '22 20:11

user3086298