Skip to content

Commit 82bf829

Browse files
committed
Prototype: improve conceptual example
1 parent ce6dc32 commit 82bf829

File tree

2 files changed

+40
-16
lines changed

2 files changed

+40
-16
lines changed

Prototype.Conceptual/Output.txt

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,21 @@
1-
Primitive field values have been carried over to a clone. Yay!
2-
Simple component has been cloned. Yay!
3-
Component with back reference has been cloned. Yay!
4-
Component with back reference is linked to the clone. Yay!
1+
Original values of p1, p2, p3:
2+
p1 instance values:
3+
Name: Jack Daniels, Age: 42, BirthDate: 01/01/77
4+
ID#: 666
5+
p2 instance values:
6+
Name: Jack Daniels, Age: 42, BirthDate: 01/01/77
7+
ID#: 666
8+
p3 instance values:
9+
Name: Jack Daniels, Age: 42, BirthDate: 01/01/77
10+
ID#: 666
11+
12+
Values of p1, p2 and p3 after changes to p1:
13+
p1 instance values:
14+
Name: Frank, Age: 32, BirthDate: 01/01/00
15+
ID#: 7878
16+
p2 instance values (reference values have changed):
17+
Name: Jack Daniels, Age: 42, BirthDate: 01/01/77
18+
ID#: 7878
19+
p3 instance values (everything was kept the same):
20+
Name: Jack Daniels, Age: 42, BirthDate: 01/01/77
21+
ID#: 666

Prototype.Conceptual/Program.cs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
namespace RefactoringGuru.DesignPatterns.Prototype.Conceptual
1414
{
15-
1615
public class Person
1716
{
1817
public int Age;
@@ -22,25 +21,25 @@ public class Person
2221

2322
public Person ShallowCopy()
2423
{
25-
return (Person)this.MemberwiseClone();
24+
return (Person) this.MemberwiseClone();
2625
}
2726

2827
public Person DeepCopy()
2928
{
30-
Person other = (Person)this.MemberwiseClone();
31-
other.IdInfo = new IdInfo(IdInfo.IdNumber);
32-
other.Name = String.Copy(Name);
33-
return other;
29+
Person clone = (Person) this.MemberwiseClone();
30+
clone.IdInfo = new IdInfo(IdInfo.IdNumber);
31+
clone.Name = String.Copy(Name);
32+
return clone;
3433
}
3534
}
3635

3736
public class IdInfo
3837
{
3938
public int IdNumber;
4039

41-
public IdInfo(int IdNumber)
40+
public IdInfo(int idNumber)
4241
{
43-
this.IdNumber = IdNumber;
42+
this.IdNumber = idNumber;
4443
}
4544
}
4645

@@ -54,12 +53,18 @@ static void Main(string[] args)
5453
p1.Name = "Jack Daniels";
5554
p1.IdInfo = new IdInfo(666);
5655

57-
// Perform a shallow copy of p1 and assign it to p2.
56+
// EN: Perform a shallow copy of p1 and assign it to p2.
57+
//
58+
// RU: Выполнить поверхностное копирование p1 и присвоить её p2.
5859
Person p2 = p1.ShallowCopy();
59-
// Make a deep copy of p1 and assign it to p3.
60+
// EN: Make a deep copy of p1 and assign it to p3.
61+
//
62+
// RU: Сделать глубокую копию p1 и присвоить её p3.
6063
Person p3 = p1.DeepCopy();
6164

62-
// Display values of p1, p2 and p3
65+
// EN: Display values of p1, p2 and p3.
66+
//
67+
// RU: Вывести значения p1, p2 и p3.
6368
Console.WriteLine("Original values of p1, p2, p3:");
6469
Console.WriteLine(" p1 instance values: ");
6570
DisplayValues(p1);
@@ -68,7 +73,9 @@ static void Main(string[] args)
6873
Console.WriteLine(" p3 instance values:");
6974
DisplayValues(p3);
7075

71-
// Change the value of p1 properties and display the values of p1, p2 and p3.
76+
// EN: Change the value of p1 properties and display the values of p1, p2 and p3.
77+
//
78+
// RU: Изменить значение свойств p1 и отобразить значения p1, p2 и p3.
7279
p1.Age = 32;
7380
p1.BirthDate = Convert.ToDateTime("1900-01-01");
7481
p1.Name = "Frank";

0 commit comments

Comments
 (0)