Proxy

Example 1:

package az.proxy;

import java.util.Date;

public class Attendance {

private Date date;
private boolean isPresent;

public Attendance(Date date, boolean isPresent) {
this.date = date;
this.isPresent = isPresent;
}

public Date getDate() {
return date;
}

public void setDate(Date date) {
this.date = date;
}

public boolean isPresent() {
return isPresent;
}

public void setPresent(boolean present) {
isPresent = present;
}
}



package az.proxy;

public interface DailySession {

void attendLesson();

} 


package az.proxy;

public class Student implements DailySession {

private Attendance attendance;

public Student(Attendance attendance) {
this.attendance = attendance;
}

public Attendance getAttendance() {
return attendance;
}

public void setAttendance(Attendance attendance) {
this.attendance = attendance;
}

@Override
public void attendLesson() {
System.out.println("Attending the session...");
}

}



package az.proxy;

public class StudentProxy extends Student {

public StudentProxy(Attendance attendance) {
super(attendance);
}

@Override
public void attendLesson() {
if (getAttendance().isPresent()) {
super.attendLesson();
} else throw new RuntimeException("Student is not present");
}

}



package az.proxy;

import java.util.Date;

public class Main {

public static void main(String[] args) {
Student student = new StudentProxy(new Attendance(new Date(), false));
student.attendLesson();
}

}


or


package az.proxy;

import java.util.Date;

public class Main {

public static void main(String[] args) {
DailySession student = new StudentProxy(new Attendance(new Date(), true));
student.attendLesson();
}

}



                                                                Dynamic Proxy

*** AOP using Dynamic Proxy


















































Комментарии

Популярные сообщения из этого блога

Lesson1: JDK, JVM, JRE

SE_21_Lesson_11: Inheritance, Polymorphism

SE_21_Lesson_9: Initialization Blocks, Wrapper types, String class