Mobile Application DevelopmentUnit 210 min read
Mobile App Architecture, Frameworks & Design Patterns
Unit 2 of Mobile Application Development explores the layered architecture of mobile systems (OS, middleware, apps), compares native vs. cross-platform frameworks (Flutter, React Native, Xamarin), and teaches design patterns (MVC, MVVM) with real-world app examples from Nepal (eSewa, Pathao) and global tech (Google Map
TAKEAWAYS:
- Mobile apps follow a 4-layer architecture (hardware → OS → middleware → apps), where the OS (Android/iOS) provides APIs for sensors, networking, and UI components.
- Cross-platform frameworks (Flutter, React Native) share 60–90% of code but sacrifice some native performance, while native frameworks (Swift/Kotlin) offer full device access at the cost of higher development effort.
- Design patterns like MVC and MVVM separate concerns: MVC splits logic into Model-View-Controller, while MVVM uses Data Binding to update UI automatically (critical for real-time apps like live traffic updates).
- Middleware (Firebase, Parse) handles cloud sync, authentication, and push notifications—used by eSewa for secure payments and Pathao for driver tracking.
- Performance trade-offs: Native apps load faster (e.g., WhatsApp’s 100ms response time) but require separate codebases; cross-platform apps reduce costs but may lag (e.g., Daraz’s hybrid app vs. its native iOS version).
- Exam focus: Compare frameworks in a table, trace data flow in MVC/MVVM diagrams, and explain how middleware solves offline-first challenges (e.g., Ncell’s app caching messages when no signal).
Mobile System Architecture: Layers and Components
Mobile apps don’t run in isolation—they interact with hardware, operating systems, middleware, and networks. The 4-layer architecture defines how these components communicate:
1. Hardware Layer
- Components: CPU, GPU, sensors (GPS, accelerometer), battery, storage.
- Role: Provides raw input/output (e.g., a Pathao driver’s GPS location or a user’s fingerprint for eSewa login).
- Key APIs: Android’s
SensorManager, iOS’sCoreLocationframework. - Example: When you open the NTC Electricity Bill app, it reads your SIM card’s IMEI to fetch your consumer number—this happens at the hardware layer via the OS’s telephony API.
2. Operating System Layer
- Android (Linux kernel): Uses Java/Kotlin (via Android Runtime) and NDK for C/C++.
- iOS (Darwin kernel): Uses Swift/Objective-C and Metal for GPU acceleration.
- Key Services:
- Activity Manager: Handles app lifecycle (e.g., pausing WhatsApp when you switch to a call).
- Content Providers: Secure data sharing (e.g., Contacts app accessed by Pathao for driver profiles).
- Broadcast Receiver: System-wide events (e.g., low battery alerts).
flowchart TD
A["Hardware\n(CPU, Sensors)"] --> B["OS Kernel\n(Android: Linux, iOS: Darwin)"]
B --> C["Android Runtime\n(ART/Dalvik)"]
B --> D["iOS Runtime\n(Swift/Obj-C)"]
C --> E["Java/Kotlin APIs"]
D --> F["Swift/Obj-C APIs"]
E & F --> G["Middleware\n(Firebase, Google Play Services)"]
G --> H["Mobile Apps\n(eSewa, Pathao)"]3. Middleware Layer
Middleware acts as a bridge between apps and cloud services. Nepalese apps rely heavily on:
- Firebase (used by eSewa for real-time transaction status, Khalti for payment gateways).
- Google Play Services (used by Pathao for maps and location tracking).
- Parse Server (open-source alternative for offline data sync).
Example: When you book a ride on Pathao, the app:
- Sends your location to Firebase via Google Maps API.
- Firebase matches you with the nearest driver (using geohashing).
- The driver’s app receives a push notification (via Firebase Cloud Messaging).
sequenceDiagram
participant User
participant PathaoApp
participant Firebase
participant DriverApp
User->>PathaoApp: Request ride (GPS on)
PathaoApp->>Firebase: Send location (geohash)
Firebase->>Firebase: Query nearest driver
Firebase->>DriverApp: Push notification (new ride)
DriverApp->>User: Accept/reject4. Application Layer
This is where your code runs. Apps are built using:
- Native frameworks: Swift (iOS), Kotlin (Android).
- Cross-platform frameworks: Flutter (Dart), React Native (JavaScript), Xamarin (C#).
Native vs. Cross-Platform Frameworks: Comparison
| Feature | Native (Swift/Kotlin) | Cross-Platform (Flutter/React Native) |
|---|---|---|
| Performance | Near-native (60 FPS) | 50–60 FPS (Flutter), 40–50 FPS (React Native) |
| Development Time | High (separate codebases) | Low (shared code, ~70% reuse) |
| Access to Hardware | Full (e.g., ARCore, CoreML) | Limited (plugins needed) |
| UI/UX | Platform-specific (e.g., Cupertino for iOS) | Custom widgets (Flutter) or native components (React Native) |
| Example Apps | WhatsApp, Google Maps | Alibaba (React Native), BMW (Flutter) |
Worked Example: eSewa’s Payment Flow
- Native Code (Kotlin/Swift):
- Uses Android’s
PaymentRequestAPI or iOS’sPassKitfor secure transactions. - Direct access to the device’s biometric scanner (fingerprint/Face ID).
- Uses Android’s
- Cross-Platform Limitation:
- If eSewa used Flutter, it would need a plugin like
flutter_secure_storagefor OTP handling, adding latency.
- If eSewa used Flutter, it would need a plugin like
flowchart LR
A["User Opens eSewa\n(Kotlin/Swift)"] --> B["Checks Biometric\n(Hardware Layer)"]
B --> C["Validates OTP\n(Firebase Auth)"]
C --> D["Debits Account\n(Nepal Rastra Bank API)"]
D --> E["Updates UI\n(Native Widgets)"]Design Patterns for Mobile Apps
Design patterns organize code to handle common problems like state management, networking, and UI updates.
1. Model-View-Controller (MVC)
- Structure:
- Model: Data (e.g., user profile in eSewa).
- View: UI (e.g., payment screen).
- Controller: Logic (e.g., validate OTP before payment).
- Data Flow: View → Controller → Model → View (updates UI).
- Example: Khalti’s Payment Screen
- View: Dropdown for bank selection.
- Controller: Validates bank code before proceeding.
- Model: Fetches bank details from API.
classDiagram
class Model {
+fetchUserData()
+updateBalance()
}
class View {
+renderPaymentScreen()
+showError(message)
}
class Controller {
+validateOTP()
+processPayment()
}
Model --> Controller : updates
Controller --> View : notifies
View --> Controller : user input2. Model-View-ViewModel (MVVM)
- Key Idea: Data Binding (ViewModel → View) reduces boilerplate.
- Used in: Android (Jetpack), iOS (SwiftUI).
- Example: Pathao’s Live Location Tracking
- ViewModel: Polls driver location every 2 seconds.
- View: Automatically updates the map (no manual
setStatecalls).
flowchart TD
A["Driver Moves\n(GPS Update)"] --> B["ViewModel\n(Polling Loop)"]
B --> C["Observable Data\n(RxJava/Kotlin Flow)"]
C --> D["View\n(Map Updates Automatically)"]Comparison Table:
| Pattern | Pros | Cons | Best For |
|---|---|---|---|
| MVC | Simple, unidirectional flow | Tight coupling | Small apps, CRUD operations |
| MVVM | Decoupled, testable | Steeper learning curve | Complex apps (e.g., live tracking) |
| VIPER | Scalable, modular | Overkill for small apps | Enterprise apps (e.g., banks) |
In the Real World
eSewa (Nepal):
- Architecture: Uses native Kotlin/Swift for performance-critical tasks (biometric auth, payment processing) and Firebase for real-time transaction updates.
- Design Pattern: MVC for the payment flow (View = UI buttons, Controller = OTP validation, Model = user balance).
Pathao (Nepal/Global):
- Middleware: Relies on Google Maps API (via Firebase) to match riders/drivers in real-time.
- Cross-Platform: Uses React Native for iOS/Android, but critical features (e.g., in-app calls) are native modules.
Google Maps (Global):
- Native Performance: Uses Swift/Kotlin for smooth animations (e.g., 3D buildings).
- Middleware: Google Play Services handles location updates even when the app is in the background.
Wireless Connectivity in Mobile Apps
Mobile apps use HTTP/HTTPS, WebSockets, and MQTT for real-time data. Nepalese apps often face high latency (e.g., Ncell’s slow API responses), so optimizations like caching (e.g., Daraz’s offline mode) are critical.
Example: NTC Electricity Bill App
- Fetches consumer data via REST API (HTTP).
- Uses local SQLite database to cache bills for offline viewing.
- Syncs changes when connectivity resumes.
sequenceDiagram
participant App
participant NTCServer
participant LocalDB
App->>NTCServer: GET /bills/{consumer_id}
NTCServer-->>App: JSON response
App->>LocalDB: Cache response
loop Offline Use
App->>LocalDB: GET cached_bill
end
App->>NTCServer: POST /sync (when online)Exam Tip
- Diagrams are mandatory:
- Draw the 4-layer architecture and label each layer’s role.
- Trace data flow in MVC/MVVM (e.g., "How does Pathao update the map when the driver moves?").
- Compare frameworks:
- Memorize the trade-offs table (native vs. cross-platform) and give one Nepalese example (e.g., "eSewa uses native code for security").
- Real-world scenarios:
- Explain how Firebase solves offline-first challenges (e.g., Khalti’s payment queue when no internet).
- Describe one app’s architecture (e.g., "Daraz’s hybrid app uses React Native for 80% features but native modules for checkout").
- Common pitfalls:
- Don’t confuse MVC (Controller updates Model) with MVVM (ViewModel binds to View).
- Avoid vague answers like "Flutter is fast"—specify 60 FPS vs. native’s 60–120 FPS.
Practice Questions (Self-Check)
- Draw the Android architecture layers and explain how eSewa’s biometric login interacts with each layer.
- Compare MVC and MVVM using Pathao’s live tracking as an example. Which pattern would you choose and why?
- Why does Ncell’s app use local caching? What middleware could it use for push notifications?
- If you were building a Khalti-like app, would you choose Flutter or native code? Justify with performance and development time trade-offs.
Based on the TU BIT syllabus for Mobile Application Development, unit 2.
Discussion
Loading…