Flyweight Pattern
Summary:Use sharing to support large numbers of fine-grained objects efficiently.
Sample Code
Code
public class Filter
{
public Guid ID { get; set; }
public List<string> FilterProperties { get; set; } = new List<string>();
public bool Equals(Filter filter)
{
if(this.FilterProperties.Count != filter.FilterProperties.Count)
return false;
this.FilterProperties.Sort();
filter.FilterProperties.Sort();
if (!this.FilterProperties.SequenceEqual(filter.FilterProperties))
return false;
return true;
}
}
public class PhotoEditor
{
private List<Filter> filterCache = new List<Filter>();
private Dictionary<string,Guid> filters = new Dictionary<string, Guid>();
public void ApplyFilter(string filterName)
{
if(!filters.ContainsKey(filterName))
return;
var filterGuid = filters[filterName];
var filter = filterCache.FirstOrDefault(f => f.ID == filterGuid);
System.Console.WriteLine($"\nFilter Name: {filterName}");
System.Console.WriteLine($"Internal Filter ID: {filterGuid}");
filter.FilterProperties.ForEach(p => System.Console.WriteLine($"\tApplying {p}"));
}
public void AddFilter(string filterName, List<string> filterProperties)
{
var newFilter = new Filter { ID = Guid.NewGuid(), FilterProperties = filterProperties };
Guid existingFilterGuid = Guid.Empty;
foreach (var filter in filterCache)
{
if(filter.Equals(newFilter))
{
existingFilterGuid = filter.ID;
break;
}
}
if(existingFilterGuid != Guid.Empty)
filters.Add(filterName, existingFilterGuid);
else
{
filterCache.Add(newFilter);
filters.Add(filterName, newFilter.ID);
}
}
}
Usage
photoEditor.AddFilter("Preset1", new List<string> { "Contrast" });
photoEditor.AddFilter("Preset2", new List<string> { "Contrast", "Sharpen", "Saturation", "White balance"});
photoEditor.AddFilter("Preset3", new List<string> { "Contrast", "Sharpen", "Saturation", "White balance"});
photoEditor.AddFilter("Preset4", new List<string> { "Saturation" });
photoEditor.AddFilter("Preset5", new List<string> { "Contrast" });
photoEditor.ApplyFilter("Preset1");
photoEditor.ApplyFilter("Preset2");
photoEditor.ApplyFilter("Preset3");
photoEditor.ApplyFilter("Preset4");
photoEditor.ApplyFilter("Preset5");
Output
Filter Name: Preset1
Internal Filter ID: 8d056cb7-9f78-498f-a692-57dfa0674d16
Applying Contrast
Filter Name: Preset2
Internal Filter ID: eb0b6b2d-b4ed-43d1-b46b-b1a8ad06cd53
Applying Contrast
Applying Saturation
Applying Sharpen
Applying White balance
Filter Name: Preset3
Internal Filter ID: eb0b6b2d-b4ed-43d1-b46b-b1a8ad06cd53
Applying Contrast
Applying Saturation
Applying Sharpen
Applying White balance
Filter Name: Preset4
Internal Filter ID: 11ed980b-902a-4e0d-8d9b-05e6908ea2d3
Applying Saturation
Filter Name: Preset5
Internal Filter ID: 8d056cb7-9f78-498f-a692-57dfa0674d16
Applying Contrast
Internal Filter ID: 8d056cb7-9f78-498f-a692-57dfa0674d16
Applying Contrast
Filter Name: Preset2
Internal Filter ID: eb0b6b2d-b4ed-43d1-b46b-b1a8ad06cd53
Applying Contrast
Applying Saturation
Applying Sharpen
Applying White balance
Filter Name: Preset3
Internal Filter ID: eb0b6b2d-b4ed-43d1-b46b-b1a8ad06cd53
Applying Contrast
Applying Saturation
Applying Sharpen
Applying White balance
Filter Name: Preset4
Internal Filter ID: 11ed980b-902a-4e0d-8d9b-05e6908ea2d3
Applying Saturation
Filter Name: Preset5
Internal Filter ID: 8d056cb7-9f78-498f-a692-57dfa0674d16
Applying Contrast
Comments
Post a Comment