模拟页面跳转,使用代理反向传值

假设有这样一个场景,app第一个页面是一个正方形,第二个页面有一个按钮,点击第一个的某按钮后,跳转到第二个页面,第二个页面修改正方形的边长,然后返回第一个页面,此时当前页面的矩形变化。

import java.util.Objects;

/**
 * Created by itfanr on 14/11/11.
 */
public interface ClickDelegate {
    public void lengthDidChange(SecondPage sp , int length) ;
}

/**
 * Created by itfanr on 14/11/11.
 */
public class FirstPage implements ClickDelegate {
    private int area ;
    @Override
    public void lengthDidChange(SecondPage sp, int length) {
        this.area = length *2 ;
        System.out.println("back to first page");
        System.out.println("area is also changed : " + this.area+ "\n");

    }

    public  SecondPage jumpToSecond(){
        System.out.println("jump to second page");
        SecondPage sp  = new SecondPage() ;
        sp.cd = this ;
        return sp ;
    }
}

/**
 * Created by itfanr on 14/11/11.
 */
public class SecondPage {
    private int length = 10;
    public ClickDelegate cd = null ;

    public void click() {
        System.out.println("clicked in second page !");
        this.length++;
        if (this.cd != null) {
            System.out.println("length is " + this.length);
            this.cd.lengthDidChange(this, this.length) ;
        }
    }
}

/**
 * Created by itfanr on 14/11/11.
 */
public class Client {
    public static void main(String args[]){
        FirstPage fp = new FirstPage() ;
        SecondPage sp = fp.jumpToSecond();
        sp.click();
        sp.click();
    }
}

输出结果如下:

jump to second page
clicked in second page !
length is 11
back to first page
area is also changed : 22

clicked in second page !
length is 12
back to first page
area is also changed : 24


感觉和普通的观察者模式有点不同。如果是普通的观察者模式,应该是第二个页面观察第一个页面的变化。

转载于:https://my.oschina.net/itfanr/blog/343212

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值