代理模式-静态代理
相对于动态代理,静态代理好理解多了。
看静态代理涉及的角色以及相关关系图
再来看看代码实现,(相关知识写在代码注释中了~呵~)
//抽象对象 public interface ISubject { public void request(); }
//真实对象,实现类 public class SubjectImpl implements ISubject{ @Override public void request() { System.out.println("hello world"); } }
//代理对象 ,代理对象和实际对象都实现了抽象对象 //这样的好处,可以用代理对象来完成相应的操作, //也可以用实际对象来完成操作 //某些情况,就是不想通过实际对象去完成操作, //要交给代理对象去完成相应的操作. public class SubjectProxy implements ISubject { //组合真实对象 private ISubject subject = new SubjectImpl(); @Override public void request() { //在执行实际对象的request之前,还可以做一些其他操作 //是不是很像Spring 中的AOP啊 //-------------------------- //实际上是调用真实对象的request //只是这是代理对象帮忙去调用reqeust //很符合实际中的代理意义 subject.request(); //-------------------------- //还可以在实际对象操作结束之后,做一些其他操作 } }
public class Test { public static void main(String[] args) { ISubject proxy = new SubjectProxy(); proxy.request(); } }
完成,可以看到静态代理和动态代理的区别,静态代理,一个真实对象对应一个代理,如果有很多个真实对象需要代理,那么就需要很多个代理对象了,而动态代理的作用就在于此,他有一个总代理,可由代理工厂根据进入的代理类来得到代理对象。
呵呵~~
2022年8月27日 21:26
The Rajshahi Board is also completed the Junior School Certificate and Junior Dakil (Grade-8) terminal exams successfully under Secondary and Higher Secondary Education Board, and a huge number of students have appeared from all districts of Rajshahi Division, and those subject wise exams are completed between 1st to 3rd weeks at all schools across the country along with the divisional schools to the academic year of 2022. Both of JSC & JDC students are waiting to check their exam result with full marksheet in student wise to get total GPA grade for this year terminal exams, JDC Result rajshahi Grade 8th standard is the most important examination for secondary education in the country and this is gateway to secondary education in the country, and the school education department will announce student wise result with total marksheet online and this year also published same like as previous years.