Skip to content

Commit e1a673f

Browse files
committed
Update flyweight pattern examples
1 parent d2b949a commit e1a673f

File tree

4 files changed

+19
-26
lines changed

4 files changed

+19
-26
lines changed

src/StructuralPatterns/Flyweight/FlyweightLibrary/DotNetStringExample/DotNetStringExecutor.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,16 @@ public static void Execute()
2929
Console.WriteLine($"s1 == s3 -> {ReferenceEquals(s1, s3)}");
3030

3131
// We can make use of Console.ReadLine to accept a string at runtime, and assign it to s4.
32-
// And a surprise, the output it false! By default .NET runtime doesn't replace runtime strings,
33-
// which are instances.
32+
// If you enter 'flyweight' after running the app, the output will be false!
33+
// By default .NET runtime doesn't replace runtime strings, which are instances.
3434
// OUTPUT: false (if you enter 'flyweight')
3535
Console.WriteLine($"s1 == s4 -> {ReferenceEquals(s1, s4)}");
3636

37-
// While .NET runtime by default doesn't replace runtime strings, which are instances, we can still
38-
// check if your string is present inside the shared string pool by using string.Intern method.
37+
// While .NET runtime by default doesn't replace runtime strings, we can still
38+
// check if a runtime string is present inside the shared string pool by using string.Intern method.
3939
// Intern method is like the FlyweightFactory.
4040
// It returns a reference to an existing string if it is part of the pool.
4141
// OUTPUT: true (if you enter 'flyweight')
4242
Console.WriteLine($"s1 == s5 -> {ReferenceEquals(s1, s5)}");
4343
}
44-
}
44+
}

src/StructuralPatterns/Flyweight/FlyweightLibrary/ForestExample/Tree.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,5 @@ public Tree(double latitude, double longitude, TreeType treeType)
1313
_treeType = treeType;
1414
}
1515

16-
public void Render()
17-
{
18-
_treeType.Render(_latitude, _longitude);
19-
}
20-
}
16+
public void Render() => _treeType.Render(_latitude, _longitude);
17+
}

src/StructuralPatterns/Flyweight/FlyweightLibrary/ForestExample/TreeFactory.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,18 @@ public TreeType GetTreeType(string name, KnownColor color, string texture)
1818
{
1919
var key = GetTreeTypeKey(name, color, texture);
2020

21-
if (!_treeTypes.TryGetValue(key, out TreeType treeType))
21+
if (_treeTypes.TryGetValue(key, out var treeType))
2222
{
23-
treeType = new TreeType(name, color, texture);
24-
_treeTypes.Add(key, treeType);
25-
26-
Console.WriteLine($"Registered new type: {name} tree with {color} color and {texture}");
23+
Console.WriteLine($"Returning already initialized {name} tree with {color} color and {texture} from the tree factory...");
24+
return treeType;
2725
}
2826

27+
treeType = new TreeType(name, color, texture);
28+
_treeTypes.Add(key, treeType);
29+
Console.WriteLine($"Registered new type: {name} tree with {color} color and {texture}");
30+
2931
return treeType;
3032
}
3133

32-
private string GetTreeTypeKey(string name, KnownColor color, string texture)
33-
{
34-
return $"{name}-{color}-{texture}";
35-
}
36-
}
34+
private static string GetTreeTypeKey(string name, KnownColor color, string texture) => $"{name}-{color}-{texture}";
35+
}

src/StructuralPatterns/Flyweight/FlyweightLibrary/ForestExample/TreeType.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ namespace FlyweightLibrary.ForestExample;
55
/// <summary>
66
/// The flyweight class contains a portion of the state of a <see cref="Tree"/>.
77
/// These fields store values that are unique for each particular tree.
8-
/// For instance, you won't find here the tree coordinates, but the texture and colors
9-
/// shared between many trees are here.
8+
/// For instance, you won't find here the tree coordinates, but the texture and colors shared between many trees are here.
109
/// Since this data is usually BIG, you'd waste a lot of memory by keeping it in each tree object.
1110
/// Instead, we can extract name, color, texture and other repeating data into a
1211
/// separate object which lots of individual tree objects can reference.
@@ -24,10 +23,8 @@ public TreeType(string name, KnownColor color, string texture)
2423
_texture = texture;
2524
}
2625

27-
public void Render(double latitude, double longitude)
28-
{
26+
public void Render(double latitude, double longitude) =>
2927
Console.WriteLine(
3028
$"{_name} tree with {_color} color and {_texture} is rendered " +
3129
$"at coordinates ({latitude}, {longitude})");
32-
}
33-
}
30+
}

0 commit comments

Comments
 (0)