diff --git a/oop20250222.sln b/oop20250222.sln new file mode 100644 index 0000000..25976ce --- /dev/null +++ b/oop20250222.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.2.32616.157 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "oop20250222", "oop20250222\oop20250222.csproj", "{C11D78EB-758D-4819-BA1D-5F986393304C}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {C11D78EB-758D-4819-BA1D-5F986393304C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C11D78EB-758D-4819-BA1D-5F986393304C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C11D78EB-758D-4819-BA1D-5F986393304C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C11D78EB-758D-4819-BA1D-5F986393304C}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {7B529A88-3579-4852-9E56-D49B5A2F1F48} + EndGlobalSection +EndGlobal diff --git a/oop20250222/Program.cs b/oop20250222/Program.cs new file mode 100644 index 0000000..cdf8499 --- /dev/null +++ b/oop20250222/Program.cs @@ -0,0 +1,52 @@ +using ConsoleApp1.intf; +using ConsoleApp1.pubsub; +using ConsoleApp1.pubsub.generics; + +namespace ConsoleApp1 +{ + internal class Program + { + static void Main(string[] args) + { + IPublisher pub1 = h_generics(); + IPublisher pub2 = h_fixedClasses(); + + string[] arss = new string[] { "EXCEPTION", "INFO", "DEBUG" }; + for (int ii = 0; ii < 10; ii++) + { + Thread.Sleep(1000); + IMessage msg = new Message() + { + Category = arss[Random.Shared.Next(arss.Length)], + Text = $"{ii}" + }; + pub1.Publish(msg); + pub2.Publish(msg); + }; + } + + private static IPublisher h_generics() + { + IPublisher pub = new Publisher(); + ISubscriber sub1 = new ConsoleSubscriber(); + ISubscriber sub2 = new ColoredConsoleSubscriber(); + pub.Subscribe(sub1); + pub.Subscribe(sub2); + return pub; + } + + + private static IPublisher h_fixedClasses() + { + IPublisher pub = new MsgPublisher(); + ISubscriber sub1 = new MsgConsoleSubscriber(); + ISubscriber sub2 = new MsgColoredConsoleSubscriber(); + pub.Subscribe(sub1); + pub.Subscribe(sub2); + return pub; + } + } +} + + + diff --git a/oop20250222/intf/IMessage.cs b/oop20250222/intf/IMessage.cs new file mode 100644 index 0000000..b2d8ede --- /dev/null +++ b/oop20250222/intf/IMessage.cs @@ -0,0 +1,9 @@ +namespace ConsoleApp1.intf +{ + public interface IMessage + { + DateTime EventDate { get; set; } + string Category { get; set; } + string Text { get; set; } + } +} \ No newline at end of file diff --git a/oop20250222/intf/IPublisher.cs b/oop20250222/intf/IPublisher.cs new file mode 100644 index 0000000..2dd0897 --- /dev/null +++ b/oop20250222/intf/IPublisher.cs @@ -0,0 +1,12 @@ +using ConsoleApp1.pubsub; + +namespace ConsoleApp1.intf +{ + + public interface IPublisher + where TMsgType : IMessage + { + void Publish(TMsgType message); + void Subscribe(ISubscriber subscriber); + } +} \ No newline at end of file diff --git a/oop20250222/intf/ISubscriber.cs b/oop20250222/intf/ISubscriber.cs new file mode 100644 index 0000000..61a4325 --- /dev/null +++ b/oop20250222/intf/ISubscriber.cs @@ -0,0 +1,11 @@ +namespace ConsoleApp1.intf +{ + + + public interface ISubscriber + where TMsgType : IMessage + { + void FireEvent(TMsgType message); + } + +} \ No newline at end of file diff --git a/oop20250222/oop20250222.csproj b/oop20250222/oop20250222.csproj new file mode 100644 index 0000000..9ad2a07 --- /dev/null +++ b/oop20250222/oop20250222.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/oop20250222/pubsub/Message.cs b/oop20250222/pubsub/Message.cs new file mode 100644 index 0000000..c16bcd9 --- /dev/null +++ b/oop20250222/pubsub/Message.cs @@ -0,0 +1,16 @@ +using ConsoleApp1.intf; + +namespace ConsoleApp1.pubsub +{ + public class Message : IMessage + { + public DateTime EventDate { get; set; } + + public string Category { get; set; } + public string Text { get; set; } + public override string ToString() + { + return $"[{EventDate:D}] [{Category}] {Text}"; + } + } +} \ No newline at end of file diff --git a/oop20250222/pubsub/MsgColoredConsoleSubscriber.cs b/oop20250222/pubsub/MsgColoredConsoleSubscriber.cs new file mode 100644 index 0000000..29febfd --- /dev/null +++ b/oop20250222/pubsub/MsgColoredConsoleSubscriber.cs @@ -0,0 +1,33 @@ +using ConsoleApp1.intf; +using ConsoleApp1.pubsub.generics; + +namespace ConsoleApp1.pubsub +{ + public class MsgColoredConsoleSubscriber : Subscriber + { + protected override void ObjFire(IMessage sMsg) + { + var cc = Console.ForegroundColor; + var newcc = cc; + + if (sMsg.Category.Contains("EXCEPTION", StringComparison.CurrentCultureIgnoreCase)) + { + newcc = ConsoleColor.Red; + } + else + if (sMsg.Category.Contains("INFO", StringComparison.CurrentCultureIgnoreCase)) + { + newcc = ConsoleColor.Green; + } + else + { + return; + } + + Console.ForegroundColor = newcc; + Console.WriteLine(sMsg); + Console.ForegroundColor = cc; + } + } + +} \ No newline at end of file diff --git a/oop20250222/pubsub/MsgConsoleSubscriber.cs b/oop20250222/pubsub/MsgConsoleSubscriber.cs new file mode 100644 index 0000000..a99702d --- /dev/null +++ b/oop20250222/pubsub/MsgConsoleSubscriber.cs @@ -0,0 +1,14 @@ +using ConsoleApp1.intf; +using ConsoleApp1.pubsub.generics; + +namespace ConsoleApp1.pubsub +{ + public class MsgConsoleSubscriber: Subscriber + { + protected override void ObjFire(IMessage sMsg) + { + Console.WriteLine(sMsg?.Text); + } + } + +} \ No newline at end of file diff --git a/oop20250222/pubsub/MsgPublisher.cs b/oop20250222/pubsub/MsgPublisher.cs new file mode 100644 index 0000000..7a444a7 --- /dev/null +++ b/oop20250222/pubsub/MsgPublisher.cs @@ -0,0 +1,37 @@ +using ConsoleApp1.intf; + +namespace ConsoleApp1.pubsub.generics +{ + internal class MsgPublisher : IPublisher + { + public List> _subscribers = new List>(); + + public void Publish(IMessage message) + { + if (string.IsNullOrEmpty(message?.Text)) + { + return; + } + if (_subscribers.Count == 0) + { + return; + } + // ... + // ... + // ... + foreach (ISubscriber? subscriber in _subscribers) + { + if (subscriber == null) + { + continue; + } + subscriber?.FireEvent(message); + } + } + + public void Subscribe(ISubscriber subscriber) + { + _subscribers.Add(subscriber); + } + } +} \ No newline at end of file diff --git a/oop20250222/pubsub/MsgSubscriber.cs b/oop20250222/pubsub/MsgSubscriber.cs new file mode 100644 index 0000000..15ed863 --- /dev/null +++ b/oop20250222/pubsub/MsgSubscriber.cs @@ -0,0 +1,25 @@ +using ConsoleApp1.intf; + +namespace ConsoleApp1.pubsub +{ + public abstract class MsgSubscriber : ISubscriber + { + public void FireEvent(IMessage message) + { + if (message == null) + { + return; + } + if (String.IsNullOrEmpty(message.Text)) + { + return; + } + string sMsg = $"{DateTime.Now:D} {message.ToString()}"; + message.Text = sMsg; + ObjFire(message); + } + + protected abstract void ObjFire(IMessage sMsg); + } + +} \ No newline at end of file diff --git a/oop20250222/pubsub/generics/ColoredConsoleSubscriber.cs b/oop20250222/pubsub/generics/ColoredConsoleSubscriber.cs new file mode 100644 index 0000000..f3e2852 --- /dev/null +++ b/oop20250222/pubsub/generics/ColoredConsoleSubscriber.cs @@ -0,0 +1,33 @@ +using ConsoleApp1.intf; + +namespace ConsoleApp1.pubsub.generics +{ + public class ColoredConsoleSubscriber : Subscriber + where T : IMessage + { + protected override void ObjFire(T sMsg) + { + var cc = Console.ForegroundColor; + var newcc = cc; + + if (sMsg.Category.Contains("EXCEPTION", StringComparison.CurrentCultureIgnoreCase)) + { + newcc = ConsoleColor.Red; + } + else + if (sMsg.Category.Contains("INFO", StringComparison.CurrentCultureIgnoreCase)) + { + newcc = ConsoleColor.Green; + } + else + { + return; + } + + Console.ForegroundColor = newcc; + Console.WriteLine(sMsg); + Console.ForegroundColor = cc; + } + } + +} \ No newline at end of file diff --git a/oop20250222/pubsub/generics/ConsoleSubscriber.cs b/oop20250222/pubsub/generics/ConsoleSubscriber.cs new file mode 100644 index 0000000..501ab66 --- /dev/null +++ b/oop20250222/pubsub/generics/ConsoleSubscriber.cs @@ -0,0 +1,16 @@ +using ConsoleApp1.intf; + +namespace ConsoleApp1.pubsub.generics +{ + + public class ConsoleSubscriber : Subscriber, ISubscriber + where T : IMessage + { + + protected override void ObjFire(T sMsg) + { + Console.WriteLine(sMsg?.Text); + } + } + +} \ No newline at end of file diff --git a/oop20250222/pubsub/generics/Publisher.cs b/oop20250222/pubsub/generics/Publisher.cs new file mode 100644 index 0000000..b94711d --- /dev/null +++ b/oop20250222/pubsub/generics/Publisher.cs @@ -0,0 +1,38 @@ +using ConsoleApp1.intf; + +namespace ConsoleApp1.pubsub.generics +{ + internal class Publisher : IPublisher + where T : IMessage + { + public List> _subscribers = new List>(); + + public void Publish(T message) + { + if (string.IsNullOrEmpty(message?.Text)) + { + return; + } + if (_subscribers.Count == 0) + { + return; + } + // ... + // ... + // ... + foreach (ISubscriber? subscriber in _subscribers) + { + if (subscriber == null) + { + continue; + } + subscriber?.FireEvent(message); + } + } + + public void Subscribe(ISubscriber subscriber) + { + _subscribers.Add(subscriber); + } + } +} \ No newline at end of file diff --git a/oop20250222/pubsub/generics/Subscriber.cs b/oop20250222/pubsub/generics/Subscriber.cs new file mode 100644 index 0000000..8c13493 --- /dev/null +++ b/oop20250222/pubsub/generics/Subscriber.cs @@ -0,0 +1,25 @@ +using ConsoleApp1.intf; + +namespace ConsoleApp1.pubsub.generics +{ + + public abstract class Subscriber : ISubscriber + where T : IMessage + { + public void FireEvent(T message) + { + if (message == null) + { + return; + } + if (string.IsNullOrEmpty(message.Text)) + { + return; + } + ObjFire(message); + } + + protected abstract void ObjFire(T sMsg); + } + +} \ No newline at end of file diff --git a/oop20250222/report/HtmlReportFormatter.cs b/oop20250222/report/HtmlReportFormatter.cs new file mode 100644 index 0000000..651a2ea --- /dev/null +++ b/oop20250222/report/HtmlReportFormatter.cs @@ -0,0 +1,37 @@ +using System.Text; + +namespace ConsoleApp1.report +{ + public class HtmlReportFormatter : IReportFormatter + { + public byte[] PrepareReport(IReportContent report) + { + StringBuilder sb = new StringBuilder(); + sb.AppendLine(""); + sb.AppendLine($"

