java 回调 广义上的回调其实是一种设计模式_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > java 回调 广义上的回调其实是一种设计模式

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

 2013/8/21 12:59:18  hezhou_0521  程序员俱乐部  我要评论(0)
  • 摘要:publicinterfaceIFilterMethod{List<UserModel>doCallFilter(UserModeluserModel);}publicclassUserModelimplementsSerializable{/****/privatestaticfinallongserialVersionUID=1L;privateStringuserName;privateStringpassword;privateStringaddress
  • 标签:Java 模式 设计 设计模式
class="接口" name="code">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);
        }
    }
}

?

发表评论
用户名: 匿名