Proxy dizayn nümunəsi (Pattern)
-
Proxy Pattern — obyektə birbaşa çıxışı nəzarət altında saxlamaq üçün istifadə olunan dizayn pattern-dir. Bu pattern-in əsas məqsədi, obyektə vasitəçi (proxy) vasitəsilə nəzarət etməkdir. JavaScript-də bu konsept həm real həyatda, həm də proqramlaşdırmada çox faydalıdır.
Bu yazıda aşağıdakı Proxy Pattern növlərini incələyəcəyik:
- Property Proxy
- Protection Proxy
- Virtual Proxy
1.
valueOf()
vətoString()
ilə xüsusi tip obyektlərAşağıdakı
Percentage
class-ıvalueOf
vətoString
metodları ilə say kimi istifadə edilə bilən obyekt yaradır:class Percentage { constructor(value) { this.value = value; } toString() { return `${this.value}%`; } valueOf() { return this.value / 100; } } let fivePercent = new Percentage(5); console.log(fivePercent.toString()); // "5%" console.log(`5% of 50 is ${50 * fivePercent}`); // 2.5
️ Bu, JavaScript-in type coercion xüsusiyyətindən yararlanır:
valueOf()
metodu çağrıldıqda obyektin rəqəmsal dəyəri kimi işlənir.
2. Property Proxy – Dəyərlərə nəzarət və loglama
Property
classı ilə dəyərlərin dəyişməsinə nəzarət və loglama imkanı yaradılır:class Property { constructor(value, name = "") { this._value = value; this.name = name; } get value() { return this._value; } set value(newValue) { if (this._value === newValue) return; console.log(`Property ${this.name} changed from ${this._value} to ${newValue}`); this._value = newValue; } } class Creature { constructor() { this._agility = new Property(10, "agility"); } get agility() { return this._agility.value; } set agility(newValue) { this._agility.value = newValue; } } let creature = new Creature(); creature.agility = 15; // Property agility changed from 10 to 15 creature.agility = 15; // No change console.log(creature.agility); // 15
Fayda: Hər dəfə dəyər dəyişəndə loglama aparılır. Lazım olsa,
undo
sistemi və ya dəyişiklik tarixi də əlavə edilə bilər.
3. Protection Proxy – Təhlükəsizlik nəzarəti
Aşağıdakı nümunədə bir maşını yalnız yaşa uyğun olaraq sürməyə icazə verilir:
class Car { drive() { console.log("Car is being driven"); } } class CarProxy { constructor(driver) { this.driver = driver; this._car = new Car(); } drive() { if (this.driver.age >= 16) { this._car.drive(); } else { console.log("Driver is too young to drive"); } } } class Driver { constructor(age) { this.age = age; } } let car1 = new CarProxy(new Driver(15)); car1.drive(); // Driver is too young to drive let car2 = new CarProxy(new Driver(22)); car2.drive(); // Car is being driven
Protection Proxy sistemə yalnız uyğun istifadəçilərin çıxışını təmin edir. Məsələn, admin panelinə giriş, API endpoint-ə çıxış və s.
4. Virtual Proxy – Tənbəl yükləmə (Lazy Loading)
Image
obyektinin yaddaşı çox istifadə edə biləcəyini fərz edək. Onu yalnız ilk dəfə lazım olduqda yaratmaq istərik:class Image { constructor(url) { this.url = url; console.log(`Image created with URL: ${this.url}`); } draw() { console.log(`Drawing image from ${this.url}`); } } class LazyImage { constructor(url) { this.url = url; } draw() { if (!this._image) { console.log(`Loading image from ${this.url}`); this._image = new Image(this.url); } this._image.draw(); } } function drawImage(image) { console.log("About to draw image..."); image.draw(); console.log("Done drawing the image."); } let image = new LazyImage("https://example.com/image.jpg"); drawImage(image); // Yalnız ilk dəfə draw çağırıldıqda Image yaradılır
Fayda: Lazım olmayan resursların istifadəsini gecikdirməklə performansı optimallaşdırır.
Nəticə
Proxy Pattern JavaScript-də çox güclü bir konseptdir. Bu pattern aşağıdakı hallarda çox faydalıdır:
Növ Təsvir Property Proxy
Mülkət dəyişikliklərinə nəzarət, loglama Protection Proxy
Giriş nəzarəti və hüquq yoxlaması Virtual Proxy
Resursların tənbəl yüklənməsi (lazy loading) Bu nümunələr real həyatda istər UI komponentləri, istərsə də API istəkləri, giriş kontrol sistemləri üçün praktik imkanlar yaradır.
Bilik paylaşdıqca artan bir sərvətdir