Skip to content

Commit a44a715

Browse files
committed
Update prototype examples
1 parent ad42ac4 commit a44a715

File tree

13 files changed

+72
-69
lines changed

13 files changed

+72
-69
lines changed

src/CreationalPatterns/Prototype/PrototypeLibrary/ColorRegistryExample/Color.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ public void Customize(int red, int green, int blue)
2626

2727
public IColorPrototype Clone()
2828
{
29-
Console.WriteLine($"Cloning color RGB: {Red},{Green},{Blue}");
30-
31-
return MemberwiseClone() as IColorPrototype;
29+
Console.WriteLine($"\nCloning color RGB: {Red},{Green},{Blue}");
30+
return (MemberwiseClone() as IColorPrototype)!;
3231
}
33-
}
32+
}

src/CreationalPatterns/Prototype/PrototypeLibrary/ColorRegistryExample/ColorRegistry.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,21 @@ namespace PrototypeLibrary.ColorRegistryExample;
44

55
public class ColorRegistry
66
{
7-
private Dictionary<string, IColorPrototype> _colors = new Dictionary<string, IColorPrototype>();
7+
private readonly Dictionary<string, IColorPrototype> _colors = new();
88

99
public IColorPrototype this[string key]
1010
{
11-
get { return _colors[key]; }
12-
set { _colors[key] = value; }
11+
get => _colors[key];
12+
set => _colors[key] = value;
1313
}
14-
}
14+
15+
public void List()
16+
{
17+
Console.WriteLine("Available colors in the registry...");
18+
19+
foreach (var color in _colors)
20+
{
21+
Console.WriteLine($"Name: {color.Key}");
22+
}
23+
}
24+
}

src/CreationalPatterns/Prototype/PrototypeLibrary/ColorRegistryExample/ColorRegistryExecutor.cs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,23 @@ public static void Execute()
88
{
99
ConsoleExtension.WriteSeparator("Color registry example");
1010

11-
var registry = new ColorRegistry();
12-
13-
// Initialize registry with standard colors
14-
registry["red"] = new Color(255, 0, 0);
15-
registry["green"] = new Color(0, 255, 0);
16-
registry["blue"] = new Color(0, 0, 255);
17-
18-
// Clone red color
19-
var clonedColor = registry["red"].Clone() as Color;
11+
// Initialize registry with standard colors.
12+
var registry = new ColorRegistry
13+
{
14+
["red"] = new Color(255, 0, 0),
15+
["green"] = new Color(0, 255, 0),
16+
["blue"] = new Color(0, 0, 255)
17+
};
18+
registry.List();
2019

20+
// Clone red color and store it in the registry.
21+
var clonedColor = (registry["red"].Clone() as Color)!;
2122
clonedColor.Customize(clonedColor.Red, clonedColor.Green, 33);
2223
registry["custom"] = clonedColor;
24+
registry.List();
2325

24-
// Clone custom color
25-
var secondClonedColor = registry["custom"].Clone() as Color;
26+
// Clone custom color without storing it in the registry.
27+
_ = (registry["custom"].Clone() as Color)!;
28+
registry.List();
2629
}
27-
}
30+
}

src/CreationalPatterns/Prototype/PrototypeLibrary/ColorRegistryExample/Common/IColorPrototype.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
/// <summary>
44
/// The Prototype interface declares the cloning methods.
55
/// In most cases, it’s a single Clone method.
6-
/// Instead of manually defining interface, we can use <see cref="System.ICloneable"/>.
6+
/// Instead of manually defining interface, we can use <see cref="ICloneable"/>.
77
/// </summary>
88
public interface IColorPrototype
99
{
1010
IColorPrototype Clone();
11-
}
11+
}

src/CreationalPatterns/Prototype/PrototypeLibrary/ComplexUnstableObjectExample/ComplexUnstableObject.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,5 @@ internal ComplexUnstableObject(string a, int c, char d, string e, string f)
3030
{
3131
}
3232

33-
public object Clone()
34-
{
35-
return MemberwiseClone();
36-
}
37-
}
33+
public object Clone() => MemberwiseClone();
34+
}

src/CreationalPatterns/Prototype/PrototypeLibrary/ComplexUnstableObjectExample/ComplexUnstableObjectExecutor.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public static void Execute()
1111
var complicatedObject = new ComplexUnstableObject("a", 'c', "e");
1212

1313
// Clients from different assembly can't call constructor
14-
// because it's internal,but they can call Clone.
15-
var complicatedObject2 = complicatedObject.Clone();
14+
// because it's internal, but they can call Clone.
15+
var unused = complicatedObject.Clone();
1616
}
17-
}
17+
}

src/CreationalPatterns/Prototype/PrototypeLibrary/DeepCloneExample/Extensions/SystemExtension.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ public static T DeepClone<T>(this T source)
99
var serialized = JsonConvert.SerializeObject(source);
1010
return JsonConvert.DeserializeObject<T>(serialized);
1111
}
12-
}
12+
}

src/CreationalPatterns/Prototype/PrototypeLibrary/DeepCloneExample/Models/MobilePhone.cs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,17 @@ namespace PrototypeLibrary.DeepCloneExample.Models;
44

55
public class MobilePhone
66
{
7-
public string Manufacturer { get; set; }
8-
public string Model { get; set; }
9-
public OperatingSystem OperatingSystem { get; set; }
7+
public string Manufacturer { get; set; } = string.Empty;
8+
public string Model { get; set; } = string.Empty;
9+
public OperatingSystem OperatingSystem { get; set; } = new();
1010

11-
public void PrintDetails()
12-
{
11+
public void PrintDetails() =>
1312
Console.WriteLine(
1413
$"\nManufacturer: {Manufacturer}" +
1514
$"\nModel: {Model}" +
1615
$"\nOperating system: {OperatingSystem.Name}" +
1716
$"\nOS version: {OperatingSystem.Version}" +
1817
$"\nOS description: {OperatingSystem.Description}");
19-
}
2018

21-
public MobilePhone Clone()
22-
{
23-
return this.DeepClone();
24-
}
25-
}
19+
public MobilePhone Clone() => this.DeepClone();
20+
}

src/CreationalPatterns/Prototype/PrototypeLibrary/DeepCloneExample/Models/OperatingSystem.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
public class OperatingSystem
44
{
5-
public string Name { get; set; }
6-
public string Version { get; set; }
7-
public string Description { get; set; }
8-
}
5+
public string Name { get; set; } = string.Empty;
6+
public string Version { get; set; } = string.Empty;
7+
public string Description { get; set; } = string.Empty;
8+
}

src/CreationalPatterns/Prototype/PrototypeLibrary/LogicPuzzleExample/LogicPuzzle.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,5 @@ public void PrintState()
7272
Console.WriteLine($"Move count: {_moveCount}");
7373
}
7474

75-
public object Clone()
76-
{
77-
return MemberwiseClone();
78-
}
79-
}
75+
public object Clone() => MemberwiseClone();
76+
}

0 commit comments

Comments
 (0)