From 96c37dc68abb7d209a33f8dc42e376892fc1d6c2 Mon Sep 17 00:00:00 2001 From: syneffort Date: Thu, 9 Mar 2023 15:55:22 +0900 Subject: [PATCH] prototype --- DesignPatternGuru/DesignPatternGuru.sln | 6 +++ DesignPatternGuru/Prototype/Program.cs | 48 +++++++++++++++++++ DesignPatternGuru/Prototype/Prototype.csproj | 10 ++++ .../Prototype/Prototypes/IdInfo.cs | 18 +++++++ .../Prototype/Prototypes/Person.cs | 30 ++++++++++++ 5 files changed, 112 insertions(+) create mode 100644 DesignPatternGuru/Prototype/Program.cs create mode 100644 DesignPatternGuru/Prototype/Prototype.csproj create mode 100644 DesignPatternGuru/Prototype/Prototypes/IdInfo.cs create mode 100644 DesignPatternGuru/Prototype/Prototypes/Person.cs diff --git a/DesignPatternGuru/DesignPatternGuru.sln b/DesignPatternGuru/DesignPatternGuru.sln index 4fac2cb..5a81c07 100644 --- a/DesignPatternGuru/DesignPatternGuru.sln +++ b/DesignPatternGuru/DesignPatternGuru.sln @@ -9,6 +9,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbstractFactory", "Abstract EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Builder", "Builder\Builder.csproj", "{FD2896D5-8199-4036-8D45-1CC0D3B4B79F}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Prototype", "Prototype\Prototype.csproj", "{3A8E007E-79D4-499B-8D19-BF745EC9C68E}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -27,6 +29,10 @@ Global {FD2896D5-8199-4036-8D45-1CC0D3B4B79F}.Debug|Any CPU.Build.0 = Debug|Any CPU {FD2896D5-8199-4036-8D45-1CC0D3B4B79F}.Release|Any CPU.ActiveCfg = Release|Any CPU {FD2896D5-8199-4036-8D45-1CC0D3B4B79F}.Release|Any CPU.Build.0 = Release|Any CPU + {3A8E007E-79D4-499B-8D19-BF745EC9C68E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3A8E007E-79D4-499B-8D19-BF745EC9C68E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3A8E007E-79D4-499B-8D19-BF745EC9C68E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3A8E007E-79D4-499B-8D19-BF745EC9C68E}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/DesignPatternGuru/Prototype/Program.cs b/DesignPatternGuru/Prototype/Program.cs new file mode 100644 index 0000000..fbd4dcd --- /dev/null +++ b/DesignPatternGuru/Prototype/Program.cs @@ -0,0 +1,48 @@ +using Prototype.Prototypes; + +namespace Prototype +{ + internal class Program + { + static void Main(string[] args) + { + Person p1 = new Person(); + p1.Age = 42; + p1.BirthDate = Convert.ToDateTime("1977-01-01"); + p1.Name = "Jack Daniels"; + p1.IdInfo = new IdInfo(666); + + Person p2 = p1.ShallowCopy(); + Person p3 = p1.DeepCopy(); + + Console.WriteLine("Original values of p1, p2, p3"); + Console.WriteLine(" p1 instance value: "); + DisplayValues(p1); + Console.WriteLine(" p2 instance value: "); + DisplayValues(p2); + Console.WriteLine(" p3 instance value: "); + DisplayValues(p3); + + Console.WriteLine(); + + p1.Age = 32; + p1.BirthDate = Convert.ToDateTime("1900-01-01"); + p1.Name = "Frank Daniels"; + p1.IdInfo.IdNumber = 6767; + + Console.WriteLine("Values of p1, p2, p3"); + Console.WriteLine(" p1 instance value: "); + DisplayValues(p1); + Console.WriteLine(" p2 instance value: "); + DisplayValues(p2); + Console.WriteLine(" p3 instance value: "); + DisplayValues(p3); + } + + private static void DisplayValues(Person person) + { + Console.WriteLine($" Name: {person.Name}, Age: {person.Age}, BirthDate: {person.BirthDate}"); + Console.WriteLine($" ID#: {person.IdInfo.IdNumber}"); + } + } +} \ No newline at end of file diff --git a/DesignPatternGuru/Prototype/Prototype.csproj b/DesignPatternGuru/Prototype/Prototype.csproj new file mode 100644 index 0000000..74abf5c --- /dev/null +++ b/DesignPatternGuru/Prototype/Prototype.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/DesignPatternGuru/Prototype/Prototypes/IdInfo.cs b/DesignPatternGuru/Prototype/Prototypes/IdInfo.cs new file mode 100644 index 0000000..8b692b0 --- /dev/null +++ b/DesignPatternGuru/Prototype/Prototypes/IdInfo.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Prototype.Prototypes +{ + internal class IdInfo + { + public int IdNumber; + + public IdInfo(int idNumber) + { + IdNumber = idNumber; + } + } +} diff --git a/DesignPatternGuru/Prototype/Prototypes/Person.cs b/DesignPatternGuru/Prototype/Prototypes/Person.cs new file mode 100644 index 0000000..34da28f --- /dev/null +++ b/DesignPatternGuru/Prototype/Prototypes/Person.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Prototype.Prototypes +{ + internal class Person + { + public int Age; + public DateTime BirthDate; + public string Name; + public IdInfo IdInfo; + + public Person ShallowCopy() + { + return (Person)this.MemberwiseClone(); + } + + public Person DeepCopy() + { + Person clone = (Person)this.MemberwiseClone(); + clone.IdInfo = new IdInfo(IdInfo.IdNumber); + //clone.Name = this.Name; + + return clone; + } + } +}