0 Replies - 613 Views - Last Post: 30 May 2017 - 07:06 AM

#1 bobj181   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 17-October 10

Thought Experiment in Code Reusability

Posted 30 May 2017 - 07:06 AM

I am trying to develop a code base to make Android development go a little faster. I am looking at utilizing the Model View Controller (MVC) concept, and maybe some concepts of other design patterns, but I am looking mainly at MVC.

My thought is a flow like this: View <--> Controller <--> Model
\-> Data Service Module

I have this concept almost working. I am just hitting one little snag, how to generically work with the models.

All my controllers extend a class called BaseController, which takes a single Generic Type.

Based upon what is passed as the Generic Type, the Base Controller is set to that model. From the model specific controller, we setup the the Data Services Module (an API Calling class) to point to the API specific to that model. The Base Controller then process the JSON Object/Array that is returned.

For now, my thought is to take a play from Dependency Injection and have each model implement a static method that basically returns an instantiation of that model class.

Then within the Base Controller, I have a logic statement as such:

        if (model instanceof PersonModel) { return PersonModel.Instantiate(o); }
        else if (model instanceof ContactModel) { return ContactModel.Instantiate(o); }
        else if (model instanceof ContactTypeModel) { return ContactTypeModel.Instantiate(o); }
        else if (model instanceof JobModel) { return JobModel.Instantiate(o); }
        else { throw new IllegalStateException("The Model is defined as something other than predefined!  The Model is defined as " + model.getClass().getName() + "."); }

This one piece is keeping me from true reusability, it defeats the purpose when you have to touch a Base Class each time you add or detract a model.

I have looked at both Interfaces and Abstract Classes, but they don't play well with static methods (for the most part). Yes, Java 8 loosens this restrictions, but I am using Android Studio, and the current live version doesn't support that natively.

So before I go shacking things up to get Android Studio to play nice with Java 8, I thought I would post this thought experiment and see what the Dream In Code community has to say.

Thank you in advance for your thoughts,

Is This A Good Question/Topic? 0
  • +

Page 1 of 1