Flyweight

From CSSEMediaWiki
(Difference between revisions)
Jump to: navigation, search
(New page: The Flyweight pattern is an efficient solution when you need to create large numbers of repeated objects. It is useful when your original design requires that so many objects that represen...)
 
Line 1: Line 1:
The Flyweight pattern is an efficient solution when you need to create large numbers of repeated objects. It is useful when your original design requires that so many objects that represent the exact same things are created at run time that they take up too much memory. An example that Wal gave in class was for his [[Parse tree design|parse tree]], where so many node objects were being created when it was run that it filled up all the memory. Many of the node objects were exactly the same, for example an object to represent an "=" character, however a new object is created every time one is found. The Flyweight pattern provides a way to deal with these repeated objects so that only one of each type is ever stored in memory which is shared among whatever contexts need to use it.
+
The Flyweight pattern is an efficient solution when you need to create large numbers of repeated objects. It is useful when your original design requires that so many objects that represent the exact same things are created at run time that they take up too much memory. An example that [[User:Warwick Irwin|Wal]] gave in class was for his [[Parse tree design|parse tree]], where so many node objects were being created when it was run that it filled up all the memory. Many of the node objects were exactly the same, for example an object to represent an "=" character, however a new object is created every time one is found. The Flyweight pattern provides a way to deal with these repeated objects so that only one of each type is ever stored in memory which is shared among whatever contexts need to use it.
  
 
The Flyweight pattern structure looks like this:
 
The Flyweight pattern structure looks like this:

Revision as of 21:38, 29 September 2008

The Flyweight pattern is an efficient solution when you need to create large numbers of repeated objects. It is useful when your original design requires that so many objects that represent the exact same things are created at run time that they take up too much memory. An example that Wal gave in class was for his parse tree, where so many node objects were being created when it was run that it filled up all the memory. Many of the node objects were exactly the same, for example an object to represent an "=" character, however a new object is created every time one is found. The Flyweight pattern provides a way to deal with these repeated objects so that only one of each type is ever stored in memory which is shared among whatever contexts need to use it.

The Flyweight pattern structure looks like this:

Flyweight structure.png


Flyweight objects can either be shared or unshared. It is important to note that the Flyweight pattern merely enables sharing, it does not enforce it. In the parse tree example, shared objects are for example an "int" type, where there can be many occurrences of the exact same object in various contexts. Unshared objects are for example an ID node that will have a different value field in different contexts, hence these objects cannot be shared.

Intrinsic state means that it must be independent of the object's context.

The FlyweightFactory deals with the Flyweight objects. When a context requires a Flyweight, it tells the Flyweight factory. It searches the pool of existing Flyweights and returns the Flyweight if it exists. If it hasn't been created yet, it creates a new Flyweight, adds it to the pool of existing Flyweights, and returns the Flyweight.

The design of the Flyweight pattern may not seem like a great design, but is the best way to deal with the efficiency problem described earlier.

Personal tools