{report.ReportDate}"); + foreach (var row in report.ReportRows) + { + sb.AppendLine(""); + foreach (var cell in row.cells) + { + sb.AppendLine(""); + h_Format(sb, cell); + sb.AppendLine(""); + + } + sb.AppendLine(""); + } + sb.AppendLine(""); + sb.AppendLine(""); + return Encoding.UTF8.GetBytes(sb.ToString()); + } + + private void h_Format(StringBuilder sb, string cell) + { + sb.AppendLine(cell); + } + } + + +} \ No newline at end of file diff --git a/oop20250222/report/IReportContent.cs b/oop20250222/report/IReportContent.cs new file mode 100644 index 0000000..58aecc3 --- /dev/null +++ b/oop20250222/report/IReportContent.cs @@ -0,0 +1,13 @@ +namespace ConsoleApp1.report +{ + + + public interface IReportContent + { + string ReportDate { get; set; } + Dictionary Data { get; set; } + List ReportRows { get; set; } + + void Print(HtmlReportFormatter formatter); + } +} \ No newline at end of file diff --git a/oop20250222/report/IReportFormatter.cs b/oop20250222/report/IReportFormatter.cs new file mode 100644 index 0000000..2dd6bfd --- /dev/null +++ b/oop20250222/report/IReportFormatter.cs @@ -0,0 +1,7 @@ +namespace ConsoleApp1.report +{ + public interface IReportFormatter + { + byte[] PrepareReport(IReportContent report); + } +} \ No newline at end of file diff --git a/oop20250222/report/IReportReader.cs b/oop20250222/report/IReportReader.cs new file mode 100644 index 0000000..d3fbb35 --- /dev/null +++ b/oop20250222/report/IReportReader.cs @@ -0,0 +1,23 @@ +namespace ConsoleApp1.report +{ + + public interface IUserViewerReportFacade + { + public IReportContent FillReportFromReader( + IReportReader reader); + } + + + + public interface IUserExporterReportFacade + { + public byte[] PrepareReport( + IReportFormatter formatter, + IReportContent report); + } + + public interface IReportReader + { + void FillReport(IReportContent report); + } +} \ No newline at end of file diff --git a/oop20250222/report/ReportContent.cs b/oop20250222/report/ReportContent.cs new file mode 100644 index 0000000..3f55c03 --- /dev/null +++ b/oop20250222/report/ReportContent.cs @@ -0,0 +1,17 @@ +namespace ConsoleApp1.report +{ + + public class ReportContent : IReportContent + { + public string ReportDate { get; set; } + public Dictionary Data { get; set; } = new Dictionary(); + public List ReportRows { get; set; } = new List(); + + public void Print(HtmlReportFormatter formatter) + { + throw new NotImplementedException(); + } + } + + +} \ No newline at end of file diff --git a/oop20250222/report/ReportFormatter.cs b/oop20250222/report/ReportFormatter.cs new file mode 100644 index 0000000..28b14e5 --- /dev/null +++ b/oop20250222/report/ReportFormatter.cs @@ -0,0 +1,13 @@ +namespace ConsoleApp1.report +{ + public class ReportFormatter : IReportFormatter + { + public byte[] PrepareReport(IReportContent report) + { + // TODO + return new byte[0]; + } + } + + +} \ No newline at end of file diff --git a/oop20250222/report/ReportReader.cs b/oop20250222/report/ReportReader.cs new file mode 100644 index 0000000..afe74ce --- /dev/null +++ b/oop20250222/report/ReportReader.cs @@ -0,0 +1,21 @@ +namespace ConsoleApp1.report +{ + public class ReportReaderMongo : IReportReader + { + public void FillReport(IReportContent report) + { + // TODO + } + } + + public class ReportReaderPgsql : IReportReader + { + public void FillReport(IReportContent report) + { + // TODO + } + } + + + +} \ No newline at end of file diff --git a/oop20250222/report/ReportRow.cs b/oop20250222/report/ReportRow.cs new file mode 100644 index 0000000..8440df9 --- /dev/null +++ b/oop20250222/report/ReportRow.cs @@ -0,0 +1,7 @@ +namespace ConsoleApp1.report +{ + public class ReportRow + { + public List cells = new List(); + } +} \ No newline at end of file