Factory method

main
syneffort 2 years ago
parent 0a165336e6
commit a0f6934fed
  1. 25
      DesignPatternGuru/DesignPatternGuru.sln
  2. 10
      DesignPatternGuru/DesignPatternGuru/DesignPatternGuru.csproj
  3. 2
      DesignPatternGuru/DesignPatternGuru/Program.cs
  4. 28
      DesignPatternGuru/FactoryMethod/Client.cs
  5. 17
      DesignPatternGuru/FactoryMethod/Creator/ConcreateCreator1.cs
  6. 17
      DesignPatternGuru/FactoryMethod/Creator/ConcreateCreator2.cs
  7. 22
      DesignPatternGuru/FactoryMethod/Creator/Creator.cs
  8. 10
      DesignPatternGuru/FactoryMethod/FactoryMethod.csproj
  9. 16
      DesignPatternGuru/FactoryMethod/Product/ConcreateProduct1.cs
  10. 16
      DesignPatternGuru/FactoryMethod/Product/ConcreateProduct2.cs
  11. 13
      DesignPatternGuru/FactoryMethod/Product/IProduct.cs
  12. 10
      DesignPatternGuru/FactoryMethod/Program.cs

@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.3.32929.385
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FactoryMethod", "FactoryMethod\FactoryMethod.csproj", "{4F58C486-7121-435B-B7B2-F3ADD0FBE2AE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{4F58C486-7121-435B-B7B2-F3ADD0FBE2AE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4F58C486-7121-435B-B7B2-F3ADD0FBE2AE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4F58C486-7121-435B-B7B2-F3ADD0FBE2AE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4F58C486-7121-435B-B7B2-F3ADD0FBE2AE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3E5A9C6B-2E8A-466E-B5E0-4379902EFFAC}
EndGlobalSection
EndGlobal

@ -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,2 @@
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");

@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FactoryMethod
{
internal class Client
{
public void Main()
{
Console.WriteLine("App: Launched with the ConcreateCreator1");
ClinetCode(new ConcreateCreator1());
Console.WriteLine("---");
Console.WriteLine("App: Launched with the ConcreateCreator2");
ClinetCode(new ConcreateCreator2());
}
private void ClinetCode(Creator creator)
{
Console.WriteLine($"Client: I'm not aware of the creator's class, " +
$"but it still works. {creator.SomeOperation()}");
}
}
}

@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FactoryMethod.Product;
namespace FactoryMethod.Creator.Creator
{
internal class ConcreateCreator1 : Creator
{
public override IProduct FactoryMethod()
{
return new ConcreateProduct1();
}
}
}

@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FactoryMethod.Product;
namespace FactoryMethod.Creator.Creator
{
internal class ConcreateCreator2 : Creator
{
public override IProduct FactoryMethod()
{
return new ConcreateProduct2();
}
}
}

@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FactoryMethod.Product;
namespace FactoryMethod.Creator
{
abstract internal class Creator
{
public abstract IProduct FactoryMethod();
public string SomeOperation()
{
var prodcut = FactoryMethod();
var result = $"Creator: The same creator's code has just worked with {prodcut.Operation()}";
return result;
}
}
}

@ -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,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FactoryMethod.Product
{
internal class ConcreateProduct1 : IProduct
{
public string Operation()
{
return "{Result of ConcreateProduct1}";
}
}
}

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FactoryMethod.Product
{
internal class ConcreateProduct2 : IProduct
{
public string Operation()
{
return "{Result of ConcreateProduct2}";
}
}
}

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FactoryMethod.Product
{
internal interface IProduct
{
public string Operation();
}
}

@ -0,0 +1,10 @@
namespace FactoryMethod
{
internal class Program
{
static void Main(string[] args)
{
new Client().Main();
}
}
}
Loading…
Cancel
Save