Go Back   Android Forums > Android Development > Application Development
Application Development Dev lounge for our application developers.

test: Reply
 
LinkBack Thread Tools
Old July 31st, 2011, 08:11 AM   #1 (permalink)
Junior Member
Thread Author (OP)
 
Join Date: Jun 2011
Posts: 32
 
Device(s):
Carrier: Not Provided

Thanks: 8
Thanked 2 Times in 2 Posts
Default What design patterns do you use?

Good day, friends.

What design patterns do you use in your applications?

We use pureMVC framework: from activity methods we send notifications, mediators handle notifications, proxies and layouts. It wasn't proved to be 100% satisfactory for me, and I'm willing to use something else.

What did you recommend?

Advertisements
QuickNick is offline  
Last edited by QuickNick; August 6th, 2011 at 07:50 AM.
Reply With Quote
sponsored links
Old July 31st, 2011, 09:05 AM   #2 (permalink)
Beware The Milky Pirate!
 
El Presidente's Avatar
 
Join Date: Jan 2011
Location: Scotland
Posts: 27,230
 
Device(s): Xperia Z1, Nexus 7 3G, HTC One X, SGS3 Mini
Carrier: EE

Thanks: 14,331
Thanked 16,332 Times in 8,739 Posts
Default

Looking at your link, I'd say this post would be more appropriate for the application development forum.
__________________
Site Rules & Guidelines / Staff List / Ask the Staff
Want to bring naughty posts to our attention? Use:
Be respectful to each other - That's what we're all about.
El Presidente is offline  
Reply With Quote
Old July 31st, 2011, 09:59 AM   #3 (permalink)
Junior Member
Thread Author (OP)
 
Join Date: Jun 2011
Posts: 32
 
Device(s):
Carrier: Not Provided

Thanks: 8
Thanked 2 Times in 2 Posts
Default

A little more details.

Suppose we have complex activity with the complex layout and async tasks.
Where do you set listeners, build dialogs, turn async tasks on, publish results from separate thread? In which class?
QuickNick is offline  
Reply With Quote
Old August 2nd, 2011, 07:21 PM   #4 (permalink)
Over Macho Grande?
 
alostpacket's Avatar
 
Join Date: Nov 2009
Location: NY
Posts: 7,920
 
Device(s): GlassXE, MotoX, N5, N4, N7'12, GNex, N1, SGT10.1, Revue, Xoom, Eris, OG Droid
Carrier: TMO

Thanks: 4,603
Thanked 3,578 Times in 1,530 Posts
Default

Good question! I think what patterns you use are up to you.

Patterns in essence, are really just logical ways to solve common problems. The pattern you use, depends on the problems you will face.

Personally I have found the Factory pattern very helpful.

I have also found it extremeley helpful to use POJOs (Plain Old Java Objects) to encapsulate data. As opposed to Java beans with lots of getters or setters. get/set is verbose and harms readability (IMHO). You also take a very small performance hit accessing a method over a property.


Anyways, android already is "kinda" MVC-based so I think the PureMVC framework might be excessive.

(Adapters = Model, Layouts = View, Activity = Controller)

The biggest problem I have found is that you need to avoid the "God anti-pattern" with Activities. They tend to fill up with code quickly. Personally I dont use ASyncTasks because I find Runnables and threads to be easier and more powerful and flexible.

Threads can also be separated out from an Activity to help reduce the clutter. I highly reccommend mastering the use of passing around Handlers and Context (Application - Context, be careful with Activity Contexts, you can cause memory leaks). But once you know how to use handlers to pass messages your Threads and Activities and Adapters and Models will be all working harmoniously just like the messaging systems of PureMVC (think of Handlers as Mediators)


Iterator stuff is of course always useful in Java. As are related Queue/Memento and other patterns.


hth
alostpacket is offline  
Last edited by alostpacket; August 2nd, 2011 at 07:26 PM. Reason: overuse of the typo pattern
Reply With Quote
The Following User Says Thank You to alostpacket For This Useful Post:
QuickNick (August 5th, 2011)
Old August 2nd, 2011, 07:28 PM   #5 (permalink)
Over Macho Grande?
 
alostpacket's Avatar
 
Join Date: Nov 2009
Location: NY
Posts: 7,920
 
Device(s): GlassXE, MotoX, N5, N4, N7'12, GNex, N1, SGT10.1, Revue, Xoom, Eris, OG Droid
Carrier: TMO

Thanks: 4,603
Thanked 3,578 Times in 1,530 Posts
Default

Also I have heard singleton recommended for data IO. But static classes could do this fine as well.
alostpacket is offline  
Reply With Quote
Old August 5th, 2011, 08:23 AM   #6 (permalink)
Junior Member
Thread Author (OP)
 
Join Date: Jun 2011
Posts: 32
 
Device(s):
Carrier: Not Provided

Thanks: 8
Thanked 2 Times in 2 Posts
Default

Thanks, it was very interesting and informative answer

