List<UserInfo> findUser(int category,int total);
List<UserInfo> findUser(int category);
方法一样,返回值类型一样,就是参数不一样,里面具体算法不一样,用什么模式比较好呢?策略,工厂?不是为了模式而模式,就是想探讨下?

解决方案 »

  1.   

    List<UserInfo> findUser(int category,int total)List<UserInfo> findUser(int category){
        return findUser(category,Integher.MAX_VALUE)
    }
      

  2.   

    List<UserInfo> findUser(int category,int total)List<UserInfo> findUser(int category){
      return findUser(category,Integher.MAX_VALUE)
    }
      

  3.   

    策略,参数不一致的话,可以用 Object args... 的方式。 
      

  4.   


    public interface IPerson {
        public List<UserInfo> findUser();
    }public class StudentImpl implements IPerson  {
       private int category;
       private int total;
       private List<UserInfo> list;
       public StudentImpl (int category,int total) {
           this.category = category;
           this.total = total ;
           this.list = new ArrayList<UserInfo>();
       }
       
       public List<UserInfo> findUser() {
            list = 根据构造函数的参数写处理方法
            return list;
       }}public class Teacher implements IPerson  {
          private int category;
          private List<UserInfo> list;      public Teacher () {
              this.category = category;
              this.list = new ArrayList<UserInfo>();      }      public List<UserInfo> findUser() {
            list = 根据构造函数的参数写处理方法
             return list;
          }
    }public class PersonHelper() {
         IPerson  person;
         public PersonHelper(IPerson  person) {
     this.person = person;
         }  public List<UserInfo> findUser() {
    return person.findUser();
     }
    }public class test {
    private  IPerson  person;
    if(没有total) {
    person = new Teacher(category);
    }
    else
    {
    person = new StudentImpl(category,total);
    }

    PersonHelper helper = new PersonHelper(person);
    List<UserInfo> list = helper.findUser(); .................
    }自己写一个吧,不知道算不算
      

  5.   

    设计模式,是要看语义的。不能只看,什么返回值,什么参数类型。你这个语义看似,就是根据相应的信息返回返回用户信息。可以用策略模式。根据不同的策略,返回不同用户信息。至于参数不一样可以用 Object args... 的方式。