`

java 回调 广义上的回调其实是一种设计模式

    博客分类:
  • java
阅读更多
public interface IFilterMethod {
    
    List<UserModel> doCallFilter(UserModel userModel);

}

 

public class UserModel implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private String userName;

    private String password;

    private String address;

    private String type;

    private String role;
    
    public UserModel() {}
    
    public UserModel(String _userName,String _password) {
        this.userName = _userName;
        this.password = _password;
    }
    
    public UserModel(String _userName,String _password,String _address) {
        this.userName = _userName;
        this.password = _password;
        this.address = _address;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    @Override
    public String toString() {
        return "userName = " + userName + ",password = " + password + ",type = " + type + ",address = " + address + ",role = "
               + role;
    }

 

public class ModelLogic {
    public static void main(String[] args) {
        IFilterMethod filterMethod = new IFilterMethod() {
            @Override
            public List<UserModel> doCallFilter(UserModel userModel) {
                List<UserModel> list = new ArrayList<UserModel>();
                if (null != userModel) {
                    if (null == userModel.getAddress()) {
                        list.add(new UserModel("hello1", "world1"));
                        return list;
                    } else {
                        list.add(new UserModel("hello2", "world2", "address2"));
                        list.add(new UserModel("hello3", "world3", "address3"));
                        return list;
                    }
                }
                return null;
            }
        };

        first(filterMethod);
        second(filterMethod);
    }

    public static void first(IFilterMethod filterMethod) {
        UserModel userModel = new UserModel();
        userModel.setUserName("hechuan");
        userModel.setPassword("password");

        List<UserModel> doCallFilter = filterMethod.doCallFilter(userModel);
        for (UserModel model : doCallFilter) {
            System.out.println(model);
        }
    }

    public static void second(IFilterMethod filterMethod) {
        UserModel userModel = new UserModel();
        userModel.setUserName("hechuan");
        userModel.setPassword("password");
        userModel.setAddress("hangzhou");

        List<UserModel> doCallFilter = filterMethod.doCallFilter(userModel);
        for (UserModel model : doCallFilter) {
            System.out.println(model);
        }
    }
}

 

分享到:
评论

相关推荐

    用于时频分析的广义s变换变换代码(时频分析特别有用)

    这里提到的“用于时频分析的广义s变换变换代码”是基于MATLAB实现的一种方法,适用于对非平稳信号进行深入研究。 **广义S变换(Generalized S Transform, GST)**: 广义S变换是信号处理中的一个概念,是对传统短时...

    工程硕士学位论文 基于Android+HTML5的移动Web项目高效开发探究

    Viewport 移动设备上的Viewport就是设备的屏幕上能用来显示网页的一块区域,即浏览器上用来显示网页的那部分区域。Viewport不局限于浏览器可视区域的大小,可能比浏览器的可视区域要大,也可能比浏览器的可视区域要...

    Android 应用框架原理与程序设计36技.pdf

    - **层次间的交互**:通过接口和回调机制实现不同层次间的通信。 #### 1.4 框架的“无用之用”效果 - **解释**:“无用之用”是指看似无用的设计原则实际上对于整体架构的健壮性和灵活性至关重要。 - **例子**:...

    AngularJS学习笔记

    - **11.2 广义回调管理**:$http服务支持Promise模式,可以优雅地处理异步操作的成功和失败情况。 #### 12. 工具函数 - **12.1 上下文绑定**:AngularJS提供了一些工具函数来帮助开发者绑定函数的执行上下文。 - *...

Global site tag (gtag.js) - Google Analytics