From 2668d9348d07badd067cf4fba131ff2a41e206cc Mon Sep 17 00:00:00 2001 From: Marinko Date: Sun, 23 Dec 2018 09:27:08 +0100 Subject: [PATCH 1/7] SRP project --- .gitignore | 3 ++ README.md | 5 +-- SRP/SRP.sln | 25 +++++++++++++ SRP/SRP/App.config | 6 ++++ SRP/SRP/FileSaver.cs | 17 +++++++++ SRP/SRP/IEntryManager.cs | 14 ++++++++ SRP/SRP/Program.cs | 29 +++++++++++++++ SRP/SRP/Properties/AssemblyInfo.cs | 36 +++++++++++++++++++ SRP/SRP/SRP.csproj | 58 ++++++++++++++++++++++++++++++ SRP/SRP/ScheduleTask.cs | 15 ++++++++ SRP/SRP/Scheduler.cs | 25 +++++++++++++ SRP/SRP/WorkReport.cs | 23 ++++++++++++ SRP/SRP/WorkReportEntry.cs | 15 ++++++++ 13 files changed, 269 insertions(+), 2 deletions(-) create mode 100644 .gitignore create mode 100644 SRP/SRP.sln create mode 100644 SRP/SRP/App.config create mode 100644 SRP/SRP/FileSaver.cs create mode 100644 SRP/SRP/IEntryManager.cs create mode 100644 SRP/SRP/Program.cs create mode 100644 SRP/SRP/Properties/AssemblyInfo.cs create mode 100644 SRP/SRP/SRP.csproj create mode 100644 SRP/SRP/ScheduleTask.cs create mode 100644 SRP/SRP/Scheduler.cs create mode 100644 SRP/SRP/WorkReport.cs create mode 100644 SRP/SRP/WorkReportEntry.cs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..dbc9440 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/SRP/.vs +/SRP/SRP/bin +/SRP/SRP/obj diff --git a/README.md b/README.md index c1b61e8..aed7660 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ -# solid-principles-csharp -This repository contains source code for the SOLID principles in C# on Code Maze blog +# Single Responsibility Principle +## https://code-maze.com/single-responsibility-principle/ + diff --git a/SRP/SRP.sln b/SRP/SRP.sln new file mode 100644 index 0000000..1007489 --- /dev/null +++ b/SRP/SRP.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.28306.52 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SRP", "SRP\SRP.csproj", "{ACF6D983-7D7E-4AFE-B81A-E038D95197CC}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {ACF6D983-7D7E-4AFE-B81A-E038D95197CC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {ACF6D983-7D7E-4AFE-B81A-E038D95197CC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {ACF6D983-7D7E-4AFE-B81A-E038D95197CC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {ACF6D983-7D7E-4AFE-B81A-E038D95197CC}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {24C8E96E-EA5D-4ADE-9CB2-20C4838A9992} + EndGlobalSection +EndGlobal diff --git a/SRP/SRP/App.config b/SRP/SRP/App.config new file mode 100644 index 0000000..8e15646 --- /dev/null +++ b/SRP/SRP/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/SRP/SRP/FileSaver.cs b/SRP/SRP/FileSaver.cs new file mode 100644 index 0000000..82b5846 --- /dev/null +++ b/SRP/SRP/FileSaver.cs @@ -0,0 +1,17 @@ +using System.IO; + +namespace SRP +{ + public class FileSaver + { + public void SaveToFile(string directoryPath, string fileName, IEntryManager workReport) + { + if (!Directory.Exists(directoryPath)) + { + Directory.CreateDirectory(directoryPath); + } + + File.WriteAllText(Path.Combine(directoryPath, fileName), workReport.ToString()); + } + } +} diff --git a/SRP/SRP/IEntryManager.cs b/SRP/SRP/IEntryManager.cs new file mode 100644 index 0000000..647884a --- /dev/null +++ b/SRP/SRP/IEntryManager.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SRP +{ + public interface IEntryManager + { + void AddEntry(T entry); + void RemoveEntryAt(int index); + } +} diff --git a/SRP/SRP/Program.cs b/SRP/SRP/Program.cs new file mode 100644 index 0000000..4eb7142 --- /dev/null +++ b/SRP/SRP/Program.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SRP +{ + class Program + { + static void Main(string[] args) + { + var report = new WorkReport(); + report.AddEntry(new WorkReportEntry { ProjectCode = "123Ds", ProjectName = "Project1", SpentHours = 5 }); + report.AddEntry(new WorkReportEntry { ProjectCode = "987Fc", ProjectName = "Project2", SpentHours = 3 }); + + var scheduler = new Scheduler(); + scheduler.AddEntry(new ScheduleTask { TaskId = 1, Content = "Do something now.", ExecuteOn = DateTime.Now.AddDays(5) }); + scheduler.AddEntry(new ScheduleTask { TaskId = 2, Content = "Don't forget to...", ExecuteOn = DateTime.Now.AddDays(2) }); + + Console.WriteLine(report.ToString()); + Console.WriteLine(scheduler.ToString()); + + var saver = new FileSaver(); + saver.SaveToFile(@"Reports", "WorkReport.txt", report); + saver.SaveToFile(@"Schedulers", "Schedule.txt", report); + } + } +} diff --git a/SRP/SRP/Properties/AssemblyInfo.cs b/SRP/SRP/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..a72a799 --- /dev/null +++ b/SRP/SRP/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("SRP")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("SRP")] +[assembly: AssemblyCopyright("Copyright © 2018")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("acf6d983-7d7e-4afe-b81a-e038d95197cc")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/SRP/SRP/SRP.csproj b/SRP/SRP/SRP.csproj new file mode 100644 index 0000000..295bd14 --- /dev/null +++ b/SRP/SRP/SRP.csproj @@ -0,0 +1,58 @@ + + + + + Debug + AnyCPU + {ACF6D983-7D7E-4AFE-B81A-E038D95197CC} + Exe + SRP + SRP + v4.5 + 512 + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/SRP/SRP/ScheduleTask.cs b/SRP/SRP/ScheduleTask.cs new file mode 100644 index 0000000..e24d383 --- /dev/null +++ b/SRP/SRP/ScheduleTask.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SRP +{ + public class ScheduleTask + { + public int TaskId { get; set; } + public string Content { get; set; } + public DateTime ExecuteOn { get; set; } + } +} diff --git a/SRP/SRP/Scheduler.cs b/SRP/SRP/Scheduler.cs new file mode 100644 index 0000000..9ed5dfc --- /dev/null +++ b/SRP/SRP/Scheduler.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SRP +{ + public class Scheduler : IEntryManager + { + private readonly List _scheduleTasks; + + public Scheduler() + { + _scheduleTasks = new List(); + } + + public void AddEntry(ScheduleTask entry) => _scheduleTasks.Add(entry); + + public void RemoveEntryAt(int index) => _scheduleTasks.RemoveAt(index); + + public override string ToString() => + string.Join(Environment.NewLine, _scheduleTasks.Select(x => $"Task with id: {x.TaskId} with content: {x.Content} is going to be executed on: {x.ExecuteOn}")); + } +} diff --git a/SRP/SRP/WorkReport.cs b/SRP/SRP/WorkReport.cs new file mode 100644 index 0000000..2710c3d --- /dev/null +++ b/SRP/SRP/WorkReport.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace SRP +{ + public class WorkReport: IEntryManager + { + private readonly List _entries; + + public WorkReport() + { + _entries = new List(); + } + + public void AddEntry(WorkReportEntry entry) => _entries.Add(entry); + + public void RemoveEntryAt(int index) => _entries.RemoveAt(index); + + public override string ToString() => + string.Join(Environment.NewLine, _entries.Select(x => $"Code: {x.ProjectCode}, Name: {x.ProjectName}, Hours: {x.SpentHours}")); + } +} diff --git a/SRP/SRP/WorkReportEntry.cs b/SRP/SRP/WorkReportEntry.cs new file mode 100644 index 0000000..a65139c --- /dev/null +++ b/SRP/SRP/WorkReportEntry.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SRP +{ + public class WorkReportEntry + { + public string ProjectCode { get; set; } + public string ProjectName { get; set; } + public int SpentHours { get; set; } + } +} From 29e23f1020f9062b7fecb14918d59d51e7dbda39 Mon Sep 17 00:00:00 2001 From: Marinko Date: Sun, 30 Dec 2018 14:39:53 +0100 Subject: [PATCH 2/7] OCP commit --- .gitignore | 6 ++++ README.md | 4 +-- SRP/SRP.sln | 25 ------------- SRP/SRP/App.config | 6 ---- SRP/SRP/FileSaver.cs | 17 --------- SRP/SRP/IEntryManager.cs | 14 -------- SRP/SRP/Program.cs | 29 --------------- SRP/SRP/Properties/AssemblyInfo.cs | 36 ------------------- SRP/SRP/SRP.csproj | 58 ------------------------------ SRP/SRP/ScheduleTask.cs | 15 -------- SRP/SRP/Scheduler.cs | 25 ------------- SRP/SRP/WorkReport.cs | 23 ------------ SRP/SRP/WorkReportEntry.cs | 15 -------- 13 files changed, 8 insertions(+), 265 deletions(-) delete mode 100644 SRP/SRP.sln delete mode 100644 SRP/SRP/App.config delete mode 100644 SRP/SRP/FileSaver.cs delete mode 100644 SRP/SRP/IEntryManager.cs delete mode 100644 SRP/SRP/Program.cs delete mode 100644 SRP/SRP/Properties/AssemblyInfo.cs delete mode 100644 SRP/SRP/SRP.csproj delete mode 100644 SRP/SRP/ScheduleTask.cs delete mode 100644 SRP/SRP/Scheduler.cs delete mode 100644 SRP/SRP/WorkReport.cs delete mode 100644 SRP/SRP/WorkReportEntry.cs diff --git a/.gitignore b/.gitignore index dbc9440..798d2a5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,9 @@ /SRP/.vs /SRP/SRP/bin /SRP/SRP/obj +/OCP_First_Example/.vs +/OCP_First_Example/OCP_First_Example/bin +/OCP_First_Example/OCP_First_Example/obj +/OCP_Second_Example/.vs +/OCP_Second_Example/OCP_Second_Example/bin +/OCP_Second_Example/OCP_Second_Example/obj diff --git a/README.md b/README.md index aed7660..e4a4c3d 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ -# Single Responsibility Principle -## https://code-maze.com/single-responsibility-principle/ +# Open Closed Principle +## https://code-maze.com/open-closed-principle/ diff --git a/SRP/SRP.sln b/SRP/SRP.sln deleted file mode 100644 index 1007489..0000000 --- a/SRP/SRP.sln +++ /dev/null @@ -1,25 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 15 -VisualStudioVersion = 15.0.28306.52 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SRP", "SRP\SRP.csproj", "{ACF6D983-7D7E-4AFE-B81A-E038D95197CC}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {ACF6D983-7D7E-4AFE-B81A-E038D95197CC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {ACF6D983-7D7E-4AFE-B81A-E038D95197CC}.Debug|Any CPU.Build.0 = Debug|Any CPU - {ACF6D983-7D7E-4AFE-B81A-E038D95197CC}.Release|Any CPU.ActiveCfg = Release|Any CPU - {ACF6D983-7D7E-4AFE-B81A-E038D95197CC}.Release|Any CPU.Build.0 = Release|Any CPU - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {24C8E96E-EA5D-4ADE-9CB2-20C4838A9992} - EndGlobalSection -EndGlobal diff --git a/SRP/SRP/App.config b/SRP/SRP/App.config deleted file mode 100644 index 8e15646..0000000 --- a/SRP/SRP/App.config +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/SRP/SRP/FileSaver.cs b/SRP/SRP/FileSaver.cs deleted file mode 100644 index 82b5846..0000000 --- a/SRP/SRP/FileSaver.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System.IO; - -namespace SRP -{ - public class FileSaver - { - public void SaveToFile(string directoryPath, string fileName, IEntryManager workReport) - { - if (!Directory.Exists(directoryPath)) - { - Directory.CreateDirectory(directoryPath); - } - - File.WriteAllText(Path.Combine(directoryPath, fileName), workReport.ToString()); - } - } -} diff --git a/SRP/SRP/IEntryManager.cs b/SRP/SRP/IEntryManager.cs deleted file mode 100644 index 647884a..0000000 --- a/SRP/SRP/IEntryManager.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace SRP -{ - public interface IEntryManager - { - void AddEntry(T entry); - void RemoveEntryAt(int index); - } -} diff --git a/SRP/SRP/Program.cs b/SRP/SRP/Program.cs deleted file mode 100644 index 4eb7142..0000000 --- a/SRP/SRP/Program.cs +++ /dev/null @@ -1,29 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace SRP -{ - class Program - { - static void Main(string[] args) - { - var report = new WorkReport(); - report.AddEntry(new WorkReportEntry { ProjectCode = "123Ds", ProjectName = "Project1", SpentHours = 5 }); - report.AddEntry(new WorkReportEntry { ProjectCode = "987Fc", ProjectName = "Project2", SpentHours = 3 }); - - var scheduler = new Scheduler(); - scheduler.AddEntry(new ScheduleTask { TaskId = 1, Content = "Do something now.", ExecuteOn = DateTime.Now.AddDays(5) }); - scheduler.AddEntry(new ScheduleTask { TaskId = 2, Content = "Don't forget to...", ExecuteOn = DateTime.Now.AddDays(2) }); - - Console.WriteLine(report.ToString()); - Console.WriteLine(scheduler.ToString()); - - var saver = new FileSaver(); - saver.SaveToFile(@"Reports", "WorkReport.txt", report); - saver.SaveToFile(@"Schedulers", "Schedule.txt", report); - } - } -} diff --git a/SRP/SRP/Properties/AssemblyInfo.cs b/SRP/SRP/Properties/AssemblyInfo.cs deleted file mode 100644 index a72a799..0000000 --- a/SRP/SRP/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -// General Information about an assembly is controlled through the following -// set of attributes. Change these attribute values to modify the information -// associated with an assembly. -[assembly: AssemblyTitle("SRP")] -[assembly: AssemblyDescription("")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("SRP")] -[assembly: AssemblyCopyright("Copyright © 2018")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] - -// Setting ComVisible to false makes the types in this assembly not visible -// to COM components. If you need to access a type in this assembly from -// COM, set the ComVisible attribute to true on that type. -[assembly: ComVisible(false)] - -// The following GUID is for the ID of the typelib if this project is exposed to COM -[assembly: Guid("acf6d983-7d7e-4afe-b81a-e038d95197cc")] - -// Version information for an assembly consists of the following four values: -// -// Major Version -// Minor Version -// Build Number -// Revision -// -// You can specify all the values or you can default the Build and Revision Numbers -// by using the '*' as shown below: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/SRP/SRP/SRP.csproj b/SRP/SRP/SRP.csproj deleted file mode 100644 index 295bd14..0000000 --- a/SRP/SRP/SRP.csproj +++ /dev/null @@ -1,58 +0,0 @@ - - - - - Debug - AnyCPU - {ACF6D983-7D7E-4AFE-B81A-E038D95197CC} - Exe - SRP - SRP - v4.5 - 512 - true - - - AnyCPU - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - AnyCPU - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/SRP/SRP/ScheduleTask.cs b/SRP/SRP/ScheduleTask.cs deleted file mode 100644 index e24d383..0000000 --- a/SRP/SRP/ScheduleTask.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace SRP -{ - public class ScheduleTask - { - public int TaskId { get; set; } - public string Content { get; set; } - public DateTime ExecuteOn { get; set; } - } -} diff --git a/SRP/SRP/Scheduler.cs b/SRP/SRP/Scheduler.cs deleted file mode 100644 index 9ed5dfc..0000000 --- a/SRP/SRP/Scheduler.cs +++ /dev/null @@ -1,25 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace SRP -{ - public class Scheduler : IEntryManager - { - private readonly List _scheduleTasks; - - public Scheduler() - { - _scheduleTasks = new List(); - } - - public void AddEntry(ScheduleTask entry) => _scheduleTasks.Add(entry); - - public void RemoveEntryAt(int index) => _scheduleTasks.RemoveAt(index); - - public override string ToString() => - string.Join(Environment.NewLine, _scheduleTasks.Select(x => $"Task with id: {x.TaskId} with content: {x.Content} is going to be executed on: {x.ExecuteOn}")); - } -} diff --git a/SRP/SRP/WorkReport.cs b/SRP/SRP/WorkReport.cs deleted file mode 100644 index 2710c3d..0000000 --- a/SRP/SRP/WorkReport.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; - -namespace SRP -{ - public class WorkReport: IEntryManager - { - private readonly List _entries; - - public WorkReport() - { - _entries = new List(); - } - - public void AddEntry(WorkReportEntry entry) => _entries.Add(entry); - - public void RemoveEntryAt(int index) => _entries.RemoveAt(index); - - public override string ToString() => - string.Join(Environment.NewLine, _entries.Select(x => $"Code: {x.ProjectCode}, Name: {x.ProjectName}, Hours: {x.SpentHours}")); - } -} diff --git a/SRP/SRP/WorkReportEntry.cs b/SRP/SRP/WorkReportEntry.cs deleted file mode 100644 index a65139c..0000000 --- a/SRP/SRP/WorkReportEntry.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace SRP -{ - public class WorkReportEntry - { - public string ProjectCode { get; set; } - public string ProjectName { get; set; } - public int SpentHours { get; set; } - } -} From 3fd03ff20fa6188a423ef9f84342e68fd14e3c34 Mon Sep 17 00:00:00 2001 From: Marinko Date: Sun, 30 Dec 2018 14:42:33 +0100 Subject: [PATCH 3/7] OCP Projects --- OCP_First_Example/OCP_First_Example.sln | 25 ++++++++ .../OCP_First_Example/App.config | 6 ++ .../OCP_First_Example/BaseSalaryCalculator.cs | 20 +++++++ .../OCP_First_Example/DeveloperReport.cs | 17 ++++++ .../JuniorDevSalaryCalculator.cs | 18 ++++++ .../OCP_First_Example.csproj | 57 ++++++++++++++++++ .../OCP_First_Example/Program.cs | 24 ++++++++ .../Properties/AssemblyInfo.cs | 36 +++++++++++ .../OCP_First_Example/SalaryCalculator.cs | 30 ++++++++++ .../SeniorDevSalaryCalculator.cs | 18 ++++++ OCP_Second_Example/OCP_Second_Example.sln | 25 ++++++++ .../OCP_Second_Example/App.config | 6 ++ .../OCP_Second_Example/ComputerMonitor.cs | 15 +++++ .../OCP_Second_Example/IFilter.cs | 13 ++++ .../OCP_Second_Example/ISpecification.cs | 13 ++++ .../OCP_Second_Example/MonitorFilter.cs | 14 +++++ .../OCP_Second_Example/MonitorType.cs | 15 +++++ .../MonitorTypeSpecification.cs | 20 +++++++ .../OCP_Second_Example.csproj | 60 +++++++++++++++++++ .../OCP_Second_Example/Program.cs | 39 ++++++++++++ .../Properties/AssemblyInfo.cs | 36 +++++++++++ .../OCP_Second_Example/Screen.cs | 14 +++++ .../OCP_Second_Example/ScreenSpecification.cs | 20 +++++++ 23 files changed, 541 insertions(+) create mode 100644 OCP_First_Example/OCP_First_Example.sln create mode 100644 OCP_First_Example/OCP_First_Example/App.config create mode 100644 OCP_First_Example/OCP_First_Example/BaseSalaryCalculator.cs create mode 100644 OCP_First_Example/OCP_First_Example/DeveloperReport.cs create mode 100644 OCP_First_Example/OCP_First_Example/JuniorDevSalaryCalculator.cs create mode 100644 OCP_First_Example/OCP_First_Example/OCP_First_Example.csproj create mode 100644 OCP_First_Example/OCP_First_Example/Program.cs create mode 100644 OCP_First_Example/OCP_First_Example/Properties/AssemblyInfo.cs create mode 100644 OCP_First_Example/OCP_First_Example/SalaryCalculator.cs create mode 100644 OCP_First_Example/OCP_First_Example/SeniorDevSalaryCalculator.cs create mode 100644 OCP_Second_Example/OCP_Second_Example.sln create mode 100644 OCP_Second_Example/OCP_Second_Example/App.config create mode 100644 OCP_Second_Example/OCP_Second_Example/ComputerMonitor.cs create mode 100644 OCP_Second_Example/OCP_Second_Example/IFilter.cs create mode 100644 OCP_Second_Example/OCP_Second_Example/ISpecification.cs create mode 100644 OCP_Second_Example/OCP_Second_Example/MonitorFilter.cs create mode 100644 OCP_Second_Example/OCP_Second_Example/MonitorType.cs create mode 100644 OCP_Second_Example/OCP_Second_Example/MonitorTypeSpecification.cs create mode 100644 OCP_Second_Example/OCP_Second_Example/OCP_Second_Example.csproj create mode 100644 OCP_Second_Example/OCP_Second_Example/Program.cs create mode 100644 OCP_Second_Example/OCP_Second_Example/Properties/AssemblyInfo.cs create mode 100644 OCP_Second_Example/OCP_Second_Example/Screen.cs create mode 100644 OCP_Second_Example/OCP_Second_Example/ScreenSpecification.cs diff --git a/OCP_First_Example/OCP_First_Example.sln b/OCP_First_Example/OCP_First_Example.sln new file mode 100644 index 0000000..98f9f81 --- /dev/null +++ b/OCP_First_Example/OCP_First_Example.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.28307.136 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OCP_First_Example", "OCP_First_Example\OCP_First_Example.csproj", "{34BA7048-340C-442F-8832-2DBB4E4435B8}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {34BA7048-340C-442F-8832-2DBB4E4435B8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {34BA7048-340C-442F-8832-2DBB4E4435B8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {34BA7048-340C-442F-8832-2DBB4E4435B8}.Release|Any CPU.ActiveCfg = Release|Any CPU + {34BA7048-340C-442F-8832-2DBB4E4435B8}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {4487F760-129B-4A5D-AE8A-8D39BEC1B512} + EndGlobalSection +EndGlobal diff --git a/OCP_First_Example/OCP_First_Example/App.config b/OCP_First_Example/OCP_First_Example/App.config new file mode 100644 index 0000000..8e15646 --- /dev/null +++ b/OCP_First_Example/OCP_First_Example/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/OCP_First_Example/OCP_First_Example/BaseSalaryCalculator.cs b/OCP_First_Example/OCP_First_Example/BaseSalaryCalculator.cs new file mode 100644 index 0000000..102db6f --- /dev/null +++ b/OCP_First_Example/OCP_First_Example/BaseSalaryCalculator.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OCP_First_Example +{ + public abstract class BaseSalaryCalculator + { + protected DeveloperReport DeveloperReport { get; private set; } + + public BaseSalaryCalculator(DeveloperReport developerReport) + { + DeveloperReport = developerReport; + } + + public abstract double CalculateSalary(); + } +} diff --git a/OCP_First_Example/OCP_First_Example/DeveloperReport.cs b/OCP_First_Example/OCP_First_Example/DeveloperReport.cs new file mode 100644 index 0000000..97d5100 --- /dev/null +++ b/OCP_First_Example/OCP_First_Example/DeveloperReport.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OCP_First_Example +{ + public class DeveloperReport + { + public int Id { get; set; } + public string Name { get; set; } + public string Level { get; set; } + public int WorkingHours { get; set; } + public double HourlyRate { get; set; } + } +} diff --git a/OCP_First_Example/OCP_First_Example/JuniorDevSalaryCalculator.cs b/OCP_First_Example/OCP_First_Example/JuniorDevSalaryCalculator.cs new file mode 100644 index 0000000..5c2f55a --- /dev/null +++ b/OCP_First_Example/OCP_First_Example/JuniorDevSalaryCalculator.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OCP_First_Example +{ + public class JuniorDevSalaryCalculator : BaseSalaryCalculator + { + public JuniorDevSalaryCalculator(DeveloperReport developerReport) + :base(developerReport) + { + } + + public override double CalculateSalary() => DeveloperReport.HourlyRate * DeveloperReport.WorkingHours; + } +} diff --git a/OCP_First_Example/OCP_First_Example/OCP_First_Example.csproj b/OCP_First_Example/OCP_First_Example/OCP_First_Example.csproj new file mode 100644 index 0000000..ca9da4b --- /dev/null +++ b/OCP_First_Example/OCP_First_Example/OCP_First_Example.csproj @@ -0,0 +1,57 @@ + + + + + Debug + AnyCPU + {34BA7048-340C-442F-8832-2DBB4E4435B8} + Exe + OCP_First_Example + OCP_First_Example + v4.5 + 512 + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/OCP_First_Example/OCP_First_Example/Program.cs b/OCP_First_Example/OCP_First_Example/Program.cs new file mode 100644 index 0000000..b0e6253 --- /dev/null +++ b/OCP_First_Example/OCP_First_Example/Program.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OCP_First_Example +{ + class Program + { + static void Main(string[] args) + { + var devCalculations = new List + { + new SeniorDevSalaryCalculator(new DeveloperReport {Id = 1, Name = "Dev1", Level = "Senior developer", HourlyRate = 30.5, WorkingHours = 160 }), + new JuniorDevSalaryCalculator(new DeveloperReport {Id = 2, Name = "Dev2", Level = "Junior developer", HourlyRate = 20, WorkingHours = 150 }), + new SeniorDevSalaryCalculator(new DeveloperReport {Id = 3, Name = "Dev3", Level = "Senior developer", HourlyRate = 30.5, WorkingHours = 180 }) + }; + + var calculator = new SalaryCalculator(devCalculations); + Console.WriteLine($"Sum of all the developer salaries is {calculator.CalculateTotalSalaries()} dollars"); + } + } +} diff --git a/OCP_First_Example/OCP_First_Example/Properties/AssemblyInfo.cs b/OCP_First_Example/OCP_First_Example/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..7f26953 --- /dev/null +++ b/OCP_First_Example/OCP_First_Example/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("OCP_First_Example")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("OCP_First_Example")] +[assembly: AssemblyCopyright("Copyright © 2018")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("34ba7048-340c-442f-8832-2dbb4e4435b8")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/OCP_First_Example/OCP_First_Example/SalaryCalculator.cs b/OCP_First_Example/OCP_First_Example/SalaryCalculator.cs new file mode 100644 index 0000000..2998ddd --- /dev/null +++ b/OCP_First_Example/OCP_First_Example/SalaryCalculator.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OCP_First_Example +{ + public class SalaryCalculator + { + private readonly IEnumerable _developerCalculation; + + public SalaryCalculator(IEnumerable developerCalculation) + { + _developerCalculation = developerCalculation; + } + + public double CalculateTotalSalaries() + { + double totalSalaries = 0D; + + foreach (var devCalc in _developerCalculation) + { + totalSalaries += devCalc.CalculateSalary(); + } + + return totalSalaries; + } + } +} diff --git a/OCP_First_Example/OCP_First_Example/SeniorDevSalaryCalculator.cs b/OCP_First_Example/OCP_First_Example/SeniorDevSalaryCalculator.cs new file mode 100644 index 0000000..32ea7c9 --- /dev/null +++ b/OCP_First_Example/OCP_First_Example/SeniorDevSalaryCalculator.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OCP_First_Example +{ + public class SeniorDevSalaryCalculator : BaseSalaryCalculator + { + public SeniorDevSalaryCalculator(DeveloperReport report) + :base(report) + { + } + + public override double CalculateSalary() => DeveloperReport.HourlyRate * DeveloperReport.WorkingHours * 1.2; + } +} diff --git a/OCP_Second_Example/OCP_Second_Example.sln b/OCP_Second_Example/OCP_Second_Example.sln new file mode 100644 index 0000000..352c7e2 --- /dev/null +++ b/OCP_Second_Example/OCP_Second_Example.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.28307.136 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OCP_Second_Example", "OCP_Second_Example\OCP_Second_Example.csproj", "{B17FABB3-452F-45A7-A2EC-81398721C6BA}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {B17FABB3-452F-45A7-A2EC-81398721C6BA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B17FABB3-452F-45A7-A2EC-81398721C6BA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B17FABB3-452F-45A7-A2EC-81398721C6BA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B17FABB3-452F-45A7-A2EC-81398721C6BA}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {FB4B96F7-7F18-4A27-80A3-7A7BF26F1BE0} + EndGlobalSection +EndGlobal diff --git a/OCP_Second_Example/OCP_Second_Example/App.config b/OCP_Second_Example/OCP_Second_Example/App.config new file mode 100644 index 0000000..8e15646 --- /dev/null +++ b/OCP_Second_Example/OCP_Second_Example/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/OCP_Second_Example/OCP_Second_Example/ComputerMonitor.cs b/OCP_Second_Example/OCP_Second_Example/ComputerMonitor.cs new file mode 100644 index 0000000..ead923c --- /dev/null +++ b/OCP_Second_Example/OCP_Second_Example/ComputerMonitor.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OCP_Second_Example +{ + public class ComputerMonitor + { + public string Name { get; set; } + public MonitorType Type { get; set; } + public Screen Screen { get; set; } + } +} diff --git a/OCP_Second_Example/OCP_Second_Example/IFilter.cs b/OCP_Second_Example/OCP_Second_Example/IFilter.cs new file mode 100644 index 0000000..71d6805 --- /dev/null +++ b/OCP_Second_Example/OCP_Second_Example/IFilter.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OCP_Second_Example +{ + public interface IFilter + { + List Filter(IEnumerable monitors, ISpecification specification); + } +} diff --git a/OCP_Second_Example/OCP_Second_Example/ISpecification.cs b/OCP_Second_Example/OCP_Second_Example/ISpecification.cs new file mode 100644 index 0000000..50aa160 --- /dev/null +++ b/OCP_Second_Example/OCP_Second_Example/ISpecification.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OCP_Second_Example +{ + public interface ISpecification + { + bool isSatisfied(T item); + } +} diff --git a/OCP_Second_Example/OCP_Second_Example/MonitorFilter.cs b/OCP_Second_Example/OCP_Second_Example/MonitorFilter.cs new file mode 100644 index 0000000..960c9ef --- /dev/null +++ b/OCP_Second_Example/OCP_Second_Example/MonitorFilter.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OCP_Second_Example +{ + public class MonitorFilter : IFilter + { + public List Filter(IEnumerable monitors, ISpecification specification) => + monitors.Where(m => specification.isSatisfied(m)).ToList(); + } +} diff --git a/OCP_Second_Example/OCP_Second_Example/MonitorType.cs b/OCP_Second_Example/OCP_Second_Example/MonitorType.cs new file mode 100644 index 0000000..45e2855 --- /dev/null +++ b/OCP_Second_Example/OCP_Second_Example/MonitorType.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OCP_Second_Example +{ + public enum MonitorType + { + OLED, + LCD, + LED + } +} diff --git a/OCP_Second_Example/OCP_Second_Example/MonitorTypeSpecification.cs b/OCP_Second_Example/OCP_Second_Example/MonitorTypeSpecification.cs new file mode 100644 index 0000000..c7576c5 --- /dev/null +++ b/OCP_Second_Example/OCP_Second_Example/MonitorTypeSpecification.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OCP_Second_Example +{ + public class MonitorTypeSpecification: ISpecification + { + private readonly MonitorType _type; + + public MonitorTypeSpecification(MonitorType type) + { + _type = type; + } + + public bool isSatisfied(ComputerMonitor item) => item.Type == _type; + } +} diff --git a/OCP_Second_Example/OCP_Second_Example/OCP_Second_Example.csproj b/OCP_Second_Example/OCP_Second_Example/OCP_Second_Example.csproj new file mode 100644 index 0000000..60d11b0 --- /dev/null +++ b/OCP_Second_Example/OCP_Second_Example/OCP_Second_Example.csproj @@ -0,0 +1,60 @@ + + + + + Debug + AnyCPU + {B17FABB3-452F-45A7-A2EC-81398721C6BA} + Exe + OCP_Second_Example + OCP_Second_Example + v4.5 + 512 + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/OCP_Second_Example/OCP_Second_Example/Program.cs b/OCP_Second_Example/OCP_Second_Example/Program.cs new file mode 100644 index 0000000..3847ebb --- /dev/null +++ b/OCP_Second_Example/OCP_Second_Example/Program.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OCP_Second_Example +{ + class Program + { + static void Main(string[] args) + { + var monitors = new List + { + new ComputerMonitor { Name = "Samsung S345", Screen = Screen.CurvedScreen, Type = MonitorType.OLED }, + new ComputerMonitor { Name = "Philips P532", Screen = Screen.WideScreen, Type = MonitorType.LCD }, + new ComputerMonitor { Name = "LG L888", Screen = Screen.WideScreen, Type = MonitorType.LED }, + new ComputerMonitor { Name = "Samsung S999", Screen = Screen.WideScreen, Type = MonitorType.OLED }, + new ComputerMonitor { Name = "Dell D2J47", Screen = Screen.CurvedScreen, Type = MonitorType.LCD } + }; + + var filter = new MonitorFilter(); + + var lcdMonitors = filter.Filter(monitors, new MonitorTypeSpecification(MonitorType.LCD)); + Console.WriteLine("All LCD monitors"); + foreach (var monitor in lcdMonitors) + { + Console.WriteLine($"Name: {monitor.Name}, Type: {monitor.Type}, Screen: {monitor.Screen}"); + } + + Console.WriteLine("All WideScreen Monitors"); + var wideScreenMonitors = filter.Filter(monitors, new ScreenSpecification(Screen.WideScreen)); + foreach (var monitor in wideScreenMonitors) + { + Console.WriteLine($"Name: {monitor.Name}, Type: {monitor.Type}, Screen: {monitor.Screen}"); + } + } + } +} diff --git a/OCP_Second_Example/OCP_Second_Example/Properties/AssemblyInfo.cs b/OCP_Second_Example/OCP_Second_Example/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..a49d7c6 --- /dev/null +++ b/OCP_Second_Example/OCP_Second_Example/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("OCP_Second_Example")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("OCP_Second_Example")] +[assembly: AssemblyCopyright("Copyright © 2018")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("b17fabb3-452f-45a7-a2ec-81398721c6ba")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/OCP_Second_Example/OCP_Second_Example/Screen.cs b/OCP_Second_Example/OCP_Second_Example/Screen.cs new file mode 100644 index 0000000..a14726f --- /dev/null +++ b/OCP_Second_Example/OCP_Second_Example/Screen.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OCP_Second_Example +{ + public enum Screen + { + WideScreen, + CurvedScreen + } +} diff --git a/OCP_Second_Example/OCP_Second_Example/ScreenSpecification.cs b/OCP_Second_Example/OCP_Second_Example/ScreenSpecification.cs new file mode 100644 index 0000000..66159e2 --- /dev/null +++ b/OCP_Second_Example/OCP_Second_Example/ScreenSpecification.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OCP_Second_Example +{ + public class ScreenSpecification : ISpecification + { + private readonly Screen _screen; + + public ScreenSpecification(Screen screen) + { + _screen = screen; + } + + public bool isSatisfied(ComputerMonitor item) => item.Screen == _screen; + } +} From 532a4a92e526bdac6bbc6eff62520be61c7ad551 Mon Sep 17 00:00:00 2001 From: Marinko Date: Sat, 5 Jan 2019 08:49:02 +0100 Subject: [PATCH 4/7] LSP --- .../OCP_First_Example.sln => LSP/LSP.sln | 12 ++-- .../OCP_First_Example => LSP/LSP}/App.config | 0 LSP/LSP/EvenNumbersSumCalculator.cs | 18 ++++++ .../LSP/LSP.csproj | 13 ++-- LSP/LSP/Program.cs | 24 ++++++++ .../LSP}/Properties/AssemblyInfo.cs | 6 +- LSP/LSP/SumCalculator.cs | 20 +++++++ .../OCP_First_Example/BaseSalaryCalculator.cs | 20 ------- .../OCP_First_Example/DeveloperReport.cs | 17 ------ .../JuniorDevSalaryCalculator.cs | 18 ------ .../OCP_First_Example/Program.cs | 24 -------- .../OCP_First_Example/SalaryCalculator.cs | 30 ---------- .../SeniorDevSalaryCalculator.cs | 18 ------ OCP_Second_Example/OCP_Second_Example.sln | 25 -------- .../OCP_Second_Example/App.config | 6 -- .../OCP_Second_Example/ComputerMonitor.cs | 15 ----- .../OCP_Second_Example/IFilter.cs | 13 ---- .../OCP_Second_Example/ISpecification.cs | 13 ---- .../OCP_Second_Example/MonitorFilter.cs | 14 ----- .../OCP_Second_Example/MonitorType.cs | 15 ----- .../MonitorTypeSpecification.cs | 20 ------- .../OCP_Second_Example.csproj | 60 ------------------- .../OCP_Second_Example/Program.cs | 39 ------------ .../Properties/AssemblyInfo.cs | 36 ----------- .../OCP_Second_Example/Screen.cs | 14 ----- .../OCP_Second_Example/ScreenSpecification.cs | 20 ------- README.md | 4 +- 27 files changed, 78 insertions(+), 436 deletions(-) rename OCP_First_Example/OCP_First_Example.sln => LSP/LSP.sln (58%) rename {OCP_First_Example/OCP_First_Example => LSP/LSP}/App.config (100%) create mode 100644 LSP/LSP/EvenNumbersSumCalculator.cs rename OCP_First_Example/OCP_First_Example/OCP_First_Example.csproj => LSP/LSP/LSP.csproj (83%) create mode 100644 LSP/LSP/Program.cs rename {OCP_First_Example/OCP_First_Example => LSP/LSP}/Properties/AssemblyInfo.cs (89%) create mode 100644 LSP/LSP/SumCalculator.cs delete mode 100644 OCP_First_Example/OCP_First_Example/BaseSalaryCalculator.cs delete mode 100644 OCP_First_Example/OCP_First_Example/DeveloperReport.cs delete mode 100644 OCP_First_Example/OCP_First_Example/JuniorDevSalaryCalculator.cs delete mode 100644 OCP_First_Example/OCP_First_Example/Program.cs delete mode 100644 OCP_First_Example/OCP_First_Example/SalaryCalculator.cs delete mode 100644 OCP_First_Example/OCP_First_Example/SeniorDevSalaryCalculator.cs delete mode 100644 OCP_Second_Example/OCP_Second_Example.sln delete mode 100644 OCP_Second_Example/OCP_Second_Example/App.config delete mode 100644 OCP_Second_Example/OCP_Second_Example/ComputerMonitor.cs delete mode 100644 OCP_Second_Example/OCP_Second_Example/IFilter.cs delete mode 100644 OCP_Second_Example/OCP_Second_Example/ISpecification.cs delete mode 100644 OCP_Second_Example/OCP_Second_Example/MonitorFilter.cs delete mode 100644 OCP_Second_Example/OCP_Second_Example/MonitorType.cs delete mode 100644 OCP_Second_Example/OCP_Second_Example/MonitorTypeSpecification.cs delete mode 100644 OCP_Second_Example/OCP_Second_Example/OCP_Second_Example.csproj delete mode 100644 OCP_Second_Example/OCP_Second_Example/Program.cs delete mode 100644 OCP_Second_Example/OCP_Second_Example/Properties/AssemblyInfo.cs delete mode 100644 OCP_Second_Example/OCP_Second_Example/Screen.cs delete mode 100644 OCP_Second_Example/OCP_Second_Example/ScreenSpecification.cs diff --git a/OCP_First_Example/OCP_First_Example.sln b/LSP/LSP.sln similarity index 58% rename from OCP_First_Example/OCP_First_Example.sln rename to LSP/LSP.sln index 98f9f81..a2a44da 100644 --- a/OCP_First_Example/OCP_First_Example.sln +++ b/LSP/LSP.sln @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 VisualStudioVersion = 15.0.28307.136 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OCP_First_Example", "OCP_First_Example\OCP_First_Example.csproj", "{34BA7048-340C-442F-8832-2DBB4E4435B8}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LSP", "LSP\LSP.csproj", "{7D7641E7-44D8-42ED-9443-99A655FA7195}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -11,15 +11,15 @@ Global Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {34BA7048-340C-442F-8832-2DBB4E4435B8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {34BA7048-340C-442F-8832-2DBB4E4435B8}.Debug|Any CPU.Build.0 = Debug|Any CPU - {34BA7048-340C-442F-8832-2DBB4E4435B8}.Release|Any CPU.ActiveCfg = Release|Any CPU - {34BA7048-340C-442F-8832-2DBB4E4435B8}.Release|Any CPU.Build.0 = Release|Any CPU + {7D7641E7-44D8-42ED-9443-99A655FA7195}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7D7641E7-44D8-42ED-9443-99A655FA7195}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7D7641E7-44D8-42ED-9443-99A655FA7195}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7D7641E7-44D8-42ED-9443-99A655FA7195}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {4487F760-129B-4A5D-AE8A-8D39BEC1B512} + SolutionGuid = {10F1FC52-49A2-4C74-83C9-9389C91C8C3F} EndGlobalSection EndGlobal diff --git a/OCP_First_Example/OCP_First_Example/App.config b/LSP/LSP/App.config similarity index 100% rename from OCP_First_Example/OCP_First_Example/App.config rename to LSP/LSP/App.config diff --git a/LSP/LSP/EvenNumbersSumCalculator.cs b/LSP/LSP/EvenNumbersSumCalculator.cs new file mode 100644 index 0000000..5bf1d40 --- /dev/null +++ b/LSP/LSP/EvenNumbersSumCalculator.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LSP +{ + public class EvenNumbersSumCalculator: SumCalculator + { + public EvenNumbersSumCalculator(int[] numbers) + :base(numbers) + { + } + + public override int Calculate() => _numbers.Where(x => x % 2 == 0).Sum(); + } +} diff --git a/OCP_First_Example/OCP_First_Example/OCP_First_Example.csproj b/LSP/LSP/LSP.csproj similarity index 83% rename from OCP_First_Example/OCP_First_Example/OCP_First_Example.csproj rename to LSP/LSP/LSP.csproj index ca9da4b..52b8350 100644 --- a/OCP_First_Example/OCP_First_Example/OCP_First_Example.csproj +++ b/LSP/LSP/LSP.csproj @@ -4,10 +4,10 @@ Debug AnyCPU - {34BA7048-340C-442F-8832-2DBB4E4435B8} + {7D7641E7-44D8-42ED-9443-99A655FA7195} Exe - OCP_First_Example - OCP_First_Example + LSP + LSP v4.5 512 true @@ -42,13 +42,10 @@ - - - - + - + diff --git a/LSP/LSP/Program.cs b/LSP/LSP/Program.cs new file mode 100644 index 0000000..63ff5d0 --- /dev/null +++ b/LSP/LSP/Program.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LSP +{ + class Program + { + static void Main(string[] args) + { + var numbers = new int[] { 5, 7, 9, 8, 1, 6, 4 }; + + SumCalculator sum = new SumCalculator(numbers); + Console.WriteLine($"The sum of all the numbers: {sum.Calculate()}"); + + Console.WriteLine(); + + SumCalculator evenSum = new EvenNumbersSumCalculator(numbers); + Console.WriteLine($"The sum of all the even numbers: {evenSum.Calculate()}"); + } + } +} diff --git a/OCP_First_Example/OCP_First_Example/Properties/AssemblyInfo.cs b/LSP/LSP/Properties/AssemblyInfo.cs similarity index 89% rename from OCP_First_Example/OCP_First_Example/Properties/AssemblyInfo.cs rename to LSP/LSP/Properties/AssemblyInfo.cs index 7f26953..fd7deac 100644 --- a/OCP_First_Example/OCP_First_Example/Properties/AssemblyInfo.cs +++ b/LSP/LSP/Properties/AssemblyInfo.cs @@ -5,11 +5,11 @@ // General Information about an assembly is controlled through the following // set of attributes. Change these attribute values to modify the information // associated with an assembly. -[assembly: AssemblyTitle("OCP_First_Example")] +[assembly: AssemblyTitle("LSP")] [assembly: AssemblyDescription("")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("OCP_First_Example")] +[assembly: AssemblyProduct("LSP")] [assembly: AssemblyCopyright("Copyright © 2018")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] @@ -20,7 +20,7 @@ [assembly: ComVisible(false)] // The following GUID is for the ID of the typelib if this project is exposed to COM -[assembly: Guid("34ba7048-340c-442f-8832-2dbb4e4435b8")] +[assembly: Guid("7d7641e7-44d8-42ed-9443-99a655fa7195")] // Version information for an assembly consists of the following four values: // diff --git a/LSP/LSP/SumCalculator.cs b/LSP/LSP/SumCalculator.cs new file mode 100644 index 0000000..d3c04a8 --- /dev/null +++ b/LSP/LSP/SumCalculator.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LSP +{ + public class SumCalculator + { + protected readonly int[] _numbers; + + public SumCalculator(int[] numbers) + { + _numbers = numbers; + } + + public virtual int Calculate() => _numbers.Sum(); + } +} diff --git a/OCP_First_Example/OCP_First_Example/BaseSalaryCalculator.cs b/OCP_First_Example/OCP_First_Example/BaseSalaryCalculator.cs deleted file mode 100644 index 102db6f..0000000 --- a/OCP_First_Example/OCP_First_Example/BaseSalaryCalculator.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace OCP_First_Example -{ - public abstract class BaseSalaryCalculator - { - protected DeveloperReport DeveloperReport { get; private set; } - - public BaseSalaryCalculator(DeveloperReport developerReport) - { - DeveloperReport = developerReport; - } - - public abstract double CalculateSalary(); - } -} diff --git a/OCP_First_Example/OCP_First_Example/DeveloperReport.cs b/OCP_First_Example/OCP_First_Example/DeveloperReport.cs deleted file mode 100644 index 97d5100..0000000 --- a/OCP_First_Example/OCP_First_Example/DeveloperReport.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace OCP_First_Example -{ - public class DeveloperReport - { - public int Id { get; set; } - public string Name { get; set; } - public string Level { get; set; } - public int WorkingHours { get; set; } - public double HourlyRate { get; set; } - } -} diff --git a/OCP_First_Example/OCP_First_Example/JuniorDevSalaryCalculator.cs b/OCP_First_Example/OCP_First_Example/JuniorDevSalaryCalculator.cs deleted file mode 100644 index 5c2f55a..0000000 --- a/OCP_First_Example/OCP_First_Example/JuniorDevSalaryCalculator.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace OCP_First_Example -{ - public class JuniorDevSalaryCalculator : BaseSalaryCalculator - { - public JuniorDevSalaryCalculator(DeveloperReport developerReport) - :base(developerReport) - { - } - - public override double CalculateSalary() => DeveloperReport.HourlyRate * DeveloperReport.WorkingHours; - } -} diff --git a/OCP_First_Example/OCP_First_Example/Program.cs b/OCP_First_Example/OCP_First_Example/Program.cs deleted file mode 100644 index b0e6253..0000000 --- a/OCP_First_Example/OCP_First_Example/Program.cs +++ /dev/null @@ -1,24 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace OCP_First_Example -{ - class Program - { - static void Main(string[] args) - { - var devCalculations = new List - { - new SeniorDevSalaryCalculator(new DeveloperReport {Id = 1, Name = "Dev1", Level = "Senior developer", HourlyRate = 30.5, WorkingHours = 160 }), - new JuniorDevSalaryCalculator(new DeveloperReport {Id = 2, Name = "Dev2", Level = "Junior developer", HourlyRate = 20, WorkingHours = 150 }), - new SeniorDevSalaryCalculator(new DeveloperReport {Id = 3, Name = "Dev3", Level = "Senior developer", HourlyRate = 30.5, WorkingHours = 180 }) - }; - - var calculator = new SalaryCalculator(devCalculations); - Console.WriteLine($"Sum of all the developer salaries is {calculator.CalculateTotalSalaries()} dollars"); - } - } -} diff --git a/OCP_First_Example/OCP_First_Example/SalaryCalculator.cs b/OCP_First_Example/OCP_First_Example/SalaryCalculator.cs deleted file mode 100644 index 2998ddd..0000000 --- a/OCP_First_Example/OCP_First_Example/SalaryCalculator.cs +++ /dev/null @@ -1,30 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace OCP_First_Example -{ - public class SalaryCalculator - { - private readonly IEnumerable _developerCalculation; - - public SalaryCalculator(IEnumerable developerCalculation) - { - _developerCalculation = developerCalculation; - } - - public double CalculateTotalSalaries() - { - double totalSalaries = 0D; - - foreach (var devCalc in _developerCalculation) - { - totalSalaries += devCalc.CalculateSalary(); - } - - return totalSalaries; - } - } -} diff --git a/OCP_First_Example/OCP_First_Example/SeniorDevSalaryCalculator.cs b/OCP_First_Example/OCP_First_Example/SeniorDevSalaryCalculator.cs deleted file mode 100644 index 32ea7c9..0000000 --- a/OCP_First_Example/OCP_First_Example/SeniorDevSalaryCalculator.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace OCP_First_Example -{ - public class SeniorDevSalaryCalculator : BaseSalaryCalculator - { - public SeniorDevSalaryCalculator(DeveloperReport report) - :base(report) - { - } - - public override double CalculateSalary() => DeveloperReport.HourlyRate * DeveloperReport.WorkingHours * 1.2; - } -} diff --git a/OCP_Second_Example/OCP_Second_Example.sln b/OCP_Second_Example/OCP_Second_Example.sln deleted file mode 100644 index 352c7e2..0000000 --- a/OCP_Second_Example/OCP_Second_Example.sln +++ /dev/null @@ -1,25 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 15 -VisualStudioVersion = 15.0.28307.136 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OCP_Second_Example", "OCP_Second_Example\OCP_Second_Example.csproj", "{B17FABB3-452F-45A7-A2EC-81398721C6BA}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {B17FABB3-452F-45A7-A2EC-81398721C6BA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {B17FABB3-452F-45A7-A2EC-81398721C6BA}.Debug|Any CPU.Build.0 = Debug|Any CPU - {B17FABB3-452F-45A7-A2EC-81398721C6BA}.Release|Any CPU.ActiveCfg = Release|Any CPU - {B17FABB3-452F-45A7-A2EC-81398721C6BA}.Release|Any CPU.Build.0 = Release|Any CPU - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {FB4B96F7-7F18-4A27-80A3-7A7BF26F1BE0} - EndGlobalSection -EndGlobal diff --git a/OCP_Second_Example/OCP_Second_Example/App.config b/OCP_Second_Example/OCP_Second_Example/App.config deleted file mode 100644 index 8e15646..0000000 --- a/OCP_Second_Example/OCP_Second_Example/App.config +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/OCP_Second_Example/OCP_Second_Example/ComputerMonitor.cs b/OCP_Second_Example/OCP_Second_Example/ComputerMonitor.cs deleted file mode 100644 index ead923c..0000000 --- a/OCP_Second_Example/OCP_Second_Example/ComputerMonitor.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace OCP_Second_Example -{ - public class ComputerMonitor - { - public string Name { get; set; } - public MonitorType Type { get; set; } - public Screen Screen { get; set; } - } -} diff --git a/OCP_Second_Example/OCP_Second_Example/IFilter.cs b/OCP_Second_Example/OCP_Second_Example/IFilter.cs deleted file mode 100644 index 71d6805..0000000 --- a/OCP_Second_Example/OCP_Second_Example/IFilter.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace OCP_Second_Example -{ - public interface IFilter - { - List Filter(IEnumerable monitors, ISpecification specification); - } -} diff --git a/OCP_Second_Example/OCP_Second_Example/ISpecification.cs b/OCP_Second_Example/OCP_Second_Example/ISpecification.cs deleted file mode 100644 index 50aa160..0000000 --- a/OCP_Second_Example/OCP_Second_Example/ISpecification.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace OCP_Second_Example -{ - public interface ISpecification - { - bool isSatisfied(T item); - } -} diff --git a/OCP_Second_Example/OCP_Second_Example/MonitorFilter.cs b/OCP_Second_Example/OCP_Second_Example/MonitorFilter.cs deleted file mode 100644 index 960c9ef..0000000 --- a/OCP_Second_Example/OCP_Second_Example/MonitorFilter.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace OCP_Second_Example -{ - public class MonitorFilter : IFilter - { - public List Filter(IEnumerable monitors, ISpecification specification) => - monitors.Where(m => specification.isSatisfied(m)).ToList(); - } -} diff --git a/OCP_Second_Example/OCP_Second_Example/MonitorType.cs b/OCP_Second_Example/OCP_Second_Example/MonitorType.cs deleted file mode 100644 index 45e2855..0000000 --- a/OCP_Second_Example/OCP_Second_Example/MonitorType.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace OCP_Second_Example -{ - public enum MonitorType - { - OLED, - LCD, - LED - } -} diff --git a/OCP_Second_Example/OCP_Second_Example/MonitorTypeSpecification.cs b/OCP_Second_Example/OCP_Second_Example/MonitorTypeSpecification.cs deleted file mode 100644 index c7576c5..0000000 --- a/OCP_Second_Example/OCP_Second_Example/MonitorTypeSpecification.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace OCP_Second_Example -{ - public class MonitorTypeSpecification: ISpecification - { - private readonly MonitorType _type; - - public MonitorTypeSpecification(MonitorType type) - { - _type = type; - } - - public bool isSatisfied(ComputerMonitor item) => item.Type == _type; - } -} diff --git a/OCP_Second_Example/OCP_Second_Example/OCP_Second_Example.csproj b/OCP_Second_Example/OCP_Second_Example/OCP_Second_Example.csproj deleted file mode 100644 index 60d11b0..0000000 --- a/OCP_Second_Example/OCP_Second_Example/OCP_Second_Example.csproj +++ /dev/null @@ -1,60 +0,0 @@ - - - - - Debug - AnyCPU - {B17FABB3-452F-45A7-A2EC-81398721C6BA} - Exe - OCP_Second_Example - OCP_Second_Example - v4.5 - 512 - true - - - AnyCPU - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - AnyCPU - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/OCP_Second_Example/OCP_Second_Example/Program.cs b/OCP_Second_Example/OCP_Second_Example/Program.cs deleted file mode 100644 index 3847ebb..0000000 --- a/OCP_Second_Example/OCP_Second_Example/Program.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace OCP_Second_Example -{ - class Program - { - static void Main(string[] args) - { - var monitors = new List - { - new ComputerMonitor { Name = "Samsung S345", Screen = Screen.CurvedScreen, Type = MonitorType.OLED }, - new ComputerMonitor { Name = "Philips P532", Screen = Screen.WideScreen, Type = MonitorType.LCD }, - new ComputerMonitor { Name = "LG L888", Screen = Screen.WideScreen, Type = MonitorType.LED }, - new ComputerMonitor { Name = "Samsung S999", Screen = Screen.WideScreen, Type = MonitorType.OLED }, - new ComputerMonitor { Name = "Dell D2J47", Screen = Screen.CurvedScreen, Type = MonitorType.LCD } - }; - - var filter = new MonitorFilter(); - - var lcdMonitors = filter.Filter(monitors, new MonitorTypeSpecification(MonitorType.LCD)); - Console.WriteLine("All LCD monitors"); - foreach (var monitor in lcdMonitors) - { - Console.WriteLine($"Name: {monitor.Name}, Type: {monitor.Type}, Screen: {monitor.Screen}"); - } - - Console.WriteLine("All WideScreen Monitors"); - var wideScreenMonitors = filter.Filter(monitors, new ScreenSpecification(Screen.WideScreen)); - foreach (var monitor in wideScreenMonitors) - { - Console.WriteLine($"Name: {monitor.Name}, Type: {monitor.Type}, Screen: {monitor.Screen}"); - } - } - } -} diff --git a/OCP_Second_Example/OCP_Second_Example/Properties/AssemblyInfo.cs b/OCP_Second_Example/OCP_Second_Example/Properties/AssemblyInfo.cs deleted file mode 100644 index a49d7c6..0000000 --- a/OCP_Second_Example/OCP_Second_Example/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -// General Information about an assembly is controlled through the following -// set of attributes. Change these attribute values to modify the information -// associated with an assembly. -[assembly: AssemblyTitle("OCP_Second_Example")] -[assembly: AssemblyDescription("")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("OCP_Second_Example")] -[assembly: AssemblyCopyright("Copyright © 2018")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] - -// Setting ComVisible to false makes the types in this assembly not visible -// to COM components. If you need to access a type in this assembly from -// COM, set the ComVisible attribute to true on that type. -[assembly: ComVisible(false)] - -// The following GUID is for the ID of the typelib if this project is exposed to COM -[assembly: Guid("b17fabb3-452f-45a7-a2ec-81398721c6ba")] - -// Version information for an assembly consists of the following four values: -// -// Major Version -// Minor Version -// Build Number -// Revision -// -// You can specify all the values or you can default the Build and Revision Numbers -// by using the '*' as shown below: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/OCP_Second_Example/OCP_Second_Example/Screen.cs b/OCP_Second_Example/OCP_Second_Example/Screen.cs deleted file mode 100644 index a14726f..0000000 --- a/OCP_Second_Example/OCP_Second_Example/Screen.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace OCP_Second_Example -{ - public enum Screen - { - WideScreen, - CurvedScreen - } -} diff --git a/OCP_Second_Example/OCP_Second_Example/ScreenSpecification.cs b/OCP_Second_Example/OCP_Second_Example/ScreenSpecification.cs deleted file mode 100644 index 66159e2..0000000 --- a/OCP_Second_Example/OCP_Second_Example/ScreenSpecification.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace OCP_Second_Example -{ - public class ScreenSpecification : ISpecification - { - private readonly Screen _screen; - - public ScreenSpecification(Screen screen) - { - _screen = screen; - } - - public bool isSatisfied(ComputerMonitor item) => item.Screen == _screen; - } -} diff --git a/README.md b/README.md index e4a4c3d..1436c40 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ -# Open Closed Principle -## https://code-maze.com/open-closed-principle/ +# Liskov Substitution Principle +## https://code-maze.com/liskov-substitution-principle/ From c2d27db43edb8780012ddbbe78cd52e3bb2dc8b9 Mon Sep 17 00:00:00 2001 From: Marinko Date: Tue, 22 Jan 2019 22:25:49 +0100 Subject: [PATCH 5/7] Modified example --- LSP/LSP/EvenNumbersSumCalculator.cs | 2 +- LSP/LSP/LSP.csproj | 1 + LSP/LSP/Program.cs | 4 ++-- LSP/LSP/SumCalculator.cs | 8 +++----- 4 files changed, 7 insertions(+), 8 deletions(-) diff --git a/LSP/LSP/EvenNumbersSumCalculator.cs b/LSP/LSP/EvenNumbersSumCalculator.cs index 5bf1d40..bc22289 100644 --- a/LSP/LSP/EvenNumbersSumCalculator.cs +++ b/LSP/LSP/EvenNumbersSumCalculator.cs @@ -6,7 +6,7 @@ namespace LSP { - public class EvenNumbersSumCalculator: SumCalculator + public class EvenNumbersSumCalculator: Calculator { public EvenNumbersSumCalculator(int[] numbers) :base(numbers) diff --git a/LSP/LSP/LSP.csproj b/LSP/LSP/LSP.csproj index 52b8350..05c753b 100644 --- a/LSP/LSP/LSP.csproj +++ b/LSP/LSP/LSP.csproj @@ -42,6 +42,7 @@ + diff --git a/LSP/LSP/Program.cs b/LSP/LSP/Program.cs index 63ff5d0..1c34779 100644 --- a/LSP/LSP/Program.cs +++ b/LSP/LSP/Program.cs @@ -12,12 +12,12 @@ static void Main(string[] args) { var numbers = new int[] { 5, 7, 9, 8, 1, 6, 4 }; - SumCalculator sum = new SumCalculator(numbers); + Calculator sum = new SumCalculator(numbers); Console.WriteLine($"The sum of all the numbers: {sum.Calculate()}"); Console.WriteLine(); - SumCalculator evenSum = new EvenNumbersSumCalculator(numbers); + Calculator evenSum = new EvenNumbersSumCalculator(numbers); Console.WriteLine($"The sum of all the even numbers: {evenSum.Calculate()}"); } } diff --git a/LSP/LSP/SumCalculator.cs b/LSP/LSP/SumCalculator.cs index d3c04a8..29900d0 100644 --- a/LSP/LSP/SumCalculator.cs +++ b/LSP/LSP/SumCalculator.cs @@ -6,15 +6,13 @@ namespace LSP { - public class SumCalculator + public class SumCalculator : Calculator { - protected readonly int[] _numbers; - public SumCalculator(int[] numbers) + :base(numbers) { - _numbers = numbers; } - public virtual int Calculate() => _numbers.Sum(); + public override int Calculate() => _numbers.Sum(); } } From bfec192dcc9fbba2efd597836f65650e72edf3e4 Mon Sep 17 00:00:00 2001 From: Marinko Date: Sat, 26 Jan 2019 19:18:34 +0100 Subject: [PATCH 6/7] DIP initial commit --- .gitignore | 3 +++ LSP/LSP.sln => DIP/DIP.sln | 14 +++++----- {LSP/LSP => DIP/DIP}/App.config | 0 LSP/LSP/LSP.csproj => DIP/DIP/DIP.csproj | 15 ++++++----- DIP/DIP/Employee.cs | 15 +++++++++++ DIP/DIP/EmployeeManager.cs | 26 +++++++++++++++++++ DIP/DIP/EmployeeStatistics.cs | 21 +++++++++++++++ DIP/DIP/Gender.cs | 14 ++++++++++ DIP/DIP/IEmployeeSerchable.cs | 13 ++++++++++ DIP/DIP/Position.cs | 15 +++++++++++ DIP/DIP/Program.cs | 21 +++++++++++++++ .../DIP}/Properties/AssemblyInfo.cs | 6 ++--- LSP/LSP/EvenNumbersSumCalculator.cs | 18 ------------- LSP/LSP/Program.cs | 24 ----------------- LSP/LSP/SumCalculator.cs | 18 ------------- README.md | 2 +- 16 files changed, 148 insertions(+), 77 deletions(-) rename LSP/LSP.sln => DIP/DIP.sln (56%) rename {LSP/LSP => DIP/DIP}/App.config (100%) rename LSP/LSP/LSP.csproj => DIP/DIP/DIP.csproj (84%) create mode 100644 DIP/DIP/Employee.cs create mode 100644 DIP/DIP/EmployeeManager.cs create mode 100644 DIP/DIP/EmployeeStatistics.cs create mode 100644 DIP/DIP/Gender.cs create mode 100644 DIP/DIP/IEmployeeSerchable.cs create mode 100644 DIP/DIP/Position.cs create mode 100644 DIP/DIP/Program.cs rename {LSP/LSP => DIP/DIP}/Properties/AssemblyInfo.cs (90%) delete mode 100644 LSP/LSP/EvenNumbersSumCalculator.cs delete mode 100644 LSP/LSP/Program.cs delete mode 100644 LSP/LSP/SumCalculator.cs diff --git a/.gitignore b/.gitignore index 798d2a5..07c74bf 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,6 @@ /OCP_Second_Example/.vs /OCP_Second_Example/OCP_Second_Example/bin /OCP_Second_Example/OCP_Second_Example/obj +/DIP/.vs +/DIP/DIP/bin +/DIP/DIP/obj diff --git a/LSP/LSP.sln b/DIP/DIP.sln similarity index 56% rename from LSP/LSP.sln rename to DIP/DIP.sln index a2a44da..a651a36 100644 --- a/LSP/LSP.sln +++ b/DIP/DIP.sln @@ -1,9 +1,9 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 -VisualStudioVersion = 15.0.28307.136 +VisualStudioVersion = 15.0.28307.168 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LSP", "LSP\LSP.csproj", "{7D7641E7-44D8-42ED-9443-99A655FA7195}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DIP", "DIP\DIP.csproj", "{6CC8448A-93CD-42DF-AF04-72C08A87B686}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -11,15 +11,15 @@ Global Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {7D7641E7-44D8-42ED-9443-99A655FA7195}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {7D7641E7-44D8-42ED-9443-99A655FA7195}.Debug|Any CPU.Build.0 = Debug|Any CPU - {7D7641E7-44D8-42ED-9443-99A655FA7195}.Release|Any CPU.ActiveCfg = Release|Any CPU - {7D7641E7-44D8-42ED-9443-99A655FA7195}.Release|Any CPU.Build.0 = Release|Any CPU + {6CC8448A-93CD-42DF-AF04-72C08A87B686}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6CC8448A-93CD-42DF-AF04-72C08A87B686}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6CC8448A-93CD-42DF-AF04-72C08A87B686}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6CC8448A-93CD-42DF-AF04-72C08A87B686}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {10F1FC52-49A2-4C74-83C9-9389C91C8C3F} + SolutionGuid = {210AF8BA-7357-4817-A716-A29CBA522746} EndGlobalSection EndGlobal diff --git a/LSP/LSP/App.config b/DIP/DIP/App.config similarity index 100% rename from LSP/LSP/App.config rename to DIP/DIP/App.config diff --git a/LSP/LSP/LSP.csproj b/DIP/DIP/DIP.csproj similarity index 84% rename from LSP/LSP/LSP.csproj rename to DIP/DIP/DIP.csproj index 05c753b..5722e33 100644 --- a/LSP/LSP/LSP.csproj +++ b/DIP/DIP/DIP.csproj @@ -4,10 +4,10 @@ Debug AnyCPU - {7D7641E7-44D8-42ED-9443-99A655FA7195} + {6CC8448A-93CD-42DF-AF04-72C08A87B686} Exe - LSP - LSP + DIP + DIP v4.5 512 true @@ -42,11 +42,14 @@ - - + + + + + + - diff --git a/DIP/DIP/Employee.cs b/DIP/DIP/Employee.cs new file mode 100644 index 0000000..c058902 --- /dev/null +++ b/DIP/DIP/Employee.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DIP +{ + public class Employee + { + public string Name { get; set; } + public Gender Gender { get; set; } + public Position Position { get; set; } + } +} diff --git a/DIP/DIP/EmployeeManager.cs b/DIP/DIP/EmployeeManager.cs new file mode 100644 index 0000000..5f2ceee --- /dev/null +++ b/DIP/DIP/EmployeeManager.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DIP +{ + public class EmployeeManager: IEmployeeSerchable + { + private readonly List _employees; + + public EmployeeManager() + { + _employees = new List(); + } + + public void AddEmployee(Employee employee) + { + _employees.Add(employee); + } + + public IEnumerable GetEmployeesByGenderAndPosition(Gender gender, Position position) + => _employees.Where(emp => emp.Gender == gender && emp.Position == position); + } +} diff --git a/DIP/DIP/EmployeeStatistics.cs b/DIP/DIP/EmployeeStatistics.cs new file mode 100644 index 0000000..590c3f2 --- /dev/null +++ b/DIP/DIP/EmployeeStatistics.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DIP +{ + public class EmployeeStatistics + { + private readonly IEmployeeSerchable _emp; + + public EmployeeStatistics(IEmployeeSerchable emp) + { + _emp = emp; + } + + public int CountFemaleManagers() => + _emp.GetEmployeesByGenderAndPosition(Gender.Female, Position.Manager).Count(); + } +} diff --git a/DIP/DIP/Gender.cs b/DIP/DIP/Gender.cs new file mode 100644 index 0000000..414904d --- /dev/null +++ b/DIP/DIP/Gender.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DIP +{ + public enum Gender + { + Male, + Female + } +} diff --git a/DIP/DIP/IEmployeeSerchable.cs b/DIP/DIP/IEmployeeSerchable.cs new file mode 100644 index 0000000..79d0d34 --- /dev/null +++ b/DIP/DIP/IEmployeeSerchable.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DIP +{ + public interface IEmployeeSerchable + { + IEnumerable GetEmployeesByGenderAndPosition(Gender gender, Position position); + } +} diff --git a/DIP/DIP/Position.cs b/DIP/DIP/Position.cs new file mode 100644 index 0000000..d27a21a --- /dev/null +++ b/DIP/DIP/Position.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DIP +{ + public enum Position + { + Administrator, + Manager, + Executive + } +} diff --git a/DIP/DIP/Program.cs b/DIP/DIP/Program.cs new file mode 100644 index 0000000..449ef5f --- /dev/null +++ b/DIP/DIP/Program.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DIP +{ + class Program + { + static void Main(string[] args) + { + var empManager = new EmployeeManager(); + empManager.AddEmployee(new Employee { Name = "Leen", Gender = Gender.Female, Position = Position.Manager }); + empManager.AddEmployee(new Employee { Name = "Mike", Gender = Gender.Male, Position = Position.Administrator }); + + var stats = new EmployeeStatistics(empManager); + Console.WriteLine($"Number of female managers in our company is: {stats.CountFemaleManagers()}"); + } + } +} diff --git a/LSP/LSP/Properties/AssemblyInfo.cs b/DIP/DIP/Properties/AssemblyInfo.cs similarity index 90% rename from LSP/LSP/Properties/AssemblyInfo.cs rename to DIP/DIP/Properties/AssemblyInfo.cs index fd7deac..9f353dd 100644 --- a/LSP/LSP/Properties/AssemblyInfo.cs +++ b/DIP/DIP/Properties/AssemblyInfo.cs @@ -5,11 +5,11 @@ // General Information about an assembly is controlled through the following // set of attributes. Change these attribute values to modify the information // associated with an assembly. -[assembly: AssemblyTitle("LSP")] +[assembly: AssemblyTitle("DIP")] [assembly: AssemblyDescription("")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("LSP")] +[assembly: AssemblyProduct("DIP")] [assembly: AssemblyCopyright("Copyright © 2018")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] @@ -20,7 +20,7 @@ [assembly: ComVisible(false)] // The following GUID is for the ID of the typelib if this project is exposed to COM -[assembly: Guid("7d7641e7-44d8-42ed-9443-99a655fa7195")] +[assembly: Guid("6cc8448a-93cd-42df-af04-72c08a87b686")] // Version information for an assembly consists of the following four values: // diff --git a/LSP/LSP/EvenNumbersSumCalculator.cs b/LSP/LSP/EvenNumbersSumCalculator.cs deleted file mode 100644 index bc22289..0000000 --- a/LSP/LSP/EvenNumbersSumCalculator.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace LSP -{ - public class EvenNumbersSumCalculator: Calculator - { - public EvenNumbersSumCalculator(int[] numbers) - :base(numbers) - { - } - - public override int Calculate() => _numbers.Where(x => x % 2 == 0).Sum(); - } -} diff --git a/LSP/LSP/Program.cs b/LSP/LSP/Program.cs deleted file mode 100644 index 1c34779..0000000 --- a/LSP/LSP/Program.cs +++ /dev/null @@ -1,24 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace LSP -{ - class Program - { - static void Main(string[] args) - { - var numbers = new int[] { 5, 7, 9, 8, 1, 6, 4 }; - - Calculator sum = new SumCalculator(numbers); - Console.WriteLine($"The sum of all the numbers: {sum.Calculate()}"); - - Console.WriteLine(); - - Calculator evenSum = new EvenNumbersSumCalculator(numbers); - Console.WriteLine($"The sum of all the even numbers: {evenSum.Calculate()}"); - } - } -} diff --git a/LSP/LSP/SumCalculator.cs b/LSP/LSP/SumCalculator.cs deleted file mode 100644 index 29900d0..0000000 --- a/LSP/LSP/SumCalculator.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace LSP -{ - public class SumCalculator : Calculator - { - public SumCalculator(int[] numbers) - :base(numbers) - { - } - - public override int Calculate() => _numbers.Sum(); - } -} diff --git a/README.md b/README.md index 1436c40..123009f 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ # Liskov Substitution Principle -## https://code-maze.com/liskov-substitution-principle/ +## https://code-maze.com/dependency-inversion-principle/ From a4226b60e3dab64d29eb641c6e55fd57460cc2a5 Mon Sep 17 00:00:00 2001 From: Marinko Date: Sun, 21 Feb 2021 14:07:38 +0100 Subject: [PATCH 7/7] .NET 5 Transfer --- DIP/DIP/App.config | 6 ---- DIP/DIP/DIP.csproj | 58 +++--------------------------- DIP/DIP/Properties/AssemblyInfo.cs | 36 ------------------- 3 files changed, 4 insertions(+), 96 deletions(-) delete mode 100644 DIP/DIP/App.config delete mode 100644 DIP/DIP/Properties/AssemblyInfo.cs diff --git a/DIP/DIP/App.config b/DIP/DIP/App.config deleted file mode 100644 index 8e15646..0000000 --- a/DIP/DIP/App.config +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/DIP/DIP/DIP.csproj b/DIP/DIP/DIP.csproj index 5722e33..51be1a2 100644 --- a/DIP/DIP/DIP.csproj +++ b/DIP/DIP/DIP.csproj @@ -1,58 +1,8 @@ - - - + + - Debug - AnyCPU - {6CC8448A-93CD-42DF-AF04-72C08A87B686} Exe - DIP - DIP - v4.5 - 512 - true + net5.0 - - AnyCPU - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - AnyCPU - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - - - - - - - - - - - - - - - - - - - - - - - + \ No newline at end of file diff --git a/DIP/DIP/Properties/AssemblyInfo.cs b/DIP/DIP/Properties/AssemblyInfo.cs deleted file mode 100644 index 9f353dd..0000000 --- a/DIP/DIP/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -// General Information about an assembly is controlled through the following -// set of attributes. Change these attribute values to modify the information -// associated with an assembly. -[assembly: AssemblyTitle("DIP")] -[assembly: AssemblyDescription("")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("DIP")] -[assembly: AssemblyCopyright("Copyright © 2018")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] - -// Setting ComVisible to false makes the types in this assembly not visible -// to COM components. If you need to access a type in this assembly from -// COM, set the ComVisible attribute to true on that type. -[assembly: ComVisible(false)] - -// The following GUID is for the ID of the typelib if this project is exposed to COM -[assembly: Guid("6cc8448a-93cd-42df-af04-72c08a87b686")] - -// Version information for an assembly consists of the following four values: -// -// Major Version -// Minor Version -// Build Number -// Revision -// -// You can specify all the values or you can default the Build and Revision Numbers -// by using the '*' as shown below: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")]