يعني إيه؟
زي السكرتير - بيتحكم في الوصول للمدير! 🛡️
بيعمل Access Control, Caching, Logging! 📋
بيتحكم في الـ Access - مين يقدر يوصل!
بيخزن النتايج - مش بيكرر الشغل!
Lazy Loading - بيحمل لما يحتاج بس!
// 🎯 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