Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Proxy Pattern

Frank Xu
January 07, 2016

Proxy Pattern

讲述了代理模式在 App 开发过程中的应用,解耦调用和实现。

Frank Xu

January 07, 2016
Tweet

More Decks by Frank Xu

Other Decks in Programming

Transcript

  1. A proxy, in its most general form, is a class

    functioning as an interface to something else.
  2. REST API interface WRLoginService {
 @JSONEncoded
 @POST("/login")
 Observable<LoginInfo> login(
 @JSONField("code")

    String code,
 @JSONField("deviceId") String deviceId,
 @JSONField("wxToken") Integer wxToken
 );
 }
  3. REST API interface WRLoginService {
 @JSONEncoded
 @POST("/login")
 Observable<LoginInfo> login(
 @JSONField("code")

    String code,
 @JSONField("deviceId") String deviceId,
 @JSONField("wxToken") Integer wxToken
 );
 } LoginInfo loginInfo = RestAdapter#create(WRLoginService.class).login(/** */);
  4. REST API interface WRLoginService {
 @JSONEncoded
 @POST("/login")
 Observable<LoginInfo> login(
 @JSONField("code")

    String code,
 @JSONField("deviceId") String deviceId,
 @JSONField("wxToken") Integer wxToken
 );
 } LoginInfo loginInfo = RestAdapter#create(WRLoginService.class).login(/** */); login() 构造URL 发请求 JSON 反序列列化
  5. // Define events: public class MessageEvent { /* Additional fields

    if needed */ } // Prepare subscribers: eventBus.register(this); public void onEvent(AnyEventType event) { /* Do something */ }; // Post events: eventBus.post(event); greenrobot/EventBus
  6. // Define events: public class MessageEvent { /* Additional fields

    if needed */ } // Prepare subscribers: eventBus.register(this); public void onEvent(AnyEventType event) { /* Do something */ }; // Post events: eventBus.post(event); greenrobot/EventBus // Define events: public class MessageEvent { /* Additional fields if needed */ } // Prepare subscribers: eventBus.register(this); public void onEvent(AnyEventType event) { /* Do something */ }; // Post events: eventBus.post(event);
  7. Bus bus = new Bus(); bus.post(new AnswerAvailableEvent(42)); @Subscribe public void

    answerAvailable(AnswerAvailableEvent event) { // TODO: React to the event somehow! } bus.register(this); @Produce public AnswerAvailableEvent produceAnswer() { // Assuming 'lastAnswer' exists. return new AnswerAvailableEvent(this.lastAnswer); } square/otto
  8. Bus bus = new Bus(); bus.post(new AnswerAvailableEvent(42)); @Subscribe public void

    answerAvailable(AnswerAvailableEvent event) { // TODO: React to the event somehow! } bus.register(this); @Produce public AnswerAvailableEvent produceAnswer() { // Assuming 'lastAnswer' exists. return new AnswerAvailableEvent(this.lastAnswer); } square/otto Bus bus = new Bus(); bus.post(new AnswerAvailableEvent(42)); @Subscribe public void answerAvailable(AnswerAvailableEvent event) { // TODO: React to the event somehow! } bus.register(this); @Produce public AnswerAvailableEvent produceAnswer() { // Assuming 'lastAnswer' exists. return new AnswerAvailableEvent(this.lastAnswer); }
  9. // Define an Event public interface PushWatcher extents Watchers.Watcher {

    void notify(Message message); } // Publishing an Event Watchers.of(PushWatcher.class).notify(message); // Subscribing to an Event public class MyFragment extends Fragment implement PushWatcher { public void notify(Message message) { } public void onStart() { Watchers.bind(this); } } Watcher
  10. // Define an Event public interface PushWatcher extents Watchers.Watcher {

    void notify(Message message); } // Publishing an Event Watchers.of(PushWatcher.class).notify(message); // Subscribing to an Event public class MyFragment extends Fragment implement PushWatcher { public void notify(Message message) { } public void onStart() { Watchers.bind(this); } } Watcher // Define an Event public interface PushWatcher extents Watchers.Watcher { void notify(Message message); } // Publishing an Event Watchers.of(PushWatcher.class).notify(message); // Subscribing to an Event public class MyFragment extends Fragment implement PushWatcher { public void notify(Message message) { } public void onStart() { Watchers.bind(this); } }
  11. Proxy.newProxyInstance( clazz.getClassLoader(), new Class[] { clazz }, new WatcherHandler(clazz) )

    调⽤用委托 Proxy.newProxyInstance( clazz.getClassLoader(), new Class[] { clazz }, new WatcherHandler(clazz) )
  12. WatcherHandler static class WatcherHandler implements InvocationHandler {
 private final Class<?

    extends Watcher> clazz;
 WatcherHandler(Class<? extends Watcher> clazz) {
 this.clazz = clazz;
 }
 
 @Override
 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
 if (method.getDeclaringClass() == Object.class) {
 return method.invoke(this, args);
 }
 instance.trigger(clazz, method, args);
 return null;
 }
 }
  13. WatcherHandler static class WatcherHandler implements InvocationHandler {
 private final Class<?

    extends Watcher> clazz;
 WatcherHandler(Class<? extends Watcher> clazz) {
 this.clazz = clazz;
 }
 
 @Override
 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
 if (method.getDeclaringClass() == Object.class) {
 return method.invoke(this, args);
 }
 instance.trigger(clazz, method, args);
 return null;
 }
 } static class WatcherHandler implements InvocationHandler {
 private final Class<? extends Watcher> clazz;
 WatcherHandler(Class<? extends Watcher> clazz) {
 this.clazz = clazz;
 }
 
 @Override
 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
 if (method.getDeclaringClass() == Object.class) {
 return method.invoke(this, args);
 }
 instance.trigger(clazz, method, args);
 return null;
 }
 } EventBus
  14. WatcherHandler static class WatcherHandler implements InvocationHandler {
 private final Class<?

    extends Watcher> clazz;
 WatcherHandler(Class<? extends Watcher> clazz) {
 this.clazz = clazz;
 }
 
 @Override
 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
 if (method.getDeclaringClass() == Object.class) {
 return method.invoke(this, args);
 }
 instance.trigger(clazz, method, args);
 return null;
 }
 } static class WatcherHandler implements InvocationHandler {
 private final Class<? extends Watcher> clazz;
 WatcherHandler(Class<? extends Watcher> clazz) {
 this.clazz = clazz;
 }
 
 @Override
 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
 if (method.getDeclaringClass() == Object.class) {
 return method.invoke(this, args);
 }
 instance.trigger(clazz, method, args);
 return null;
 }
 } EventBus 事件类别
  15. WatcherHandler static class WatcherHandler implements InvocationHandler {
 private final Class<?

    extends Watcher> clazz;
 WatcherHandler(Class<? extends Watcher> clazz) {
 this.clazz = clazz;
 }
 
 @Override
 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
 if (method.getDeclaringClass() == Object.class) {
 return method.invoke(this, args);
 }
 instance.trigger(clazz, method, args);
 return null;
 }
 } static class WatcherHandler implements InvocationHandler {
 private final Class<? extends Watcher> clazz;
 WatcherHandler(Class<? extends Watcher> clazz) {
 this.clazz = clazz;
 }
 
 @Override
 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
 if (method.getDeclaringClass() == Object.class) {
 return method.invoke(this, args);
 }
 instance.trigger(clazz, method, args);
 return null;
 }
 } EventBus 事件类别 事件动作
  16. WatcherHandler static class WatcherHandler implements InvocationHandler {
 private final Class<?

    extends Watcher> clazz;
 WatcherHandler(Class<? extends Watcher> clazz) {
 this.clazz = clazz;
 }
 
 @Override
 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
 if (method.getDeclaringClass() == Object.class) {
 return method.invoke(this, args);
 }
 instance.trigger(clazz, method, args);
 return null;
 }
 } static class WatcherHandler implements InvocationHandler {
 private final Class<? extends Watcher> clazz;
 WatcherHandler(Class<? extends Watcher> clazz) {
 this.clazz = clazz;
 }
 
 @Override
 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
 if (method.getDeclaringClass() == Object.class) {
 return method.invoke(this, args);
 }
 instance.trigger(clazz, method, args);
 return null;
 }
 } EventBus 事件类别 事件动作 动作上下⽂文
  17. // Define an Event public interface PushWatcher extents Watchers.Watcher {

    void notify(Message message); } // Publishing an Event Watchers.of(PushWatcher.class).notify(message); // Subscribing to an Event public class MyFragment extends Fragment implement PushWatcher { public void notify(Message message) { } public void onStart() { Watchers.bind(this); } } 类型安全 & 重构
  18. // Define an Event public interface PushWatcher extents Watchers.Watcher {

    void notify(Message message); } // Publishing an Event Watchers.of(PushWatcher.class).notify(message); // Subscribing to an Event public class MyFragment extends Fragment implement PushWatcher { public void notify(Message message) { } public void onStart() { Watchers.bind(this); } } 类型安全 & 重构 // Define an Event public interface PushWatcher extents Watchers.Watcher { void notify(Message message); } // Publishing an Event Watchers.of(PushWatcher.class).notify(message); // Subscribing to an Event public class MyFragment extends Fragment implement PushWatcher { public void notify(Message message) { } public void onStart() { Watchers.bind(this); } }
  19. 两种实现 @BooleanValue(true) public static class HttpsOn implements Https {
 @Override


    public String schema() {
 return "https";
 }
 }
 
 @BooleanValue(false) public static class HttpsOff implements Https {
 @Override
 public String schema() {
 return "http";
 }
 }
  20. 使⽤用 new Endpoint() {
 @Override
 public String getUrl() {
 return

    Features.of(Https.class).schema()
 + "://" + Features.of(ServiceEndPoint.class).url();
 }
 };
  21. 实现部分接⼝口 public Observable<AllTimeLineList> LoadAllTimeLineList() {
 return LoadTimeLine(
 ReviewListType.TIMELINE.getTypeCode(),
 0,
 0l,


    LOAD_REVIEW_LIST_COUNT_AT_ONCE
 ).map(new Func1<TimelineList, AllTimeLineList>() {
 @Override
 public AllTimeLineList call(TimelineList timeLineList) {
 return new AllTimeLineList(timeLineList);
 }
 });
 }
  22. 实现部分接⼝口 public Observable<FriendsTimeLineList> LoadFriendsTimeLineList(long synckey) {
 return LoadTimeLine(
 ReviewListType.TIMELINE_FRIEND.getTypeCode(),
 synckey,


    0l,
 LOAD_REVIEW_LIST_COUNT_AT_ONCE
 ).map(new Func1<TimelineList, FriendsTimeLineList>() {
 @Override
 public FriendsTimeLineList call(TimelineList timeLineList) {
 return new FriendsTimeLineList(timeLineList);
 }
 });
 }
  23. Factory 代理理 okHttpClient.setSslSocketFactory(Reflections.proxy(WRApplicationContext.sharedInstance(),
 SSLSocketFactory.class, new InvocationHandler() {
 @Override
 public Object

    invoke(Object proxy, Method method, Object[] args) throws Throwable {
 if (method.getDeclaringClass() == Object.class) {
 return method.invoke(this, args);
 }
 method = FeatureSSLSocketFactory.class.getDeclaredMethod(
 method.getName(), method.getParameterTypes());
 return method.invoke(Features.of(FeatureSSLSocketFactory.class), args);
 }
 }));
  24. JavaBeanStorage public void setDiscoverSyncKey(long syncKey) {
 setValue(AccountManager.getInstance().getCurrentLoginAccountId(),
 WRSettingKey.DISCOVER_SYNC_KEY, String.valueOf(syncKey));
 }


    
 public long getDiscoverSyncKey() {
 return getLongValue(WRSettingKey.DISCOVER_SYNC_KEY,
 AccountManager.getInstance().getCurrentLoginAccountId());
 }
  25. JavaBeanStorage public void setReadingBookId(String bookId) {
 setValue(AccountManager.getInstance().getCurrentLoginAccountId(), 
 WRSettingKey.READING_BOOK_ID, bookId);


    }
 
 public String getReadingBookId() {
 return getStringValue(WRSettingKey.READING_BOOK_ID,
 AccountManager.getInstance().getCurrentLoginAccountId());
 }