8000 add prototype · RezaJenabi/DesignPatternsCSharp@3e08340 · GitHub
[go: up one dir, main page]

Skip to 8000 content

Commit 3e08340

Browse files
committed
add prototype
1 parent fd2e12f commit 3e08340

File tree

5 files changed

+111
-4
lines changed

5 files changed

+111
-4
lines changed

Src/Creational/Prototype/Address.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
namespace Prototype
2+
{
3+
public class Address
4+
{
5+
private string _city;
6+
7+
public Address(string city)
8+
{
9+
SetStreet(city);
10+
}
11+
12+
public void SetStreet(string city)
13+
{
14+
_city = city;
15+
}
16+
public string GetStreet()
17+
{
18+
return _city;
19+
}
20+
21+
public override string ToString()
22+
{
23+
return $"live in {_city}";
24+
}
25+
}
26+
}

Src/Creational/Prototype/Person.cs

Lines changed: 39 additions & 0 deletions
8000
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
namespace Prototype
2+
{
3+
public class Person
4+
{
5+
private string _name;
6+
7+
public Address Address { get; set; }
8+
9+
public void SetName(string name)
10+
{
11+
_name = name;
12+
}
13+
14+
public string GetName()
15+
{
16+
return _name;
17+
}
18+
19+
public Person ShallowCopy()
20+
{
21+
return (Person)this.MemberwiseClone();
22+
}
23+
24+
public Person DeepCopy()
25+
{
26+
Person clone = (Person)this.MemberwiseClone();
27+
28+
Address address = new Address(Address.GetStreet());
29+
30+
clone.Address = address;
31+
return clone;
32+
}
33+
34+
public override string ToString()
35+
{
36+
return $"my name {_name} live in {Address?.GetStreet()}";
37+
}
38+
}
39+
}

Src/Creational/Prototype/Program.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
3+
namespace Prototype
4+
{
5+
public class Program
6+
{
7+
static void Main(string[] args)
8+
{
9+
Person person = new Person();
10+
person.SetName("Reza");
11+
person.Address = new Address("Tehran");
12+
13+
Console.WriteLine($"Person {person.ToString()}");
14+
15+
Person personShallowCopy = person.ShallowCopy();
16+
Person personDeepCopy = person.DeepCopy();
17+
18+
person.SetName("Amin");
19+
person.Address.SetStreet("Mazandaran");
20+
21+
Console.WriteLine("\nPerson change ... ");
22+
Console.WriteLine($"\nPerson: {person.ToString()}");
23+
Console.WriteLine($"\nPerson Shallow Copy: {personShallowCopy.ToString()}");
24+
Console.WriteLine($"\nPerson Deep Copy: {personDeepCopy.ToString()}");
25+
}
26+
}
27+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>

Src/DesignPatterns.sln

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Creational", "Creational",
99
EndProject
1010
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Structural", "Structural", "{860BC4CA-F738-497E-99D7-CD06270D96EE}"
1111
EndProject
12-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Singleton", "Creational\Singleton\Singleton.csproj", "{3971D467-13D9-439D-9E62-FC450A95BC58}"
12+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Singleton", "Creational\Singleton\Singleton.csproj", "{3971D467-13D9-439D-9E62-FC450A95BC58}"
1313
EndProject
14-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Specification", "Behavioral\Specification\Specification.csproj", "{A72DBE83-A69A-4336-9782-D317BBEC49D2}"
14+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Specification", "Behavioral\Specification\Specification.csproj", "{A72DBE83-A69A-4336-9782-D317BBEC49D2}"
1515
EndProject
16-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FactoryMethod", "Creational\FactoryMethod\FactoryMethod.csproj", "{D44E0C84-6613-4CFF-9858-22C5834B64F8}"
16+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FactoryMethod", "Creational\FactoryMethod\FactoryMethod.csproj", "{D44E0C84-6613-4CFF-9858-22C5834B64F8}"
1717
EndProject
18-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AbstractFactory", "Creational\AbstractFactory\AbstractFactory.csproj", "{4D19C06B-D349-4907-B137-500C37DB783D}"
18+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbstractFactory", "Creational\AbstractFactory\AbstractFactory.csproj", "{4D19C06B-D349-4907-B137-500C37DB783D}"
19+
EndProject
20+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Prototype", "Creational\Prototype\Prototype.csproj", "{2E5A8CCE-007E-4329-B6FB-319BDE24912E}"
1921
EndProject
2022
Global
2123
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -39,6 +41,10 @@ Global
3941
{4D19C06B-D349-4907-B137-500C37DB783D}.Debug|Any CPU.Build.0 = Debug|Any CPU
4042
{4D19C06B-D349-4907-B137-500C37DB783D}.Release|Any CPU.ActiveCfg = Release|Any CPU
4143
{4D19C06B-D349-4907-B137-500C37DB783D}.Release|Any CPU.Build.0 = Release|Any CPU
44+
{2E5A8CCE-007E-4329-B6FB-319BDE24912E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
45+
{2E5A8CCE-007E-4329-B6FB-319BDE24912E}.Debug|Any CPU.Build.0 = Debug|Any CPU
46+
{2E5A8CCE-007E-4329-B6FB-319BDE24912E}.Release|Any CPU.ActiveCfg = Release|Any CPU
47+
{2E5A8CCE-007E-4329-B6FB-319BDE24912E}.Release|Any CPU.Build.0 = Release|Any CPU
4248
EndGlobalSection
4349
GlobalSection(SolutionProperties) = preSolution
4450
HideSolutionNode = FALSE
@@ -48,6 +54,7 @@ Global
4854
{A72DBE83-A69A-4336-9782-D317BBEC49D2} = {207C17C2-7ECE-4DD7-9B18-5A4A838288A2}
4955
{D44E0C84-6613-4CFF-9858-22C5834B64F8} = {DAF1C73D-0282-4B44-B749-B1A6747D24AA}
5056
{4D19C06B-D349-4907-B137-500C37DB783D} = {DAF1C73D-0282-4B44-B749-B1A6747D24AA}
57+
{2E5A8CCE-007E-4329-B6FB-319BDE24912E} = {DAF1C73D-0282-4B44-B749-B1A6747D24AA}
5158
EndGlobalSection
5259
GlobalSection(ExtensibilityGlobals) = postSolution
5360
SolutionGuid = {3FFEEAAE-F81B-4BCD-89B6-CD8AA0075382}

0 commit comments

Comments
 (0)
0