views:

138

answers:

2

I have two class definitions in my Spring MVC web application named Class and Object respectively:

public Class {
//instance variables
int classId;
int className;


}


public Object {
    //instance variables
int objectId;
int objectName;


}

I also have a service that that returns a list of Class and Object defined as follows.

package com.service;


import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
import com.domain.Class;
import com.domain.Object;
import com.service.SearchManager;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;


import java.io.Serializable;

public class SempediaSearchManager implements com.service.SearchManager {

    private SessionFactory sessionFactory;
    private List<Class> classes;
    private List<Object> objects;



     public List<Class> getClassSeedSearch(String classSeed) {
         Configuration configuration = new Configuration().configure();
         sessionFactory = configuration.buildSessionFactory();
         Session session = sessionFactory.openSession();
         try {
         Query query = session.createQuery("from Class c where lower(c.className) like lower('"
                 + classSeed + "%')");
         return query.list();
         } finally {
         session.close();
         }

     }
     public List<Object> getObjectSeedSearch(String objectSeed) {
         Configuration configuration = new Configuration().configure();
         sessionFactory = configuration.buildSessionFactory();
         Session session = sessionFactory.openSession();
         try {
         Query query = session.createQuery("from Object o where lower(o.objectName) like lower('"
                 + objectSeed + "%')");
         return query.list();
         } finally {
         session.close();
         }
     }


     ?????
     skeleton method
     public List<ClassesAndObjects> getObjectorClassSeedSearch(String objectOrClassSeed) {
      ?????
     }

The view I present has a search box that allow a user to search for either classes or objects based on the repsective list returned by the methods getObjectSeedSearch, and getClassSeedSearch - List, List respectively

What I'd like to allow the search text box to simply search for both classes and objects, returning perhaps a merged list - List - if you would that has both classes and Objects by leveraging a method getObjectorClassSeedSearch

What would be the best way forward in implementing this. I vaguely know I'd want to create a wrapper class that would take both Class and Object and perhaps polymorphically at run time determine what instance and item is when being returned of propped from the list. I guessing I would have a corresponding bean for this in my application. perhaps an exercise with generics.

What would be an efficient way to proceed?

Thanks

+2  A: 

Have a common interface/abstract class for both of them:

public abstract class Name
{
    private int id;
    private String name;
    // all attendant ctors and getter/setter stuff here.
}

You can return one List<Name> as needed that way.

Good names matter. Overloading "Class" and "Object", when they're already part of the java.lang package, will only cause you and future maintainers of your code grief.

These might be examples, but I see no reason for either of these classes. There's no abstraction or rich behavior. I'd say this is a design worth rethinking, based on what I see here.

duffymo
Thanks. But assuming I didn't use Class and Object class definitions, and used two different ones, possibly with different methods and instance variables except for instance variables; clas1Name, class1ID, class2Name, and class2ID, common to both classes would an approach to a solution - a list that holds objects of either class- still be as you suggest
Yaw Reuben
Your proposed design still suffers from the same problem, and my abstract class would still work. If you're going to have a single type in the List, and you should want to, then you've got to have a common interface or abstract base class.
duffymo
+1  A: 

I recommend creating an interface and having some kind of "get details" method that both Class and Object implement. Figuring out that common method can be tough, but worst case you can just use instanceof and cast it after getting it out of the list (although that would be a symptom of bad design).

eg:

public interface SearchResult {

    public String getDetails();

}

and then:

public class Class implements SearchResult {
    int classId;
    int className;

    public String getDetails() {
        return "id " + classId + ", name " + className;
    }
}

public class Object implements SearchResult {
    int objectId;
    int objectName;

    public String getDetails() {
        return "id " + objectId + ", name " + objectName;
    }
}

Obviously that's a simplistic example but you get the idea.

Spyder