Skip to content

Commit 27432b7

Browse files
committed
add flyweight pattern
1 parent d6fcf4d commit 27432b7

File tree

3 files changed

+71
-3
lines changed

3 files changed

+71
-3
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@ Design Patterns in C# / .NET
1010

1111
| | Pattern |
1212
| ---|--- |
13+
|:heavy_check_mark: | [Factory Method](/CreationalPatterns/FactoryMethod/) |
1314
|:heavy_check_mark: | [Abstract Factory](/CreationalPatterns/AbstractFactory/)|
1415
|| [Builder](/CreationalPatterns/Builder)|
15-
|:heavy_check_mark: | [Factory Method](/CreationalPatterns/FactoryMethod/) |
16-
| | Lazy Initialization
1716
|:heavy_check_mark: | [Prototype](/CreationalPatterns/Prototype) |
1817
|:heavy_check_mark: | [Singleton](/CreationalPatterns/Singleton/) |
1918

@@ -26,7 +25,7 @@ Design Patterns in C# / .NET
2625
|:heavy_check_mark: | [Composite](/StructuralPatterns/Composite) |
2726
|:heavy_check_mark:| [Decorator](/StructuralPatterns/Decorator) |
2827
|:heavy_check_mark:| [Facade](/StructuralPatterns/Facade) |
29-
| | Flyweight |
28+
| | [Flyweight](/StructuralPatterns/Flyweight) |
3029
| | Proxy |
3130

3231
## Behavioral Patterns
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Flyweight
2+
3+
Use sharing to support large numbers of objects efficiently and to save RAM.
4+
5+
## Problem
6+
7+
* Large numbers of objects should be supported efficiently.
8+
* Creating large numbers of objects should be avoided.
9+
10+
When representing large text documents, for example, creating an object for each character in the document would result in a huge amount of objects that couldn't be processed efficiently.
11+
12+
## Solution
13+
14+
Define `Flyweight` objects that
15+
16+
* store intrinsic (invariant) state that can be shared
17+
* *intrinsic state* is context independent, for example the character `A`.
18+
* provide an interface through which extrinsic (variant) state can be passed in
19+
* extrinsic state depends on and varies with the flyweight's context and therefore can't be shared, for example the position of the character `A` in a GUI.
20+
21+
This enables clients to reuse / share Flyweight objects (instead of creating a new object each time) and pass in extrinsic state when they invoke a Flyweight operation.
22+
This greatly reduces the number of physically created objects.
23+
24+
## Common Structure
25+
26+
![Common structure of Flyweight pattern](https://upload.wikimedia.org/wikipedia/commons/4/4e/W3sDesign_Flyweight_Design_Pattern_UML.jpg)
27+
28+
* Flyweight
29+
* declares an interface through which flyweights can receive and act on extrinsic state.
30+
* ConcreteFlyweight
31+
* implements the Flyweight interface and holds the intrinsic state, which must be independent of the object's context.
32+
* UnsharedConcreteFlyweight (not appropriate)
33+
* FlyweightFactory
34+
* creates and manages flyweight objects
35+
* ensures that flyweights are shared properly. When a client requests a flyweight, the FlyweightFactory object supplies an exisiting instance or creates one, if none exists.
36+
* Client
37+
* maintains a reference to flyweights.
38+
* computes or stores the extrinsic state of flyweights.
39+
40+
## Collaboration
41+
42+
* Clients should not instantiate ConcreteFlyweights directly. Clients must obtain ConcreteFlyweight objects exclusively from the FlyweightFactory object to ensure they are shared properly.
43+
44+
## Benefits
45+
46+
* Saves RAM, thus allowing a program to support much more objects.
47+
48+
## Drawbacks
49+
50+
* Wastes CPU time on searching or calculating the context
51+
* Increases overall code complexity by creating multiple additional classes.
52+
53+
## Example
54+
55+
**Definition**
56+
57+
**Usage**
58+
59+
## Comparison with other patterns
60+
61+
* **Composite** - Flyweight is often combined with Composite to implement a shared leaf nodes and save RAM.
62+
* A consequence of sharing is that flyweight leaf nodes cannot store a pointer to their parent. Rather, the parent pointer is passed to the flyweight as part of its extrinsic state. This has a major impact on how the objects in the hierarchy communicate with eath other.
63+
64+
* It's often best to implement `State` and `Strategy` objects as flyweights.
65+
66+
* Flyweight looks almost like `Singleton`. The key differences are
67+
1. Singleton object can be mutable. Flyweight objects are immutable.
68+
2. There should be only one Singleton instance, whereas Flyweight class can have multiple instances with a different intrinsic state.

StructuralPatterns/StructuralPatterns.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
<None Include="doc\Adapter.cd" />
6565
<None Include="doc\Composite.cd" />
6666
<None Include="Facade\README.md" />
67+
<None Include="Flyweight\README.md" />
6768
</ItemGroup>
6869
<ItemGroup />
6970
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

0 commit comments

Comments
 (0)