Search code examples
class-design

Java class design


So I have this web application I am making, as of now there is a Database class that handles database functionality queryMovies, updateMovies, and getConnection. Now I want to parse an array of movie titles retrieved from a file directory into addMovies. What would be the most efficient way to add this functionality?

Should I add it in the Database constructor and use an array member variable? another class? in the servlet? Come to think of it I may want to add some functions to file string name retrieval for more modularity... maybe another class would be best. This functionality would always be used for database queries hmmm. hmmm. Some help would be great.


Solution

  • The below can get you started. Remember to code against interfaces so that it is easy to unit test. keep the abstractions focused (Single Responsibility Principle)

    public interface FileParser
    {
        List<Movie> parse(String filePath);
    }
    
    public class Movie
    {
        ...
    }
    
    public class FileHandler
    {
        MovieRepository repo=new MovieRepository();
        void storeMovies(List<Movie> movies);
    }
    
    pubic class MovieRepository
    {
        //handles CRUD by talking to the DB
    }