开发者

Android Object Models

I've worked in software development for a number of years (10+) and am just starting to have a proper look at Android. From my current understanding, a GUI is built up of Activities (replacing Forms, etc) and each of those is treated almost like its own mini program since passing references to data is not the simplest approach, and those that are available aren't particularly nice.

Anyway, my current favoured approach to GUI development is to create an object model that contains all of the functions I want to provide and then I can build the GUI (or console, or server access, or whatever) to talk to that model via Interfaces. Individual forms may be passed cut down interfaces, and only partial parts of the model, that are used for that particular form. For me, this keeps the front end separate from the functionality and also abstracts away the implementation of the Object Model from the GUI. One advantage for me is that I can write a library to do something then stick on a java GUI, an android GUI, a console, whatever. In fact, I sometimes develop the functionality prior to developing any GUI.

Now, in Android this appears to be a trickier approach to take. I think I could achieve the same by serialising the model, and parts of it, and passing these around as strings but I'm not sure this is practical. I also can see that this wouldn't work if part of the model needed to callback to another part 开发者_开发问答of the model that won't exist because it wasn't in the serialised section. ie, I have an object model that holds a list of data objects. I want to pass a data object to another Activity and if it gets edited at all, call back to the object model to enable it to do something else.

Am I able to write something to reuse my object models or does this approach just not work well with Android? If it does work, how? If not, what suitable alternatives can I use?


You are attempting to model Android development with desktop GUI or console mode development.

Rather, consider Web development.

Unless you've hopped on the node.js bandwagon, your Web app is not written in Javascript. However, a bunch of your GUI functionality will be implemented in Javascript (e.g., client-side validation, enforcing required fields before enabling the submit button). While you may have a rich object model for handling the bulk of your business logic, that will be on the server, in Java, Ruby, PHP, Perl, Python, or whatever. Web pages do not directly interact with that rich object model. And, each Web page stands alone, passing data to other pages only by relatively limited data in key-value pairs (GET parameters).

Android is, architecture-wise, a bit of a mash-up of desktop and Web development patterns. Activities are standalone entities, loosely coupled, as are Web pages. You don't pass model objects between activities, just as you don't pass model objects between Web pages.

This does not mean that you cannot have a rich object model. However, just as Web pages are going back to a central server to manipulate the Web app's rich object model, activities will need to reach back to a central store to manipulate your rich object model. And, just as Web pages tend to pass around identifiers of objects, your activities can pass around identifiers of objects, so each activity can then access the proper object from the central store.

Whether that central store is mediated by a Service or via some singleton is up to you and would depend some on what that rich object model was really supposed to represent.


Matt... I have no idea why your question got me to thinking. So here is some nutty code:

Given an interface:

public interface ISayHello {
    public void SayHello();
}

A concrete class SayHello

public class SayHello implements ISayHello, Serializable {
    public void SayHello() {
        Log.d(Main.TAG,"hello");
    }
}

One can pass the fully qualified name of the interface in an intent as:

                Bundle b = new Bundle();
                SayHello say= new SayHello();
                b.putSerializable("qprinstitute.crisis.ISayHello", say);
                intent.putExtras(b);
                startActivity(intent);

And cast the instance of the concrete class to the type (the interface) in the receiving activity as:

Intent i= getIntent();
if (i != null) {inBundle= i.getExtras();}
if (inBundle != null) {
    ISayHello say= (ISayHello)inBundle.getSerializable("qprinstitute.crisis.ISayHello");
    say.SayHello();
}

Which logs "hello". Cool!

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