diff --git a/OCP/IDevice.cs b/OCP/IDevice.cs new file mode 100644 index 0000000..45944b6 --- /dev/null +++ b/OCP/IDevice.cs @@ -0,0 +1,7 @@ +namespace ElectronicDevices +{ + public interface IDevice + { + void DisplayDetails(); + } +} diff --git a/OCP/IDeviceFactory.cs b/OCP/IDeviceFactory.cs new file mode 100644 index 0000000..866afd7 --- /dev/null +++ b/OCP/IDeviceFactory.cs @@ -0,0 +1,7 @@ +namespace ElectronicDevices +{ + public interface IDeviceFactory + { + IDevice CreateDevice(); + } +} diff --git a/OCP/Laptop.cs b/OCP/Laptop.cs new file mode 100644 index 0000000..9068e01 --- /dev/null +++ b/OCP/Laptop.cs @@ -0,0 +1,10 @@ +namespace ElectronicDevices +{ + public class Laptop : IDevice + { + public void DisplayDetails() + { + Console.WriteLine("Laptop: Model Y, RAM: 16GB, Storage: 512GB"); + } + } +} diff --git a/OCP/LaptopFactory.cs b/OCP/LaptopFactory.cs new file mode 100644 index 0000000..24bcee6 --- /dev/null +++ b/OCP/LaptopFactory.cs @@ -0,0 +1,10 @@ +namespace ElectronicDevices +{ + public class LaptopFactory : IDeviceFactory + { + public IDevice CreateDevice() + { + return new Laptop(); + } + } +} diff --git a/OCP/Program.cs b/OCP/Program.cs new file mode 100644 index 0000000..e506171 --- /dev/null +++ b/OCP/Program.cs @@ -0,0 +1,17 @@ +namespace ElectronicDevices +{ + internal class Program + { + static void Main(string[] args) + { + IDeviceFactory smartphoneFactory = new SmartphoneFactory(); + IDeviceFactory laptopFactory = new LaptopFactory(); + + IDevice smartphone = smartphoneFactory.CreateDevice(); + IDevice laptop = laptopFactory.CreateDevice(); + + smartphone.DisplayDetails(); + laptop.DisplayDetails(); + } + } +} diff --git a/OCP/Smartphone.cs b/OCP/Smartphone.cs new file mode 100644 index 0000000..8fe83e1 --- /dev/null +++ b/OCP/Smartphone.cs @@ -0,0 +1,10 @@ +namespace ElectronicDevices +{ + public class Smartphone : IDevice + { + public void DisplayDetails() + { + Console.WriteLine("Smartphone: Model X, RAM: 8GB, Storage: 128GB"); + } + } +} diff --git a/OCP/SmartphoneFactory.cs b/OCP/SmartphoneFactory.cs new file mode 100644 index 0000000..e875907 --- /dev/null +++ b/OCP/SmartphoneFactory.cs @@ -0,0 +1,10 @@ +namespace ElectronicDevices +{ + public class SmartphoneFactory : IDeviceFactory + { + public IDevice CreateDevice() + { + return new Smartphone(); + } + } +} diff --git a/SRP/Example1/Printer.cs b/SRP/Example1/Printer.cs new file mode 100644 index 0000000..ee70e30 --- /dev/null +++ b/SRP/Example1/Printer.cs @@ -0,0 +1,10 @@ +namespace RectangleAreaCalculator +{ + public class Printer + { + public void PrintArea(double area) + { + Console.WriteLine($"Area: {area}"); + } + } +} diff --git a/SRP/Example1/Program.cs b/SRP/Example1/Program.cs new file mode 100644 index 0000000..5e9696a --- /dev/null +++ b/SRP/Example1/Program.cs @@ -0,0 +1,15 @@ +namespace RectangleAreaCalculator +{ + public class Program + { + static void Main() + { + Rectangle rectangle = new Rectangle(5.0, 3.0); + + double area = rectangle.CalculateArea(); + + Printer printer = new Printer(); + printer.PrintArea(area); + } + } +} diff --git a/SRP/Example1/Rectangle.cs b/SRP/Example1/Rectangle.cs new file mode 100644 index 0000000..52e0cd9 --- /dev/null +++ b/SRP/Example1/Rectangle.cs @@ -0,0 +1,19 @@ +namespace RectangleAreaCalculator +{ + public class Rectangle + { + public double Length { get; } + public double Width { get; } + + public Rectangle(double length, double width) + { + Length = length; + Width = width; + } + + public double CalculateArea() + { + return Length * Width; + } + } +} diff --git a/SRP/Example2/FileWriter.cs b/SRP/Example2/FileWriter.cs new file mode 100644 index 0000000..a48150f --- /dev/null +++ b/SRP/Example2/FileWriter.cs @@ -0,0 +1,17 @@ +namespace RectangleAreaApplication +{ + public class FileWriter + { + public static void WriteToFile(string fileName, string content) + { + try + { + File.WriteAllText(fileName, content); + } + catch (IOException exception) + { + Console.WriteLine("Error writing to file: " + exception.Message); + } + } + } +} diff --git a/SRP/Example2/Program.cs b/SRP/Example2/Program.cs new file mode 100644 index 0000000..353b76c --- /dev/null +++ b/SRP/Example2/Program.cs @@ -0,0 +1,15 @@ +namespace RectangleAreaApplication +{ + public class Program + { + static void Main(string[] args) + { + Rectangle rectangle = new Rectangle(5.0, 3.0); + double area = rectangle.CalculateArea(); + + FileWriter.WriteToFile("area.txt", $"Area: {area}"); + + Console.WriteLine($"Calculated Area: {area}"); + } + } +} diff --git a/SRP/Example2/Rectangle.cs b/SRP/Example2/Rectangle.cs new file mode 100644 index 0000000..62d1d05 --- /dev/null +++ b/SRP/Example2/Rectangle.cs @@ -0,0 +1,19 @@ +namespace RectangleAreaApplication +{ + public class Rectangle + { + private double length; + private double width; + + public Rectangle(double length, double width) + { + this.length = length; + this.width = width; + } + + public double CalculateArea() + { + return length * width; + } + } +}