Skip to content

Commit

Permalink
Factory Method design pattern code
Browse files Browse the repository at this point in the history
  • Loading branch information
ozlematayy committed Dec 21, 2023
1 parent 9851d10 commit 4fd3dff
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
6 changes: 6 additions & 0 deletions DesignPatterns/DesignPatterns.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ VisualStudioVersion = 17.6.33829.357
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AbstractFactory", "AbstractFactory\AbstractFactory.csproj", "{7D587025-5DCC-40B0-A93E-7A6E63F4C131}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FactoryMethod", "FactoryMethod\FactoryMethod.csproj", "{B0D97A44-B546-49B6-A3A8-705D7B6A33D0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -15,6 +17,10 @@ Global
{7D587025-5DCC-40B0-A93E-7A6E63F4C131}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7D587025-5DCC-40B0-A93E-7A6E63F4C131}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7D587025-5DCC-40B0-A93E-7A6E63F4C131}.Release|Any CPU.Build.0 = Release|Any CPU
{B0D97A44-B546-49B6-A3A8-705D7B6A33D0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B0D97A44-B546-49B6-A3A8-705D7B6A33D0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B0D97A44-B546-49B6-A3A8-705D7B6A33D0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B0D97A44-B546-49B6-A3A8-705D7B6A33D0}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
10 changes: 10 additions & 0 deletions DesignPatterns/FactoryMethod/FactoryMethod.csproj
Original file line number Diff line number Diff line change
@@ -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>
75 changes: 75 additions & 0 deletions DesignPatterns/FactoryMethod/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#region Product
public interface ILogger
{
void Log(string message);
}

#endregion
#region Concrete Product
public class ConsoleLogger : ILogger
{
public void Log(string message)
{
Console.WriteLine($"Console Logger: {message}");
}
}

public class DatabaseLogger : ILogger
{
public void Log(string message)
{
Console.WriteLine($"Database Logger: {message}");
}
}
#endregion
#region Concrete Creator
public class LoggerFactory
{
public ILogger GetLogger(string loggerType)
{
if (loggerType.Equals("Console"))
{
return new ConsoleLogger();
}
else if (loggerType.Equals("Database"))
{
return new DatabaseLogger();
}
else
{
throw new ArgumentException("Invalid logger type!");
}
}
}
#endregion
#region Client
public class LoggingClient
{
private readonly LoggerFactory loggerFactory;

public LoggingClient(LoggerFactory factory)
{
loggerFactory = factory;
}

public void LogMessage(string message, string loggerType)
{
ILogger logger = loggerFactory.GetLogger(loggerType);
logger.Log(message);
}
}
#endregion
#region Running the code.
class Program
{
static void Main(string[] args)
{
LoggerFactory loggerFactory = new LoggerFactory();
LoggingClient loggingClient = new LoggingClient(loggerFactory);

loggingClient.LogMessage("This message will be logged in a console.", "Console");
loggingClient.LogMessage("This message will be logged in a database.", "Database");
//loggingClient.LogMessage("This message will be logged in a database.", "Email");
}
}
#endregion

0 comments on commit 4fd3dff

Please sign in to comment.