android object oriented programming

2019-04-26 19:30发布

问题:

I am fooling around with some basic programming in Android using Eclipse. I'm currently looking through a book and playing with some sample code that is written in the book.

I have noticed that in this particular book, all of the examples so far take pace in Main-Activity. I don't believe this to be very good Object Oriented programming practice as I am from a traditional Java background.

Is this the common practice for mobile platforms? Shouldn't classes all be contained in their own files?

回答1:

Shouldn't classes all be contained in their own files?

Not necessarily as an Android Activity is a 'special case' class. If you haven't done already, I'd recommend you read Application Fundamentals and in particular the section on 'Activities' under Application components...

An activity represents a single screen with a user interface. For example, an email application might have one activity that shows a list of new emails, another activity to compose an email, and another activity for reading emails. Although the activities work together to form a cohesive user experience in the email application, each one is independent of the others. As such, a different application can start any one of these activities (if the email application allows it). For example, a camera application can start the activity in the email application that composes new mail, in order for the user to share a picture.

Note the section of text that I've highlighted in bold. The point is that an Activity in itself is not the complete app and if allowed, any third-party app can potentially invoke an Activity in one of your apps. As such, it is common to make an Activity as self-contained as possible. One particular example is the use of something like an AsyncTask which provides methods to execute a background thread as well as manipulate the UI - nesting a private class which extends AsyncTask is quite common and simplifies code. Nesting classes which extend BroadcastReceiver is also common for the same reason.

That said, there is nothing wrong with using separate Java class files for POJO helper classes, for example, it just comes down to how complex your app is but it can mean taking special consideration of how certain Android classes work - the AsyncTask class being one in particular if defined in a separate class file, try it and you'll see what I mean. :-)



回答2:

OO is about putting functionality in classes. The way you write those classes defines if it is good OO or not (although this is debatable). Whether all these classes are in a single or a few files, or each class has its own file, is a matter of taste and is not directly an OO issue.

Since this is a book with (I think) small samples, it may be just as easy to read the way it is, than when all classes are in separate files.



回答3:

If you use proper OOP you can create Template based apps much more quickly & efficiently.

You should strive to do this for example if you have a generic database app and multiple databases can be used with minor changes.



标签: java android oop