How tbiyiktas/android-clean-network-layer works
Project Type & Purpose
• This is an Android application demonstrating a “Clean Network Layer” library.
• The app module (app/src/...) contains a minimal UI (MainActivity, Lookup, MyApplication), domain models (Post), and integrates the lib network layer.
• The lib package is a standalone, pure-HttpURLConnection library providing a modular, testable network layer (producer–consumer queue, retry/backoff, cancellation, gzip, typed responses).
File Structure & Key Modules
Top-level “app” module:
• app/src/main/java/com/example/...
– MyApplication (initializes NetworkManager)
– MainActivity / Lookup (UI entry points invoking network calls)
– Domain model (Post)
• app/src/main/java/lib/...
– api: TodoApi (defines endpoints)
– model: Todo
– net: core network classes
· NetworkManager (public façade, request queue, executor service)
· ABaseApi (abstract base for typed APIs)
· MyHttpClient / AHttpClient (optional lower-level client)
· NetworkCallback / NetResult<T> (typed response wrapper)
– net/command: HTTP-method command classes (GetCommand, PostCommand, etc.) – Command Pattern
– net/connection: factory and adapters for HttpUrlConnection / HttpsConnection – Factory Pattern
– net/parser: IResponseParser + GsonResponseParser – Strategy Pattern
– net/util: NetworkConfig, UrlBuilder, ResponseHandler, RequestCancelledException
Extract Architecture Insights from README
• Emphasizes a bounded BlockingQueue<RequestTask> + ExecutorService (producer–consumer).
Main Components & Their Interactions
a. UI Layer (Android Main Thread)
– MainActivity / Lookup call → NetworkManager.get()/post()/put()/...
– Receives back a RequestHandle and a NetworkCallback<T>
b. Network Layer
– NetworkManager
• has a bounded request queue + thread-pool executor
• holds a IHttpConnectionFactory for creating connections
• posts responses back via ResponseHandler (Android Handler on main thread)
– RequestTask (internal)
• wraps a specific ACommand (e.g., GetCommand) and enqueues
– ACommand + Subclasses (GetCommand, PostCommand, …)
• build URL via UrlBuilder + NetworkConfig settings
• obtain IHttpConnection from factory → open connection → write body/headers → read response stream
– IHttpConnectionFactory / HttpUrlConnectionFactory → HttpConnectionAdapter / HttpsConnectionAdapter
– IResponseParser / GsonResponseParser parse raw JSON → model objects (e.g., Todo)
– Retry & Backoff logic lives in ACommand / NetworkManager using NetworkConfig settings
– On completion, wrap result in NetResult.Success<T> or NetResult.Error<T> and dispatch via NetworkCallback<T>
c. External Service
Architectural Patterns & Design Principles
• Command Pattern: one class per HTTP method
• Factory Pattern: decouple connection creation (HTTP vs. HTTPS)
• Producer–Consumer: bounded queue + thread pool to protect against OOM
• Strategy: pluggable IResponseParser (e.g., GSON)
• Singleton: NetworkManager optionally as Application singleton
• Separation of Concerns: UI vs. Networking vs. Parsing vs. Configuration
1. UI Layer at top; show MainActivity → calls NetworkManager.
4. Off to the side, show IHttpConnectionFactory → HttpUrlConnectionFactory → adapters.
5. Beneath that, show IResponseParser → GsonResponseParser.