1.静态代理实现要求
- 有真实的角色
- 有代理的对象
- 真实角色和代理对象实现同一个代理接口
2.静态代理事例
MarrayContent代理MarryPerson,MarryPerson只关注结婚其他事情都交给MarrayContent代理对象去做
public class StaticProxy { public static void main(String args[]){ MarryPerson person = new MarryPerson(); Marray content = new MarrayContent(person); content.marray(); } } //代理接口 interface Marray{ void marray(); } //真实角色 class MarryPerson implements Marray{ @Override public void marray() { System.err.println("结婚"); } } //代理对象 class MarrayContent implements Marray{ private MarryPerson marryPerson; public MarrayContent(MarryPerson marryPerson){ this.marryPerson =marryPerson; } private void searchTarget(){ System.err.println("寻找合适对象!"); } private void detail(){ System.err.println("婚后处理!"); } @Override public void marray() { searchTarget(); marryPerson.marray(); detail(); } }
来源:https://www.cnblogs.com/mf001/p/8462075.html