Skip to content

Instantly share code, notes, and snippets.

@krohit-bkk
krohit-bkk / IPS2.java
Created January 21, 2024 14:34
Code snippet 2 for Interface Segregation Principle
// class Assignee
abstract class Assignee {}
// Interface Dev for Developer tasks
interface Dev { void code(); }
// Interface QA for Test tasks
interface QA { void test(); }
// Developer is an Assingee
@krohit-bkk
krohit-bkk / ISP1.java
Created January 21, 2024 10:21
Code snippet 1 for Interface Segregation Principle
// Interface Tasks
interface Tasks {
void code();
void test();
}
// Class Assignee
abstract class Assignee implements Tasks {}
// Developer is an Assingee
@krohit-bkk
krohit-bkk / LSP.java
Created January 20, 2024 17:59
Code snippet 2 for Loskov Substitution Principle
// Enforce fossil cars to have method to return mileage
interface Refuelables { float getMileage(); }
// Car has mileage
abstract class Car { abstract float calculateRange(); }
// Fossil car is a car
abstract class FossilCar extends Car { float fuel; abstract float getMileage(); }
// Petrol car is a fossil car
@krohit-bkk
krohit-bkk / OcpOnly.java
Created January 20, 2024 17:16
Code snippet 1 for Loskov Substitution Principle
// Enforce cars to have method to return mileage
interface Refuelables {
float getMileage();
}
// Car has mileage
abstract class Car implements Refuelables {
float fuel;
abstract float calculateRange();
}
@krohit-bkk
krohit-bkk / OpenClosedPrincipleExample.java
Last active January 13, 2024 17:47
Example to demonstrate Open-Closed Principle
import java.util.ArrayList;
import java.util.List;
class Item {
private final String type;
private final double price;
private DiscountStrategy discountStrategy;
public Item(String type, double price, DiscountStrategy discountStrategy) {
this.type = type;