Couple of Design pattern i think every android developer should know and understand. I will not try to bore with how to implement each design pattern rather i will try to explain why and where they are necessary on Android platform.
Model-View-Controller
MVC is An architectural design patten help us architect our java classes and resources. MVC make more sense for bigger project.
For typical application we can architect following way,
View: My XML layouts or resources. My Activity class, Menu and Action bar code.
Controllar: Classes to process my business requirement. Helper or Utility classes can also go this section.
Model: Classes for storage, caching.
We can even have nested MVC. Think of an app where we are storing user email to a content provider. And, the content provider is a custom content provider we developed for our app. In that case i can sub divide my model code again in MVC style.
View: Data Access Object (DAO)for inserting/retrieving or deleting data. It is also contain every thing needed to initialize my content provider code, like retrieving content resolver or searching through a content provider. This is more close to my business logic, like getMeXData()
Controller: Code for content provider, like setting up content URI, OR, insert delete.
Model: code to create database or table. Like, SQLiteOpenHelper or BaseColumns code.
Observer/Observable
Observer observe changes in a another object. And in most cases it is an Observable.
Observable are the object which has been observed by an observer. This patter comes very handy in couple of cases:
Scenario 1, Scanning Access point
Observable: Code for AP scan request. Do some Additional Processing. For example may be we just interested about a specific AP or Signal Strength.
Observer: depend on what we are planning to do once we have to data? We can store the data or draw UI.
Scenario 2, Location data
Observable: Code for location request, like setting up criteria, or checking location accuracy.
Observer: Update UI or do some thing else depend on business logic.
Pipeline
This another design pattern we need when we try to update UI from another thread. Yes i am talking about Handler.
This is very good links explain how pipeline works. http://www.cise.ufl.edu/research/ParallelPatterns/PatternLanguage/AlgorithmStructure/Pipeline.htm
Their are lots of Good example how to use Handler on your Android application so i will not further details on this. But yes we should know what it is and what it does and how it works.
Factory Pattern
Factory is a creational design pattern and help us issue with creating an object. We all probably know how Bitmap factory works on Android.
Singleton
Singleton prevent us to have multiple instance of an object.
Then where we need it? Think of the situation where we are creating a cache for storing data. Every-time we update, edit data we do not want multiple instance of data set in that case we will end up with inconsistent data, and changes in one cache object will effect not effect another instance. So, singleton can help you to return current instance, when a instance exist or create a new one if no instance exist.
Android Application Class is also a good example of Singleton pattern.
Singleton
Singleton prevent us to have multiple instance of an object.
Then where we need it? Think of the situation where we are creating a cache for storing data. Every-time we update, edit data we do not want multiple instance of data set in that case we will end up with inconsistent data, and changes in one cache object will effect not effect another instance. So, singleton can help you to return current instance, when a instance exist or create a new one if no instance exist.
Android Application Class is also a good example of Singleton pattern.
Adapter Pattern
Adapter pattern help us to translate one interface to a compatible one. Every time we work with a list view we use adapter pattern. Probably it is one of very common design pattern we spends time without probably understanding it.
Android has bunch of Adapter class.
Adapter pattern help us to translate one interface to a compatible one. Every time we work with a list view we use adapter pattern. Probably it is one of very common design pattern we spends time without probably understanding it.
Android has bunch of Adapter class.
- List Adapter
- Cursor Adapter
- Array Adapter
- Base Adapter (top level class)
No comments:
Post a Comment