SlideShare a Scribd company logo
1 of 17
Download to read offline
よい設計とは
自己紹介
●
叶 弥悟
●
Twitter 鏡華 (@mirror_kt)
●
久留米工業高等専門学校 機械工学科 2 年
●
最近は限界開発鯖 (https://approvers.dev) とい
うコミュニティで開発しています
. よい設計とは何か
●
Pull Request の Review 時に設計に
ついて指摘することが多い気がして
きた
●
じゃあいい設計とはなんだという話
をしたいと思います
単一責任の原則
●
各クラスが行う責務は 1 つに絞るべ
き、という原則
class GameManager {
fun startGame()
fun stopGame()
fun listGames(): List<Game>
fun drawText(gameId: UUID)
fun applyEffect(effectName: String)
}
いわゆる神クラス
先ほどの例は、
●
ゲームその物の管理 ( 開始・終了 )
●
ゲーム全体の管理 ( ゲームの一覧 )
●
ゲームから利用する機能 ( エフェクト )
という責務がすべて GameManager に集まってい
るのが原因です
→ 責務ごとにクラスを分けてみましょう
class Game {
fun startGame()
fun stopGame()
}
class GameRegistry {
fun listGames()
}
class Effector {
fun applyEffect(effectName: String, target:
String): String
}
責務の封印 ( カプセル化 )
●
あるクラスを使うとき、そのクラス
の内部の実装に依存するのは悪い設
計です
先ほどの GameRegistry を例に出します
GameRegistry を使うクラスは、 GameRegistry
の「ゲームの一覧を保持する」「ゲームの一覧に
ゲームを追加・削除する」という機能に依存する
→ 機能に依存するのであって実装に依存するので
はない
GameRegistry がデータベースにゲームを保存して
ようがオンメモリだろうがファイルだろうが、使
う側は関係ない状態にするのが理想です
もし、 GameRegistry を使うクラスが DB のトラ
ンザクションを気にしなきゃいけない場合
DB を使っている、という内部実装が外に漏れ出
している
これを「責務の漏洩」という。
依存方向
( 依存関係逆転の法則 )
相互依存するクラスが増えると、
機能の追加削除を行うのが難しくなる
Web バックエンドでよく使われる
3 層アーキテクチャ
●
アプリケーション層
●
ドメイン層
●
インフラ層
アプリケーション層 :
Web やモバイルといった、表示や入力に直接関わる部分
ドメイン層 :
プロジェクト固有の概念 ( ユーザーという概念やゲームという概念
など ) や、それらを利用したビジネスロジックを置く部分
インフラ層 :
データベースやファイルなどのインフラへのアクセスを提供する
3 層アーキテクチャでは
サービス層はほかの層に
依存しない
サービス層は、
●
「データを受け取って」
●
「データを処理して」
●
「データを返す」
ことに専念する
どうやってデータをユーザーから受け取るか、ど
うやって表示するかは考えない
( アプリケーション層の役割 )
前置きが長くなりましたが
依存関係逆転の法則です
ドメイン層はインフラ層にも依存しません
しかしファイルへの保存やデータベースへ
の保存は必要なはずです
ドメイン層で
「この機能が欲しい」
と宣言しておく (interface) ことで解決します
インフラ層はドメイン層で宣言された機能を実装
(= ドメイン層に依存する ) が、ドメイン層はドメ
イン層にある interface に依存する
→ ドメイン層はほかの層に依存しない
このように interface を間に挟むことで、ドメイン
層→インフラ層への依存関係をインフラ層→ドメ
イン層への依存関係にすることを「依存関係逆転
の法則」といいます
結論
話長くなって申し訳ありません、き
れいなコードを書けるようになれば
幸いです

More Related Content

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

きれいな設計