静态代理模式
- 真实对象和代理对象都要实现同一个接口
- 代理对象要代理真实角色
好处
代理对象可以实现真实对象许多实现不了的功能
//静态代理
public class StaticProxy {
public static void main(String[] args) {
GotoSchoolHelper gotoSchoolHelper = new GotoSchoolHelper(new You());
gotoSchoolHelper.goToSchool();
}
}
interface GoToSchool{
void goToSchool();
}
//真实角色
class You implements GoToSchool{
@Override
public void goToSchool() {
System.out.println("张三去上学了");
}
}
//代理角色
class GotoSchoolHelper implements GoToSchool{
private GoToSchool people;
public GotoSchoolHelper(GoToSchool target) {
this.people= target;
}
@Override
public void goToSchool() {
before();
this.people.goToSchool();
after();
}
private void before()
{
System.out.println("上学之前,吃早饭");
}
private void after(){
System.out.println("上学之后,放学");
}
}