Created
December 4, 2023 16:09
-
-
Save kishankg/c623b9745be191c1c97ea75336688a17 to your computer and use it in GitHub Desktop.
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); | |
} | |
// Refined Abstraction: CheckingAccount | |
class CheckingAccount extends BankAccount { | |
CheckingAccount(TransactionProcessor transactionProcessor) { | |
super(transactionProcessor); | |
} | |
@Override | |
void processTransaction(double amount) { | |
System.out.println("Processing transaction for Checking Account"); | |
transactionProcessor.process(amount); | |
} | |
} | |
// Refined Abstraction: SavingsAccount | |
class SavingsAccount extends BankAccount { | |
SavingsAccount(TransactionProcessor transactionProcessor) { | |
super(transactionProcessor); | |
} | |
@Override | |
void processTransaction(double amount) { | |
System.out.println("Processing transaction for Savings Account"); | |
transactionProcessor.process(amount); | |
} | |
} | |
// Implementor: TransactionProcessor | |
interface TransactionProcessor { | |
void process(double amount); | |
} | |
// Concrete Implementor: StandardProcessor | |
class StandardProcessor implements TransactionProcessor { | |
@Override | |
public void process(double amount) { | |
System.out.println("Standard Transaction Processing"); | |
// Actual transaction processing logic | |
} | |
} | |
// Concrete Implementor: PremiumProcessor | |
class PremiumProcessor implements TransactionProcessor { | |
@Override | |
public void process(double amount) { | |
System.out.println("Premium Transaction Processing"); | |
// Additional premium transaction processing logic | |
} | |
} | |
// Client Code | |
public class BridgePatternExample { | |
public static void main(String[] args) { | |
// Standard processing for a checking account | |
BankAccount checkingAccount = new CheckingAccount(new StandardProcessor()); | |
checkingAccount.processTransaction(1000.0); | |
// Premium processing for a savings account | |
BankAccount savingsAccount = new SavingsAccount(new PremiumProcessor()); | |
savingsAccount.processTransaction(500.0); | |
} | |
} |
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: Vehicle | |
abstract class Vehicle { | |
protected AssemblyProcess assemblyProcess; | |
Vehicle(AssemblyProcess assemblyProcess) { | |
this.assemblyProcess = assemblyProcess; | |
} | |
abstract void manufacture(); | |
} | |
// Refined Abstraction: Car | |
class Car extends Vehicle { | |
Car(AssemblyProcess assemblyProcess) { | |
super(assemblyProcess); | |
} | |
@Override | |
void manufacture() { | |
System.out.println("Manufacturing Car"); | |
assemblyProcess.assemble(); | |
} | |
} | |
// Refined Abstraction: Bike | |
class Bike extends Vehicle { | |
Bike(AssemblyProcess assemblyProcess) { | |
super(assemblyProcess); | |
} | |
@Override | |
void manufacture() { | |
System.out.println("Manufacturing Bike"); | |
assemblyProcess.assemble(); | |
} | |
} | |
// Implementor: AssemblyProcess | |
interface AssemblyProcess { | |
void assemble(); | |
} | |
// Concrete Implementor: StandardAssembly | |
class StandardAssembly implements AssemblyProcess { | |
@Override | |
public void assemble() { | |
System.out.println("Standard Assembly Process"); | |
} | |
} | |
// Concrete Implementor: AdvancedAssembly | |
class AdvancedAssembly implements AssemblyProcess { | |
@Override | |
public void assemble() { | |
System.out.println("Advanced Assembly Process"); | |
} | |
} | |
// Client Code | |
public class BridgePatternExample { | |
public static void main(String[] args) { | |
// Standard assembly for a car | |
Vehicle car = new Car(new StandardAssembly()); | |
car.manufacture(); | |
// Advanced assembly for a bike | |
Vehicle bike = new Bike(new AdvancedAssembly()); | |
bike.manufacture(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment