This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// State interface | |
interface TrafficLightState { | |
void display(); | |
} | |
// Concrete State: RedLightState | |
class RedLightState implements TrafficLightState { | |
@Override | |
public void display() { | |
System.out.println("Red Light - Stop"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Abstraction: BankAccount | |
abstract class BankAccount { | |
protected TransactionProcessor transactionProcessor; | |
BankAccount(TransactionProcessor transactionProcessor) { | |
this.transactionProcessor = transactionProcessor; | |
} | |
abstract void processTransaction(double amount); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Subject: Image interface | |
interface Image { | |
void display(); | |
} | |
// RealSubject: RealImage class (Actual heavy object) | |
class RealImage implements Image { | |
private String filename; | |
RealImage(String filename) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Stack; | |
// Receiver: TextEditor | |
class TextEditor { | |
private StringBuilder text = new StringBuilder(); | |
void insertText(String insertedText) { | |
text.append(insertedText); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Product Interface: DatabaseConnection | |
interface DatabaseConnection { | |
void connect(); | |
void disconnect(); | |
} | |
// Concrete Products: MySqlConnection and PostgreSqlConnection | |
class MySqlConnection implements DatabaseConnection { | |
@Override | |
public void connect() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
// Observer interface | |
interface Observer { | |
void update(String weather); | |
} | |
// Subject interface | |
interface Subject { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |