-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9851d10
commit 4fd3dff
Showing
3 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |