Pregunta

Currently ,Managing few algorithms which perform set of normal operations on set of Student object.

I wanted to make these algorithm generic , so that we can perform those operations on others Object like Student.

I see in my legacy code base, redundant code can be replaced by Generic-Based Code.

What is best way / good practice for converting Non-Generic code to make it Generic ?

¿Fue útil?

Solución

In Java Tutorials there are examples how to convert legacy code to use generics:

http://docs.oracle.com/javase/tutorial/extra/generics/convert.html

Java Generics FAQ also has the subsection about dealing with legacy code:

http://www.angelikalanger.com/GenericsFAQ/FAQSections/ProgrammingIdioms.html#Coping%20With%20Legacy

Without examples it's hard to give you something more specific.

Otros consejos

Your question is a unclear. If you meant generics as in Set<Object>, then if you have a certain operation that can be done on a Set<Object> you can use something like:

public <T> void doSomethingOnSet(Set<T> set) {
    for (T item : set) {
       //do something fancy: let's print the first item:
       System.out.println(item);
       return;
    }
}

Your method can now be called on a set of any type of objects, not just students.

Your question is very generic :). One very simple though crude example of making your code generic is

public void updateStudent(Student student)
{}

to

public void updateObject(Object genericObject)
   {

    if( genericObject instanceof Student) 
        // do student specific operation


    if( genericObject instanceof School) 
        // do schoolspecific operation

  }

    }

It is just an example though it depends on your actual requirement what you want to achieve.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top