KMPのレイヤードアーキテクチャ
データ、ドメイン、プレゼンテーションを明確に分離します
「KMPのレイヤードアーキテクチャ」はCoddyKit上の無料Kotlin Multiplatform Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはKotlin Multiplatform Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
One Codebase, Many Apps
As your KMP app grows, shared code can turn into a tangle. A clear layered architecture keeps every piece in its lane so both apps stay easy to change.
Three Classic Layers
Most KMP apps settle on three layers: data, domain and presentation. Each has one job, and dependencies flow in a single, predictable direction.
The Data Layer
The data layer talks to the outside world: Ktor for HTTP, SQLDelight for storage. It fetches raw bytes and hands clean data upward, hiding the details.
The Domain Layer
The domain layer holds your business rules and models. It knows nothing about HTTP or SQL, so it stays pure, portable and trivially testable.
The Presentation Layer
The presentation layer prepares state for the screen. Shared ViewModels live here, turning domain data into the StateFlow each UI collects.
Dependencies Point Inward
The golden rule: outer layers depend on inner ones, never the reverse. Presentation knows domain; domain knows nothing about either neighbour.
Talk Through Interfaces
Layers connect through interfaces, not concrete classes. Domain declares what it needs; data provides the implementation, so you can swap pieces freely.
interface UserRepository {
suspend fun load(id: String): User
}Models per Layer
Each layer can own its shapes: a network DTO in data, a clean model in domain. Mapping between them keeps API quirks from leaking into your rules.
Why Bother Separating
Separation pays off when requirements shift. Swap a REST API for GraphQL and only the data layer changes; domain and presentation never notice.
It All Lives Shared
Data, domain and presentation can all sit in commonMain. Both Android and iOS reuse the entire stack and only supply the final native screen.
Start Simple
You do not need ten layers on day one. Begin with these three, keep the dependency arrows clean, and let the structure grow only when it earns its keep. 🙂
Quick Check
Which way should dependencies flow between layers?
Recap
You split shared code into data, domain and presentation, pointed dependencies inward, and connected layers through interfaces. Clean lanes, easy changes. 🎉
よくある質問
「KMPのレイヤードアーキテクチャ」レッスンは無料ですか?
はい。「KMPのレイヤードアーキテクチャ」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Kotlin Multiplatform Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
「KMPのレイヤードアーキテクチャ」で何を学びますか?
データ、ドメイン、プレゼンテーションを明確に分離します ブラウザで直接実行するハンズオンコードでKotlin Multiplatform Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Kotlin Multiplatform Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのKotlin Multiplatform Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。
「KMPのレイヤードアーキテクチャ」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このKotlin Multiplatform Academyレッスンでコードを書いて実行できますか?
はい。すべてのKotlin Multiplatform Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- KMPのレイヤードアーキテクチャ
- ユースケースとドメイン境界
- 共有コードをモジュールに分割する
- ネイティブに残すものを決める