Search This Blog

2023/04/19

Proxy Design Pattern

In object-oriented programming, the Proxy design pattern provides a way to control

the access to an object and add additional behavior to it without
modifying its original implementation.

class car {

constructor(make, model) {
this.make = make;
this.model = model;
}

start() {
console.log(`${this.make} ${this.model} started`)
}
}

class carProxy {
car = null;

constructor(ocar) {
this.car = ocar
}

start(hour) {
if (hour < 10 || hour > 19) {
console.log("Not Allowed outside Business Hour")
} else {
this.car.start()
}
}
}

var c = new car("Honda","Corolla")
var cp = new carProxy(c)
cp.start(9)
cp.start(11)

Output:
Not Allowed outside Business Hour
Honda Corolla started



No comments:

Post a Comment