Catalin H
Catalin H

Reputation: 215

How to structure an app with many activities?

I am making an application presenting a showroom and at this points I have created way too much classes.

The main view is a GridView containing all the series of cars.(Each GridView Item opens a new class, so there are 9 classes with very similar code)

How can I structure it?

Upvotes: 0

Views: 445

Answers (2)

Neil Townsend
Neil Townsend

Reputation: 6084

To put a bit more flesh on @g00dy, start by creating a class

class BMW {
    // Reference codes for every series
    public final static int SERIES_1 = 0;
    public final static int SERIES_2 = 1;
    // etc
    public final static int NUMBER_SERIES = 9;


    // All the code needed for every car

    // eg.
    public String giveManufacturuer() {
        return "BMW"; // But see @g00dy - use string resources
    }

    public String giveSeries() {
        return XXXXX;  // Depends on which approach you choose, see below
    }

    public String giveModelName() {
        return XXXXX;  // Depends on which approach you choose, see below 
    }
}

You can either load all the variations into this class (add in references codes for every car and set up some tables to make indexing easy).

Or you could extend the class using inheritance for each class:

class Series1 extends BMW {
    @Override
    public String giveSeries {
        return "Series 1";
    }
}

class Series1M3Door extends Series1 {
    @Override
    public String giveModelName {
        return "3 Door";
    }
}

When you then instantiate the final class it will have all three functions working correctly.

This approach is neat, but will still give you a lot of classes. I suspect that for what you are doing, some well thought out information tables (accessed by series and model code) may work better inside a hidden class.

A different, perhaps better approach, might be to structure the code using the information that you are returning as the core classes.

Upvotes: 2

g00dy
g00dy

Reputation: 6778

I do not actually have the time to write all this down, mean a unifying class, but here's hint for you. Use a flag, which will indicate the model of the car (Z4,M6 for example), then use it inside the class to determine the tree on which the code should run. Replace the hardcoded values with string resources (just do it, no other remarks are necessary). When instantiating the class and using it's functions, take into account the flag and put it inside an if() condition or inside a switch. If some models require more code than the others, you can always encapsulate it in the part of the code which is responsible for the model. But avoid nesting too much ifs, because it will get messy, like having 100 classes defined which do 99% the same thing as the others. Always try to re-use your code as much as possible. It will reduce the writing (copy/pasting) repetitive stuff, also the size of the application, the memory it will need etc. Conclusion: try combining the common parts of the classes into one class ( to RULE THEM ALL :-) ) and use flags, to let the program knwo what to do there.

Upvotes: 1

Related Questions