Skip to content
Open
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
12 changes: 12 additions & 0 deletions OCP/ClientWithFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
public class ClientWithFactory {
public static void main(String[] args) {
DeviceFactory smartphoneFactory = new SmartphoneFactory();
DeviceFactory laptopFactory = new LaptopFactory();

Device smartphone = smartphoneFactory.createDevice();
Device laptop = laptopFactory.createDevice();

smartphone.displayDetails();
laptop.displayDetails();
}
}
3 changes: 3 additions & 0 deletions OCP/Device.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public interface Device {
void displayDetails();
}
3 changes: 3 additions & 0 deletions OCP/DeviceFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public interface DeviceFactory {
Device createDevice();
}
4 changes: 4 additions & 0 deletions OCP/DeviceType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
public enum DeviceType {
SMARTPHONE,
LAPTOP
}
6 changes: 6 additions & 0 deletions OCP/Laptop.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public class Laptop implements Device {
@Override
public void displayDetails() {
System.out.println("Laptop: Model Y, RAM: 16GB, Storage: 512GB");
}
}
6 changes: 6 additions & 0 deletions OCP/LaptopFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public class LaptopFactory implements DeviceFactory {
@Override
public Device createDevice() {
return new Laptop();
}
}
6 changes: 6 additions & 0 deletions OCP/Smartphone.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public class Smartphone implements Device {
@Override
public void displayDetails() {
System.out.println("Smartphone: Model X, RAM: 8GB, Storage: 128GB");
}
}
6 changes: 6 additions & 0 deletions OCP/SmartphoneFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public class SmartphoneFactory implements DeviceFactory {
@Override
public Device createDevice() {
return new Smartphone();
}
}
5 changes: 5 additions & 0 deletions SRP_Example1/AreaCalculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public class AreaCalculator {
public static double calculateArea(Rectangle rectangle) {
return rectangle.getLength() * rectangle.getWidth();
}
}
6 changes: 6 additions & 0 deletions SRP_Example1/AreaPrinter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public class AreaPrinter {
public static void printArea(Rectangle rectangle) {
double area = AreaCalculator.calculateArea(rectangle);
System.out.println("Area: " + area);
}
}
6 changes: 6 additions & 0 deletions SRP_Example1/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public class Main {
public static void main(String[] args) {
Rectangle rectangle = new Rectangle(5.0, 3.0);
AreaPrinter.printArea(rectangle);
}
}
17 changes: 17 additions & 0 deletions SRP_Example1/Rectangle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
public class Rectangle {
private double length;
private double width;

public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}

public double getLength() {
return length;
}

public double getWidth() {
return width;
}
}
5 changes: 5 additions & 0 deletions SRP_Example2/AreaCalculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public class AreaCalculator {
public static double calculateArea(Rectangle rectangle) {
return rectangle.getLength() * rectangle.getWidth();
}
}
12 changes: 12 additions & 0 deletions SRP_Example2/FileWriterUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import java.io.FileWriter;
import java.io.IOException;

public class FileWriterUtil {
public static void writeToFile(String fileName, String content) {
try (FileWriter writer = new FileWriter(fileName)) {
writer.write(content);
} catch (IOException e) {
System.err.println("Error writing to file: " + e.getMessage());
}
}
}
12 changes: 12 additions & 0 deletions SRP_Example2/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
public class Main {
public static void main(String[] args) {
Rectangle rectangle = new Rectangle(5.0, 3.0);

// Calculate area
double area = AreaCalculator.calculateArea(rectangle);
System.out.println("Calculated Area: " + area);

// Save area to a file
FileWriterUtil.writeToFile("area.txt", "Area: " + area);
}
}
17 changes: 17 additions & 0 deletions SRP_Example2/Rectangle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
public class Rectangle {
private double length;
private double width;

public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}

public double getLength() {
return length;
}

public double getWidth() {
return width;
}
}