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

StackOverflow https://stackoverflow.com/questions/14425671

Pregunta

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 ?

¿Fue útil?

Solución 3

Whatever the specific wording in that bulleted list, it is important to understand the message: Flyweight applies to the case where an important part of state can be shared among many objects because it is some data that is the same for all of them. Typically the shared state is immutable by nature (i.e., "universal truth"). The example with font faces makes this quite clear; an example from everyday Java is java.util.regex.Pattern, the flyweight, vs. Matcher, the client object that reuses it and holds local extrinsic state. Many Matchers can exist in parallel, all reusing the compiled regex on the inside.

This quote makes things clearer than the one from your question:

The more flyweights are shared, the greater the storage savings. The savings increase with the amount of shared state. The greatest savings occur when the objects use substantial quantities of both intrinsic and extrinsic state, and the extrinsic state can be computed rather than stored. Then you save on storage in two ways: Sharing reduces the cost of intrinsic state, and you trade extrinsic state for computation time.

Otros consejos

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.

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.

Gang of Four’s Flyweight design pattern introduces the concept of intrinsic and extrinsic states:

The key concept here is the distinction between intrinsic and extrinsic state. Intrinsic state is stored in the flyweight; it consists of information that’s independent of the flyweight’s context, thereby making it sharable. Extrinsic state depends on and varies with the flyweight’s context and therefore can’t be shared. Client objects are responsible for passing extrinsic state to the flyweight when it needs it.

In other words, the state of an object can be decomposed with respect to a group of objects as an intrinsic state and an extrinsic state, where the intrinsic state is the intersection of the states of all objects of the group and the extrinsic state is the difference of the state of the object and the intrinsic state. Since the intrinsic state is duplicated in each object of the group, space can be saved by replacing the group of objects by a single flyweight object storing a single intrinsic state. The flyweight object cannot however store the multiple extrinsic states of the objects of the group, so the extrinsic states are stored outside and passed to the flyweight object in each request from client objects. Such an optimised communication protocol is often called a stateless protocol since the flyweight object does not store extrinsic state. Examples of stateless protocols include IP and HTTP (and more generally any REST protocols, where intrinsic state is called resource state and extrinsic state is called application state).

For instance, let’s take three objects with their respective clients:

o1 ← c1
o2 ← c2
o3 ← c3

We can decompose the state of each object with respect to the three objects:

state 1 = intrinsic stateextrinsic state 1
state 2 = intrinsic stateextrinsic state 2
state 3 = intrinsic stateextrinsic state 3

where:

intrinsic state = state 1state 2state 3
extrinsic state 1 = state 1 \ intrinsic state
extrinsic state 2 = state 2 \ intrinsic state
extrinsic state 3 = state 3 \ intrinsic state

Here the intrinsic state is duplicated. So storing it in a single flyweight object (and moving the extrinsic states into clients) saves space:

o ← c1, c2, c3

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top