main
syneffort 2 years ago
parent b8d8258a6b
commit 96c37dc68a
  1. 6
      DesignPatternGuru/DesignPatternGuru.sln
  2. 48
      DesignPatternGuru/Prototype/Program.cs
  3. 10
      DesignPatternGuru/Prototype/Prototype.csproj
  4. 18
      DesignPatternGuru/Prototype/Prototypes/IdInfo.cs
  5. 30
      DesignPatternGuru/Prototype/Prototypes/Person.cs

@ -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

@ -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}");
}
}
}

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

@ -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;
}
}
}

@ -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;
}
}
}
Loading…
Cancel
Save