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
いも
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
690
Gather.townはいいぞ その後
adarapata
1
1.7k
Unityでの開発事例
adarapata
3
23k
どこのご家庭にもあるシーンマネージャーの話
adarapata
2
8.8k
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
夏だ!祭りだ!祭りとはドメインモデリングでは?
ryugen04
0
280
TSX の <Hoge<Fuga>> という構文に驚いた話 / tsx-type-argument-syntax
kanaru0928
0
230
the container ship “Apple Silicon”@WWDC26 Recap -Japan-\(region).swift
shingangan
0
120
<title><a id="</title>君はこのHTMLをパースできるか"></a></title> #雑LT_study
pizzacat83
0
140
ルールを書いて終わらせないハーネスエンジニアリング
yug1224
4
1.9k
源内ハンズオン概要編
hideg
0
180
Built Our Own Background Agent at LayerX
layerx
PRO
10
5.4k
OpenSpecのproposalにbrainstormingを持たせてみた
tigertora7571
1
230
テーブルをDELETEした
yuzneri
0
140
jsmini JavaScript Engine を作ってみた話
yosuke_furukawa
PRO
0
320
仕様書を書く前にハーネスを作る - Agent Native開発は「探索を速く、判定を固く」
gotalab555
4
1.6k
琵琶湖の水は止められてもNet--HTTPのリトライは止められない / You might be able to stop the water flow of Lake Biwa but you can't stop Net::HTTP retries
luccafort
PRO
0
710
Featured
See All Featured
Abbi's Birthday
coloredviolet
3
9.3k
The AI Revolution Will Not Be Monopolized: How open-source beats economies of scale, even for LLMs
inesmontani
PRO
3
3.7k
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
3
1.1k
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
<Decoding/> the Language of Devs - We Love SEO 2024
nikkihalliwell
1
290
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
0
370
How to Get Subject Matter Experts Bought In and Actively Contributing to SEO & PR Initiatives.
livdayseo
0
170
How to Align SEO within the Product Triangle To Get Buy-In & Support - #RIMC
aleyda
2
1.8k
So, you think you're a good person
axbom
PRO
2
2.1k
Designing Experiences People Love
moore
143
24k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
254
22k
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