开发者

Android Library Project - Defining Classes

Hey! (Apologies for the long post here)

I am writing a few applications which will be based off of essentially the same code.

Each application will have its differences, some will have ne开发者_如何学JAVAw activities, all will have different resources assigned.

I am attempting to use an Android Library project so that I can place all of the common code in one place, so manjor changes only have to be made once and all projects will be updated.

Now, the resources are an easy solution, as I can just define each resouce in the main application and these will override the ones in the library project as long as naming conventions stay the same.

Now, my question is how do I do the same but for classes? For example, ill have a base view as a wrapper for common code across all views. in this, i launch various activities. What if all three applications need to launch a different activity as they will contain different UI and features? How would I go about this? The library project will never know that the main project wants to use a different activity, as its already been compiled.

I have a couple of solutions I have thought up, but I want to know if anyone else has some ideas? It would be of great help for any feedback here:

  1. The Activity in the Library Project that is to be launched stores a reference to the class to be loaded, which will be defined in the main application at its launch. This was when the library project calls this activity, it can load what its been told to.

  2. I leave all functionality that will not change inside the library projects activity. I then extend this activity, and override the onCreate to do what I need here (such as loading different layouts, setting up views, etc). Now this has a drawback, as I have to define most of the entry points in my main application rather than in the library project.

I hope you follow what my issue is here. Please ask if I have explained this poorly.

Thanks,

Adam


Or more simple: Should you use inheritance or composition to achieve that kind of flexibility?

I had this kind of structure quest as well. Usually you would solve it by reusing a applicable pattern & break the necessary code parts in smaller pieces. I.e. if you know the difference are views, then extract that into some distinguished classes that will implement against an common interface and refer to the interface in your base activities.

Some links for you:

  • http://tiedyedfreaks.org/eric/CompositionVsInheritance.html
  • http://mindview.net/Books/TIPatterns/
  • http://en.wikipedia.org/wiki/Decorator_pattern - maybe applicable in your case

At least from how I understand your question, this is more of a OOP Design question.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