Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions OCP/IDevice.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace ElectronicDevices
{
public interface IDevice
{
void DisplayDetails();
}
}
7 changes: 7 additions & 0 deletions OCP/IDeviceFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace ElectronicDevices
{
public interface IDeviceFactory
{
IDevice CreateDevice();
}
}
10 changes: 10 additions & 0 deletions OCP/Laptop.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace ElectronicDevices
{
public class Laptop : IDevice
{
public void DisplayDetails()
{
Console.WriteLine("Laptop: Model Y, RAM: 16GB, Storage: 512GB");
}
}
}
10 changes: 10 additions & 0 deletions OCP/LaptopFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace ElectronicDevices
{
public class LaptopFactory : IDeviceFactory
{
public IDevice CreateDevice()
{
return new Laptop();
}
}
}
17 changes: 17 additions & 0 deletions OCP/Program.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
10 changes: 10 additions & 0 deletions OCP/Smartphone.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace ElectronicDevices
{
public class Smartphone : IDevice
{
public void DisplayDetails()
{
Console.WriteLine("Smartphone: Model X, RAM: 8GB, Storage: 128GB");
}
}
}
10 changes: 10 additions & 0 deletions OCP/SmartphoneFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace ElectronicDevices
{
public class SmartphoneFactory : IDeviceFactory
{
public IDevice CreateDevice()
{
return new Smartphone();
}
}
}
10 changes: 10 additions & 0 deletions SRP/Example1/Printer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace RectangleAreaCalculator
{
public class Printer
{
public void PrintArea(double area)
{
Console.WriteLine($"Area: {area}");
}
}
}
15 changes: 15 additions & 0 deletions SRP/Example1/Program.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
19 changes: 19 additions & 0 deletions SRP/Example1/Rectangle.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
17 changes: 17 additions & 0 deletions SRP/Example2/FileWriter.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
}
15 changes: 15 additions & 0 deletions SRP/Example2/Program.cs
Original file line number Diff line number Diff line change
@@ -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}");
}
}
}
19 changes: 19 additions & 0 deletions SRP/Example2/Rectangle.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}