-
Notifications
You must be signed in to change notification settings - Fork 1
/
ProtectionProxy.java
57 lines (46 loc) · 1004 Bytes
/
ProtectionProxy.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Protection Proxy
package structural.proxy;
interface Drivable
{
void drive();
}
class Car implements Drivable
{
protected Driver driver;
public Car(Driver driver) {
this.driver = driver;
}
@Override
public void drive() {
System.out.println("Car being driven");
}
}
class Driver
{
public int age;
public Driver(int age) {
this.age = age;
}
}
// Proxy to make some checks on driver age - using inheritance
class CarProxy extends Car {
public CarProxy(Driver driver) {
super(driver);
}
@Override
public void drive() {
if(driver.age >= 16)
super.drive();
else
System.out.println("Driver too young!");
}
}
class ProtectionProxyDemo
{
public static void main(String[] args) {
Car car = new Car(new Driver(12));
car.drive(); // no age checks made
Car car2 = new CarProxy(new Driver(12)); // proxy used here
car2.drive();
}
}