Last active
October 2, 2018 19:11
-
-
Save asarnaout/b92f7dec1171aeaf6afb331bf7ec5a43 to your computer and use it in GitHub Desktop.
Flyweight Pattern Gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ZoneFactory | |
{ | |
private readonly IDictionary<string, Zone> _zones = new Dictionary<string, Zone>(); | |
private readonly object _lock = new object(); | |
public Zone GetZone(string zipCode) | |
{ | |
lock (_lock) | |
{ | |
if (_zones.TryGetValue(zipCode, out var zone)) | |
{ | |
return zone; | |
} | |
zone = new Zone(zipCode); | |
_zones.Add(zipCode, zone); | |
return zone; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment