`

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);
        }
    }
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics