STRUCTURAL PATTERN

Proxy Pattern

نمط الوسيط

يعني إيه؟ زي السكرتير - بيتحكم في الوصول للمدير! 🛡️
بيعمل Access Control, Caching, Logging! 📋

أنواع الـ Proxy 🛡️

🔐

Protection Proxy

بيتحكم في الـ Access - مين يقدر يوصل!

💾

Caching Proxy

بيخزن النتايج - مش بيكرر الشغل!

Virtual Proxy

Lazy Loading - بيحمل لما يحتاج بس!

الكود 💻

Proxy.js
// 🎯 Real Subject
class BankAccount {
  constructor(balance) { this.balance = balance; }
  withdraw(amount) {
    this.balance -= amount;
    return `💰 Withdrew $${amount}. Balance: $${this.balance}`;
  }
}

// 🛡️ Protection Proxy
class BankAccountProxy {
  constructor(account, userRole) {
    this.account = account;
    this.userRole = userRole;
  }
  
  withdraw(amount) {
    // 🔐 Access Control!
    if (this.userRole !== 'owner') {
      return '❌ Access Denied! Only owner can withdraw.';
    }
    if (amount > 1000) {
      return '⚠️ Amount exceeds limit! Max: $1000';
    }
    // ✅ Forward to real object
    return this.account.withdraw(amount);
  }
}

// Usage
const account = new BankAccount(5000);
const proxy = new BankAccountProxy(account, 'guest');
proxy.withdraw(100); // ❌ Access Denied!

جرب! 🎮

🏦

Balance: $5000

Role: guest

// BankAccountProxy ready!
Flyweight Adapter