Current code of our application looks like spaghetti (previous developer doesn't like design patterns but likes God objects) even with pureMVC, so I decided to fix it - that's why I asked about patterns.

I found out that my solution is called MVP (Model-view-presenter - Wikipedia, the free encyclopedia): different managers, services are model (thanks to you I've found out that adapters should be in model too), activity is presenter, layouts are view. I save and load preferences in activity, set listeners in layouts, and make internal calculation/playing sound/etc in model.

There is one interesting task. I have 3 different activities, all of them should use one model (for sound analysis). This model sends results in short periods of time (every 200-500 miliseconds). And result should be sent only to visible activity. What structure should I use - is it service? Or maybe something else?
QuickNick is offline  
Reply With Quote
Old August 5th, 2011, 06:33 PM   #7 (permalink)
Over Macho Grande?
 
alostpacket's Avatar
 
Join Date: Nov 2009
Location: NY
Posts: 7,920
 
Device(s): GlassXE, MotoX, N5, N4, N7'12, GNex, N1, SGT10.1, Revue, Xoom, Eris, OG Droid
Carrier: TMO

Thanks: 4,603
Thanked 3,578 Times in 1,530 Posts
Default

Service can work well for that yeah. Just need to be careful about how the service is started or stopped. Check the API docs, there is a lot of info on the Service class.

You may want to set a custom BroadcastReceiver that will start the Service if it doesnt exist.

This BroadcastReceiver would also allow an activity to register and unregister as an observer. Then you activity could register itself in onResume(), and unregister in onPause()

And I guess when I think of MVC I really mean MVP -- I never had the controller handing user input, mine was always a middle man. And i personally made the model mostly a dumb data store with the adapter "protecting" it.

(I think Android does this too, and it's sort of why Models are called Adapters in Android).

"Business" logic (or just "logic") I try to put in commands (command pattern) -- Something like a runnable would work fine for this purpose. Or even a static class/factory. Basically anywhere I could seperate out the concern. This would also lead to better (functional) cohesion.

This also allows for using delegates and responders to handle server interaction or IO.

Then, I would have the controller handle events sent from the views and route them to the proper command. Almost like a Service Locator pattern.

I'm trying to use the command pattern more, but it takes a lot of time. It is GREAT for de-coupling and modularity though.

I'm also slowly trying to move toward more dependancy injection for the same reasons, but I dont think my Java is that good yet. (I learned coding in ActionScript hehe )


Basically, while not the strict dictionary definition of MVC, I always thought of it like this in my mind:

Model - A model or structure to represent how the data should exist
View - what the user views aka the UI
Controller - the class that controlls the interaction between the model and view

This made it easy to remember and implement for me, although I know this is a bit off from what someone writing a book on MVC would say they are. But mostly MVC and Java and small talk was written durring the time of client-server interaction.

Views were web pages, models were SQL databases with prepared statments for transaction processing, and controllers were middleware languages like php and Java.

This just doesnt exist in the same way in current application programming. The frontend itself can have very complex MVC type architecture, without ever needing a server.



Long story short though, I love thinking about this stuff, but sometimes you gotta just use any pattern that seems obvious and get coding
alostpacket is offline  
Reply With Quote
The Following User Says Thank You to alostpacket For This Useful Post:
QuickNick (August 6th, 2011)
Old September 18th, 2011, 10:55 AM   #8 (permalink)
Junior Member
Thread Author (OP)
 
Join Date: Jun 2011
Posts: 32
 
Device(s):
Carrier: Not Provided

Thanks: 8
Thanked 2 Times in 2 Posts
Default

Now, I'd like to ask about place of Dialogs and BroadcastReceivers in the MVP/MVC architecture.

As I understand, BroadcastReceiver is clearly controller. ContentProvider is model.
And what about Dialogs? Are they controllers or views?

Another question. What way is best for initialization of AlertDialogs?
I've inited AlertDialog by AlertDialog.Builder right in the Activity method onCreateDialog(int id), but activity becomes too big very soon. It's look like I should try this way: create new class MyAlertDialog which extends AlertDialog, init it and so on.

What will you suggest, friends?
QuickNick is offline  
Reply With Quote
Old September 19th, 2011, 09:58 AM   #9 (permalink)
Senior Member
 
jonbonazza's Avatar
 
Join Date: Jul 2010
Gender: Male
Posts: 1,935
 
Device(s): Nexus 4, Samsung Galaxy S II, HTC Evo 4G, Amazon Kindle Fire
Carrier: ATT

Thanks: 235
Thanked 482 Times in 294 Posts
Default

For applications, I use the obvious OOP paradigm, as well as MVC concepts.

For games, however, I usually use a lot of different concepts, including OOP, COP (CBA), Singleton (As mentioned above), and more.

With that said, worrying too much about design patterns can be a bad thing. In the end, you want to do what you need to do and do it in the most efficient way. This is an especially common train of thought for game programming and other performance demanding applications. If the most efficient way of doing what needs to be done ends up straying from your normal design practices, then you shouldn't be hesitant to do it in that most efficient manner.
jonbonazza is offline  
Reply With Quote
Old November 25th, 2012, 02:43 AM   #10 (permalink)
New Member
 
Join Date: Nov 2012
Posts: 3
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by QuickNick View Post
Now, I'd like to ask about place of Dialogs and BroadcastReceivers in the MVP/MVC architecture.

As I understand, BroadcastReceiver is clearly controller. ContentProvider is model.
And what about Dialogs? Are they controllers or views?

Another question. What way is best for initialization of AlertDialogs?
I've inited AlertDialog by AlertDialog.Builder right in the Activity method onCreateDialog(int id), but activity becomes too big very soon. It's look like I should try this way: create new class MyAlertDialog which extends AlertDialog, init it and so on.

What will you suggest, friends?
@QuickNick : Did you find the solution to above ?
raj34 is offline  
Reply With Quote
sponsored links
Reply


Go Back   Android Forums > Android Development > Application Development
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 11:56 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.