tags:

views:

68

answers:

3

Hey There Stackoverflow,

I was wondering why there are so few examples and reading material on how-to program to modules.

Pretty much every large project you start to get lost there in the middle of so much code, even if its on a separate folder. (Well, I do)

I would like to understand and maybe see a quick example or link to one, of a design thats oriented to modules.

Module, its a stand-alone part of a system. Which you can always attach more modules to the main module.

fooApp-Core.jar(.dll) fooApp-BI fooApp-Reports

Core, would have the basic domain model, with its required lib, ui, controlers and views.

But how would Core know if BI is available to be used? and them make the necessary calls ?

Did I made myself clear?

Please if you could, i would love an example.

+1  A: 

Distinguish between the actual pieces of code (classes) and the modules in which they are delivered. In a simple case you launch an application and just generally say "you'll find the classes you need in these places" the places being a list of modules. We use PATHs (for DLLs) and/or CLASSPATHs (for Java classes). The application doesn't usually need to know which DLL or JAR a class came from. [What about the possibility that more than one module contains a copy, perhaps with different versions, of a class? That's when classloading gets to be so much fun, but taht's a whole other story.]

So how does a general framework decide which classes it needs? Lots of possible ways, but conceptually by some kind of configuration.

Have a look at the ideas of Inversion of Control and Dependency injection, maybe start here

I'll show how your example might be changed to do the kind of thing you're asking about. I don't want to use the word "module" here it has too many possible meanings. Instead I'll use "plugin" - one or more pieces of code are made available, our view wants to use all that are provided.:

Controller {

   // the list of what you have been calling modules
   List<IPlugins>  myPlugins;  

   initialise() {
        // somehow we initialise that list of plugins
        // imagine we read a config file, or some such
        // If you read about Dependency Injection you'll see 
        // how we might do that
   }

   index(){

     viewModel.setUser("John");
     viewModel.setCategory("Programer");

     showView(viewModel);
 }

}

View {

   showView(viewModel) {
      System.out.println(viewModel.getUser().toString());
      System.out.println(viewModel.getCategory().toString());

      forEach( onePlugin : viewModel.getPlugins() ) {
          System.out.println( onePlugin.getDisplayString() );
      }
    }
 }

This works if we can assume that all plugins conform to some agreed interface, here we expect to be able to call getDisplayString() for every plugin.

Typically the kind of modularity you are talking about depends upon some degree of control of what is getting plugged into the framework. It can get quite sophisticated. Languages such as Java allow you to get an Object and "reflect" to find all methods whose names look like "getXXXXX()". So the view could be given any old object, and simply print out the result of (say) getName(), getPhoneNumber() getPostCode() ...

The key here is that we can use some agreed pattern to determine which methids to call. In that case the View can be very flexible.

djna
A: 

Lets take this as example.

fooApp-Core.jar

Controller {

  index(){

    viewModel.setUser("John");
    viewModel.setCategory("Programer");

    if(userLocatorModuleExists())
       viewModel.setLocation(locatorModule.locate(viewModel.getUser()));

    showView(viewModel);
  }

}

View {

  System.out.println(viewModel.getUser().toString());
  System.out.println(viewModel.getCategory().toString());

    if(userLocatorModuleExists())
      System.out.println(viewModel.getLocation().toString());
}

is this how it would work?

This can work, but it not very flexible.One problem is the View and the Controlled each needs specific code for every different "module" that exists, and both must be changed if a new one is created.Also, this is not actually to do with modules. This code can work in exactly the same with if all the classes are in the same module.
djna
A: 

Wow! :) Thx alot Djna, awesome answer.