diff --git a/Emne 3/DependencyInversionPrinciple/DependencyInversionPrinciple.sln b/Emne 3/DependencyInversionPrinciple/DependencyInversionPrinciple.sln new file mode 100644 index 0000000..449ea0a --- /dev/null +++ b/Emne 3/DependencyInversionPrinciple/DependencyInversionPrinciple.sln @@ -0,0 +1,16 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DependencyInversionPrinciple", "DependencyInversionPrinciple\DependencyInversionPrinciple.csproj", "{D42BF79A-EB9A-4B1E-A7A8-E0C83D3CD844}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {D42BF79A-EB9A-4B1E-A7A8-E0C83D3CD844}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D42BF79A-EB9A-4B1E-A7A8-E0C83D3CD844}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D42BF79A-EB9A-4B1E-A7A8-E0C83D3CD844}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D42BF79A-EB9A-4B1E-A7A8-E0C83D3CD844}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/Emne 3/DependencyInversionPrinciple/DependencyInversionPrinciple/ChatClient.cs b/Emne 3/DependencyInversionPrinciple/DependencyInversionPrinciple/ChatClient.cs new file mode 100644 index 0000000..ad92640 --- /dev/null +++ b/Emne 3/DependencyInversionPrinciple/DependencyInversionPrinciple/ChatClient.cs @@ -0,0 +1,24 @@ +namespace DependencyInversionPrinciple; + +public class ChatClient +{ + private readonly string _name; + private readonly ChatServer _server; + + public ChatClient(string name, ChatServer server) + { + _name = name; + _server = server; + _server.Register(this); + } + + public void Say(string message) + { + _server.Broadcast(this, $"{_name} sier: {message}"); + } + + public void Receive(string message) + { + Console.WriteLine($"{_name} mottok: {message}"); + } +} \ No newline at end of file diff --git a/Emne 3/DependencyInversionPrinciple/DependencyInversionPrinciple/ChatServer.cs b/Emne 3/DependencyInversionPrinciple/DependencyInversionPrinciple/ChatServer.cs new file mode 100644 index 0000000..43c554d --- /dev/null +++ b/Emne 3/DependencyInversionPrinciple/DependencyInversionPrinciple/ChatServer.cs @@ -0,0 +1,28 @@ +namespace DependencyInversionPrinciple; + +public class ChatServer +{ + private readonly List _clients; + + public ChatServer() + { + _clients = new List(); + } + + public void Broadcast(ChatClient client, string message) + { + foreach (var chatClient in _clients) + { + if (chatClient != client) + { + chatClient.Receive(message); + } + + } + } + + public void Register(ChatClient client) + { + _clients.Add(client); + } +} \ No newline at end of file diff --git a/Emne 3/DependencyInversionPrinciple/DependencyInversionPrinciple/DependencyInversionPrinciple.csproj b/Emne 3/DependencyInversionPrinciple/DependencyInversionPrinciple/DependencyInversionPrinciple.csproj new file mode 100644 index 0000000..2f4fc77 --- /dev/null +++ b/Emne 3/DependencyInversionPrinciple/DependencyInversionPrinciple/DependencyInversionPrinciple.csproj @@ -0,0 +1,10 @@ + + + + Exe + net8.0 + enable + enable + + + diff --git a/Emne 3/DependencyInversionPrinciple/DependencyInversionPrinciple/Program.cs b/Emne 3/DependencyInversionPrinciple/DependencyInversionPrinciple/Program.cs new file mode 100644 index 0000000..ad2ced3 --- /dev/null +++ b/Emne 3/DependencyInversionPrinciple/DependencyInversionPrinciple/Program.cs @@ -0,0 +1,7 @@ +using DependencyInversionPrinciple; + +var server = new ChatServer(); + +var client1 = new ChatClient("Per", server); + +client1.Say("Hello"); \ No newline at end of file