Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Humble Object Patternな話
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
いも
February 21, 2019
Programming
2.3k
2
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Humble Object Patternな話
Roppongi.unity #01の資料です
いも
February 21, 2019
More Decks by いも
See All by いも
UnityプログラミングバイブルR6号宣伝&Unity Logging小話
adarapata
0
640
Unityテスト活動のふりかえり
adarapata
1
670
Gather.townはいいぞ その後
adarapata
1
1.7k
Unityでの開発事例
adarapata
3
23k
どこのご家庭にもあるシーンマネージャーの話
adarapata
2
8.7k
Gather.townはいいぞ
adarapata
2
2.4k
宴はいいぞ
adarapata
0
2.2k
わかった気になるモブプログラミング
adarapata
1
180
モブワークっぽいのをやっている話/Trying mobwork
adarapata
2
1.3k
Other Decks in Programming
See All in Programming
気圧・高度・GPSを記録&可視化するアプリ「Koudo」を作った話
hjmkth
1
330
PHPで使える日時の表現と、その知り方 #frontend_phpcon_do
o0h
PRO
0
270
dRuby over BLE
makicamel
2
390
過去最大のMCPアップデート! 2026-07-28 RC版の謎に迫る
licux
6
420
Mujeres en SEO Summit 2026 - Greatest Disaster Hits en Web Performance
guaca
0
210
AIキャラアプリkaiwaの低遅延音声通話基盤をどう作ったか - AWS Gravitonで支える低遅延・低コストAI Agent基盤
mogamit
0
120
トークンをケチるな、設計しろ:GitHub Copilotを賢く使うコンテキスト戦略
ochtum
0
240
Performance Engineering for Everyone
elenatanasoiu
0
240
「なぜそう決めたのか」を残し続ける仕組み ― Notion AI カスタムエージェント × Slack連携による設計判断の自動記録 - NIKKEI Tech Talk #47
niftycorp
PRO
0
240
Webフレームワークの ベンチマークについて
yusukebe
0
180
Developing with AI Agents — Codex, Claude Code & Cowork Practical Guide
x5gtrn
PRO
0
1.3k
正しくソフトウェアを作る、前提を疑うための認知の視点 / doubt-premise
minodriven
21
7.1k
Featured
See All Featured
Information Architects: The Missing Link in Design Systems
soysaucechin
0
990
GitHub's CSS Performance
jonrohan
1033
470k
Done Done
chrislema
186
16k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
The Mindset for Success: Future Career Progression
greggifford
PRO
0
370
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.3k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
2k
Being A Developer After 40
akosma
91
590k
Designing Powerful Visuals for Engaging Learning
tmiket
1
430
Design of three-dimensional binary manipulators for pick-and-place task avoiding obstacles (IECON2024)
konakalab
0
470
Abbi's Birthday
coloredviolet
3
8.3k
Site-Speed That Sticks
csswizardry
13
1.2k
Transcript
Humble Object Pattern な話 Roppongi.unity #1 2019/02/21 Roppongi.unity #1 2019/02/21
いもです いも(@adarapata) ゲー ムクライアントエンジニア adarapata.com Roppongi.unity #1 2019/02/21
ご注意 2019/1/23 にGotanda.unity #10 で発表した「 どこから始めるUnity Test」 の時間切れで話せなかった後半部分にフォー カスした内容です。 https://speakerdeck.com/adarapata/dokokarashi-meruunity-test
なので前回と若干被る話が多いのでご了承ください。 Roppongi.unity #1 2019/02/21
今日の話 Humble Object Pattern について Roppongi.unity #1 2019/02/21
前提 みんなテストが書きたくて手が震えている Roppongi.unity #1 2019/02/21
テストしにくさの元 密結合 static なインスタンス 入力イベントが絡む処理 UI が絡む処理 ファイル、DB などの外部リソー スが絡む処理
etc.. 実際問題、 どう綺麗に書いてもテストしにくい部分は出てくる Roppongi.unity #1 2019/02/21
テストピラミッド。 上位に行くほど難易度が高い Roppongi.unity #1 2019/02/21
コスト高いのでテスト避けたい わかる 時にはそういう判断も必要かもしれない 書かない ≠ 書けない 書けない理由は明らかにすべき。 ピラミッドの境界を跨ぐような処理を分割していく Roppongi.unity #1
2019/02/21
Humble Object Pattern とは テストしやすいものとしにくいものを住み分ける実装パター ン テストしにくいものの実装をHumble( 控え目) にする 多分初出は
xUnit Patterns http://xunitpatterns.com/Humble Object.html クリー ンアー キテクチャ本にも載ってたので有名かも Roppongi.unity #1 2019/02/21
public class PlayerUnit : MonoBehaviour { [SerializeField] private float _speed
= 1F; void Update() { var horizontal = Input.GetAxis("Horizontal"); var vertical = Input.GetAxis("Vertical"); Move(new Vector3(horizontal, vertical)); } private void Move(Vector3 direction) { transform.position += direction * _speed; } } きちんと任意の方向に動くかテストを実装したい Roppongi.unity #1 2019/02/21
書きにくさポイント 入力が絡むので書きにくい MonoBehavior の機能に依存しているので書きにくい SerializeField も同様 でも移動する部分は書きたい 書きやすいものと書きにくいものを切り分ける。 Unity における書きにくいものは大体MonoBehaviour
絡みなのでそこか ら切り出す。 Roppongi.unity #1 2019/02/21
public interface IPlayerUnit { float Speed { get; } Vector3
Position { get; set; } } // MonoBehavior 成分を0にしたけど、 移動ロジックを持つクラス public class PlayerUnitController { private readonly IPlayerUnit _unit; public PlayerUnitController(IPlayerUnit unit) { _unit = unit; } public void Move(Vector3 direction) { _unit.Position += direction * _unit.Speed; } } テストを書きにくいMonobehavior からロジックを抽出する Roppongi.unity #1 2019/02/21
public class PlayerUnitHumble : MonoBehaviour, IPlayerUnit { [SerializeField] private float
_speed = 1F; private PlayerUnitController _controller; public float Speed => _speed; public Vector3 Position { get => transform.position; set => transform.position = value; } void Start() => _controller = new PlayerUnitController(this); void Update() { var horizontal = Input.GetAxis("Horizontal"); var vertical = Input.GetAxis("Vertical"); _controller.Move(new Vector3(horizontal, vertical)); } } Roppongi.unity #1 2019/02/21
変更点 移動処理の詳細と、Monobehavior が離れた 具体的な処理は PlayerUnitController に任せるようになった テストしやすいものとしにくいものに分かれた ピラミッドの境界が明確になった 切り分けられたので、 移動処理のテストも書ける。
入力のテストはやらない! 移動処理を行う側はインタフェー スだけ見ているので、 差し替えが楽。 Roppongi.unity #1 2019/02/21
IPlayerUnit は状況に応じて差し替えられる Roppongi.unity #1 2019/02/21
IPlayerUnit は状況に応じて差し替えられる Roppongi.unity #1 2019/02/21
テストコー ド public class PlayerUnitTest { // テスト用のいい感じモック public class
MockPlayerUnit : IPlayerUnit { public float Speed { get; set; } public Vector3 Position { get; set; } } [Test] public void PlayerUnitMove() { var unit = new MockPlayerUnit { Speed = 5, Position = Vector3.zero }; var controller = new PlayerUnitController(unit); controller.Move(Vector3.up); Assert.AreEqual(new Vector3(0,5F,0), unit.Position); } } Roppongi.unity #1 2019/02/21
まとめ テストしにくい部分は発生する テストしにくいところとしやすいところを分けて、 書ける領域を増 やす Humble Object はそれらを切り分ける Roppongi.unity #1
2019/02/21