Skip to content

Instantly share code, notes, and snippets.

@kishankg
Created December 4, 2023 15:06
Show Gist options
  • Save kishankg/4c4b3a435310683791f8f7ec84f69582 to your computer and use it in GitHub Desktop.
Save kishankg/4c4b3a435310683791f8f7ec84f69582 to your computer and use it in GitHub Desktop.
// Subject: Image interface
interface Image {
void display();
}
// RealSubject: RealImage class (Actual heavy object)
class RealImage implements Image {
private String filename;
RealImage(String filename) {
this.filename = filename;
}
@Override
public void display() {
// Simulating the heavy loading of the image
System.out.println("Displaying real image: " + filename);
}
private void loadImageFromDisk() {
// Simulating the actual loading of the image from disk
System.out.println("Loading image from disk: " + filename);
}
}
// Proxy: ProxyImage class (Virtual proxy)
class ProxyImage implements Image {
private RealImage realImage;
private String filename;
ProxyImage(String filename) {
this.filename = filename;
}
@Override
public void display() {
if (realImage == null) {
// Load the real image only when it is actually needed
realImage = new RealImage(filename);
realImage.loadImageFromDisk();
}
// Delegate the display operation to the real image
realImage.display();
}
}
// Client Code
public class ProxyPatternExample {
public static void main(String[] args) {
// Creating a proxy for an image (not loaded yet)
Image imageProxy = new ProxyImage("sample.jpg");
// The image is loaded and displayed only when necessary
imageProxy.display();
// The image is not reloaded, and the cached version is displayed
imageProxy.display();
}
}
import java.util.HashMap;
import java.util.Map;
// Subject: FinancialService interface
interface FinancialService {
double getBalance(String accountNumber);
}
// RealSubject: RealFinancialService class (Actual service)
class RealFinancialService implements FinancialService {
private Map<String, Double> accountBalances;
RealFinancialService() {
// Simulating the actual financial service with account balances
accountBalances = new HashMap<>();
accountBalances.put("123456", 1000.0);
accountBalances.put("789012", 500.0);
}
@Override
public double getBalance(String accountNumber) {
// Simulating accessing the real financial service
System.out.println("Accessing real financial service to get balance for account: " + accountNumber);
return accountBalances.getOrDefault(accountNumber, 0.0);
}
}
// Proxy: FinancialServiceProxy class (Virtual proxy)
class FinancialServiceProxy implements FinancialService {
private RealFinancialService realService;
private Map<String, Double> balanceCache;
FinancialServiceProxy() {
// The real service is not created until it is actually needed
balanceCache = new HashMap<>();
}
@Override
public double getBalance(String accountNumber) {
// Check if the balance is in the cache
Double cachedBalance = balanceCache.get(accountNumber);
if (cachedBalance != null) {
System.out.println("Returning cached balance for account: " + accountNumber);
return cachedBalance;
}
// If not in the cache, create the real service and fetch the balance
if (realService == null) {
realService = new RealFinancialService();
}
// Perform additional security checks before accessing the real service
if (performSecurityChecks(accountNumber)) {
double balance = realService.getBalance(accountNumber);
// Cache the fetched balance for future use
balanceCache.put(accountNumber, balance);
return balance;
} else {
System.out.println("Security checks failed. Access denied.");
return 0.0; // Or throw an exception based on the security policy
}
}
private boolean performSecurityChecks(String accountNumber) {
// Simulating security checks (e.g., user authentication, authorization)
return accountNumber.length() == 6; // A simple example of a security check
}
}
// Client Code
public class ProxyPatternExample {
public static void main(String[] args) {
// Creating a proxy for the financial service (not created yet)
FinancialService financialServiceProxy = new FinancialServiceProxy();
// Accessing the balance through the proxy
double balance1 = financialServiceProxy.getBalance("123456");
System.out.println("Balance 1: $" + balance1);
// Accessing the balance again (retrieved from the cache)
double balance2 = financialServiceProxy.getBalance("123456");
System.out.println("Balance 2: $" + balance2);
// Accessing the balance with invalid account number (security checks fail)
double invalidBalance = financialServiceProxy.getBalance("999999");
System.out.println("Invalid Balance: $" + invalidBalance);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment