Skip to content

Instantly share code, notes, and snippets.

View kishankg's full-sized avatar
🏠
Working from home

Kishan Kumar Gupta kishankg

🏠
Working from home
  • Bengaluru, Karnataka
View GitHub Profile
// State interface
interface TrafficLightState {
void display();
}
// Concrete State: RedLightState
class RedLightState implements TrafficLightState {
@Override
public void display() {
System.out.println("Red Light - Stop");
// Abstraction: BankAccount
abstract class BankAccount {
protected TransactionProcessor transactionProcessor;
BankAccount(TransactionProcessor transactionProcessor) {
this.transactionProcessor = transactionProcessor;
}
abstract void processTransaction(double amount);
}
// Subject: Image interface
interface Image {
void display();
}
// RealSubject: RealImage class (Actual heavy object)
class RealImage implements Image {
private String filename;
RealImage(String filename) {
@kishankg
kishankg / TextEditor
Last active December 4, 2023 12:01
Command Design Pattern
import java.util.Stack;
// Receiver: TextEditor
class TextEditor {
private StringBuilder text = new StringBuilder();
void insertText(String insertedText) {
text.append(insertedText);
}
// Abstract Product: Button
interface Button {
void click();
}
// Concrete Products: WindowsButton and MacOSButton
class WindowsButton implements Button {
@Override
public void click() {
System.out.println("Windows button clicked");
// Product Interface: DatabaseConnection
interface DatabaseConnection {
void connect();
void disconnect();
}
// Concrete Products: MySqlConnection and PostgreSqlConnection
class MySqlConnection implements DatabaseConnection {
@Override
public void connect() {
import java.util.ArrayList;
import java.util.List;
// Observer interface
interface Observer {
void update(String weather);
}
// Subject interface
interface Subject {
public class ClientWrapper {
int connectionState, requestingThreads; //-1 null, 1 init done, 2 closed
Lock lock;
Condition awaitRequest, awaitClose;
ClientWrapper2(){
//constructor
connectionState = -1;
requestingThreads = 0;
lock = new ReentrantLock();