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
ふつうのJavaコーディング #ccc_e1
Search
irof
May 20, 2017
34
21k
ふつうのJavaコーディング #ccc_e1
JJUG CCC 2017 Spring E1
2017-05-20 10:00-10:45
irof
May 20, 2017
Tweet
Share
More Decks by irof
See All by irof
役立つログに取り組もう
irof
28
9.6k
Javaのリリースモデル変更 その後
irof
0
75
プールにゆこう
irof
2
280
Javaマルチバージョンビルディング
irof
1
370
データ/データベースパターン
irof
0
170
勉強会で話すハードルを下げてみる
irof
0
350
Gatlingによる負荷テスト入門
irof
6
2.5k
これからのJavaのとっかかりを掴む
irof
0
10k
Listかもしれない
irof
2
520
Featured
See All Featured
Raft: Consensus for Rubyists
vanstee
136
6.6k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
Java REST API Framework Comparison - PWX 2021
mraible
PRO
28
8.2k
The Cult of Friendly URLs
andyhume
78
6k
Scaling GitHub
holman
458
140k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.2k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
26
1.4k
The Art of Programming - Codeland 2020
erikaheidi
52
13k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
232
17k
Art, The Web, and Tiny UX
lynnandtonic
297
20k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
Transcript
;ͭ͏ͷ +BWBίʔσΟϯά !JSPG DDD@F ++6($$$4QSJOH
JSPG ؔ+BWBΤϯδχΞͷձ େࡕࡏॅ ;ͭ͏ͷϓϩάϥϚ ࡢʹձࣾΊͯɺ ͳΜͱͳ͘ϑϦʔͯ͠·͢ɻ
࿐͍
͜ͷηογϣϯʔ w ࢲͷݴޠԽͰ͖ͯͳ͍Կ͔ΛͭΒͭΒͱ͠·͢ɻ w ීஈͷίʔσΟϯάϨϏϡʔͱশͯ͠దʹோΊͨ Γบ͚ͭͨΓͯ͠Δͱ͖ʹɺؾʹͯ͠Δͱ͜Ζͱ͔ ΛɺڧҾʹݴޠԽ͠Α͏ͱࢼΈͨͷͰ͢ɻ w ਖ਼͍͠ղ͋Γ·ͤΜɻͱݴ͏͔ɺ͖ͬͱ͋ͳͨͷί ϯςΩετͰؒҧ͍͋Γ·͢ɻڧΊͷݴ༿Λͬ
ͯͨΓ͢Δ͚Ͳɺ͜͜Ͱμϝͱ͔ݴͬͯΔͷΛฏવͱ Δ͜ͱ͋Γ·͢ɻ
;ͭ͏ʔ w ;ͭ͏ɺී௨ɺීஈ௨Γ w ͍ͭͬͯΔ͜ͱ w ͻΒ͕ͳʹਂ͍ҙຯͳ͍Ͱ͢
ίʔυ͔Γݟͯ͞ʔ w ʮίʔυ͚ͩʹͩ͜Θͬͯํ͕ͳ͍ʯͱ͔ݴ͏͚Ε ͲɺຊؾͰਅʹίʔυʹͩ͜ΘΔͳΒɺશମΛݟ͟Δ Λಘͳ͘ͳΔΑ w ίʔυʹͩ͜ΘΔͨΊʹ w ϓϩμΫτྑ͍ͷͰͳ͚ΕͳΒͳ͍ w
ϓϩδΣΫτԁʹ·ΘΒͳ͍ͱ͍͚ͳ͍ w Ϗδωεޭ͠ͳ͖Ό͍͚ͳ͍
ίʔυಡΈ ಡΉ ॻ͘ ʼ ճ
ίʔυಡΈ ಡΉ ॻ͘ ʼ ରͷৄ͠͞
ίʔυಡΈ w ॻ͕͔͔࣌ؒͬͯ͘ɺಡΉ͕࣌ؒॖ͞ΕΔͳΒ ɺ࣌ؒΛ͔͚ͯॻ͜͏ w ʹॻ͚ͱݴ͏ҙຯͰͳ͍ w ॻ͘ͷʹಡΉͷʹ͕͔͔࣌ؒΔͱ͔࠷ѱ
ਆࡉ෦ʹ॓Δ ѱຐࡉ෦ʹજΉ ίʔυ͕ࡉ෦͔Ͳ͏͔ Βͳ͍͚ΕͲɻ
͍ͩ͡ͳ͜ͱ
w ॻ͍ͨίʔυͷҰߦҰߦɺҰจࣈҰจࣈ Λઆ໌Ͱ͖ΔͷͰ͋Εɺ͖ͬͱͦΕ ͍͍ίʔυɻ
ͳͥͦ͏ίʔσΟϯά͔ͨ͠Λઆ໌Ͱ ͖ΔΑ͏ʹͳΓ·͠ΐ͏ɻઆ໌Ͱ͖Δ ίʔυෆ۩߹গͳ͘อक͢͠ ͍ͣͳͷͰɺ͖ͬͱ͍͍݁Ռʹͭͳ ͕ΔͣͰ͢ɻ l +BWBΤϯδχΞཆಡຊ+BWBೖΑΓ ʢࣗͰॻ͍ͨʣ
ஈҐ ͷͨΊͷ۠ΓͳͷͰɺݫີͳͷͰͳ͍Ͱ͢ɻೝఆ͠·ͤΜɻ
ॳஈ ೖ ೋஈ ݞ׳Β͠ ࡾஈ ϝιουΛखջ͚Δ ࢛ஈ ҆৺Ͱ͖ΔΦϒδΣΫτ ޒஈ දݱྗʹͩ͜ΘΔ
ஈ 4USFBNͱͷ͖߹͍͔ͨ ࣣஈ ܕΛҙࣝ͢Δ ീஈ جຊతͳࢦΛ࣋ͭ ஈ
ॳஈ
ίʔσΟϯάن͕͋ΕɺͱΓ͋͑ͣकΔ ίʔσΟϯάن͕ΰϛͳ͜ͱ͋Δ͠ɺ݁ߏଟ͍ ͚ΕͲɺ·ͣकΔ نʹʢࣦΘΕ͍ͯΔ͜ͱଟ͍Ͱ͕͢ʣཧ༝ͱ ܦҢ͕͋Γ·͢ɻͦΕΛઆ໌Ͱ͖ͣʹഁΔͷ࣌ظ ঘૣͰ͢ɻഁΔͷɺਖ਼߈๏Ͱਅਖ਼໘͔ΒഁΕΔΑ ͏ʹͳ͔ͬͯΒɻ ίʔσΟϯάنʹ४ڌ͢Δ
ͦΕͬΆ͍ຊΛͬ͟ͱಡΜͰʮ͏Μ͏Μͦ͏ͩΑ Ͷʔʯͱ͔ݴ͏͘Β͍͢Δɻ ͜ͷΜͷδϟϯϧͳͷ͚ͩΕͲɺͲΕएׯݹ͍ ͷಥͬࠐΜͰ༡Ϳͱ͍͍·͍͠ͱ͜Ζɻ ҰൠతͳϧʔϧΛᅂΉ
ೋஈ
❌ ⭕ ۭ-JTUͷͨΊʹ"SSBZ-JTUΛΘͳ͍ return new ArrayList<>(); return Collections.emptyList();
if (hoge.isFuga()) { //...(a) } //...ʢ͕͍͜͜΄Ͳμϝ͕͞૿͢ʣ if (hoge.isFuga()) { //...(b)
} टΛ͛ͯ͠·͏JG B Λ௨ͬͯ C Λ௨Βͳ͍ͷ͕ύε্ଘࡏ͢Δ
int value = ... // 0 or 1 if (value
== 0) { //...(a) } if (value == 1) { //...(b) } टΛ͛ͯ͠·͏JG ԼWBMVFFMTFͱҧ͏ҙຯͳͷʁ B C ௨Βͳ͍ͷͬͯ͋Δͷʁ ͍͍ͩͨͷ߹FOVNΛ͏͖ɻ FOVNΛ͍͢
ΞϓϦέʔγϣϯίʔυϨϕϧͰඞཁͳ͍ ͍ͨ͘ͳͬͨΒཱͪࢭ·Δ ΘͣʹͳΜͱ͔Ͱ͖ͳ͍͔ߟ͑ͯɺղܾͰ͖ͳ͍ ͳΒղܾͰ͖Δਓʹ͓ئ͍͢Δ Ͳ͏͠Α͏ͳ͔ͬͨΒɺޙչ͠ͳ͕Β͏ ΩϟετΛΘͳ͍ ϨΠϠʹԠͨ͡ਫ४ͷ"1*Λ͏
Charset.forName("UTF-8") StandardCharset.UTF_8 list.isEmpty() list.size() == 0 !list.isEmpty() list.size() != 0
list.size() > 0 ಉ͜͡ͱಉ͡Α͏ʹॻ͘ ಉ͡ϝιουʹࠞࡏͯ͠ΔͱͭΒ͍
ࡾஈ
❌ ⭕ ݻఆͷҾϝιουΛ࡞Δ method(MyEnum.HOGE); method(MyEnum.FUGA); methodHOGE(); methodFUGA(); ࣮ࡍ͜Μͳϝιου໊͚ͭͨΒԥΓ·͕͢ɻ
❌ ⭕ ϑϥάύϥϝʔλΛආ͚Δ void method(boolean flg); void trueMethod(); void falseMethod();
ઌʹ͋͛ͨͷͭͷࢦʹΑΔ ϝιουʹҾΛ૿ͦ͏͔ͱࢥͬͨ߹ϝιο υՃΛઌʹߟ͑Δ ҾΛ૿͢લʹϝιουՃΛߟ͑Δ Ҿগͳ͘͢Δ ໊લͰදݱ͢Δ
ҰͭͷϝιουͰෳରԠͰ͖ͨํ͕൚༻ੑ͕ߴ͘ ͍͍ͯ͡Όͳ͍͔ͬͯ͋Δ͔͠Εͳ͍͚ΕͲɺ ಛʹQVCMJDͳͷಛԽͨ͠ϝιουΛ࡞͢Δ QSJWBUFϝιουͳΒͬͯྑ͍ʢείʔϓ͕ڱ͚Ε ಛʹʹͳΒͳ͍ʣ ʮΘ͟Θ͟ϝιου࡞Δͷʁʯ ίʔυಡΉํ͕ଟ͍
❌ ⭕ ݅ࣜΑΓϝιουΛΉ if (list.size() == 0) { ... if
(age >= 20) { ... if(list.isEmpty()) { ... if (isAdult()) { ...
❌ ⭕ ಉ͡ΦϒδΣΫτΛԿݺͳ͍ if (hoge.isFoo()) { hoge.bar(); } hoge.barIfFooIsTrue(); ಉ͜͡ͱΛ͍ͯ͠ΔͳΒಉ͡ʹ͢Δ
❌ ⭕ BOEPSϝιουΛநग़͢Δ if (hoge.isFoo() || hoge.isBar()) { //... }
if (hoge.isFooOrBar()) { //... } ಉ͡ΦϒδΣΫτͳΒઈରΔ͖ ಉ͡ΦϒδΣΫτΛԿݺͳ͍ ݅ࣜΑΓϝιουΛΉ
❌ ⭕ ·ͱΊΔͱ if (status == Status.SUCCESS || status ==
Status.FAIURE) { //... } if (status.isFinish()) { //... } FOVNΛͨͩͷྻڍͱͯͬͯ͠ΔͱΓ͕ͪͰ͢Ͷ FOVNΛ͍͢
͍ͭಉ͍͡ํΛ͍ͯ͠ΔͷͰ͋Εɺϝιου நग़ͯ͠ɺͦͷմʹ໊લΛ͚·͠ΐ͏ Δ͜ͱ͕ಉ͡ͳͷͰ͋Εɺຊʹಉ͡ίʔυΛ ͏Α͏ʹ͢Δ Ұํɺ݁Ռతʹಉ͡Α͏ͳίʔυʹͳ͍ͬͯΔ͚ͩ Ͱ͋Εɺಉ͡ʹͳΒͳ͍Α͏ʹม͑·͠ΐ͏ ಉ͜͡ͱΛͯ͠ΔͳΒಉ͡ʹ͢Δ
ϝιου࡞Λ ໘͕͘͞Βͳ͍
࢛ஈ
class Hoge { Foo foo; Bar bar; Hoge(Foo foo) {
this.foo = foo; } Hoge(Bar bar) { this.bar = bar; } ... } த్ͳΫϥεΛ࡞Βͳ͍ GPP͘͠CBS͔͠ೖΒͳ͍Ϋϥε
Object obj; if (condA()) obj = methodA(); if (condB()) obj
= methodB(); if (condC()) obj = methodC(); // ... objΛͬͯԿ͔͢Δ ࠶ೖΛආ͚Δ PCKͷঢ়ଶ͕ෆ҆ఆʹͳΔ
class ImmutableObject { final Hoge hoge; ImmutableObject(Hoge hoge) { this.hoge
= hoge; } } ΠϛϡʔλϒϧΦϒδΣΫτΛΉ ఆٛͰʮTFUUFSΛ࣋ͨͳ͍ɺϑΟʔϧυ͕pOBMͰQSJWBUFɺΫϥε͕pOBMɺΠϯελϯεϑΟʔϧυ͕Πϛϡʔλϒϧʯ͚ͩͲɺͦ͜·Ͱؾʹ͠ͳ͍ɻ ϑΟʔϧυ͕શͯpOBMͳΫϥε
࠶ೖΛආ͚ΔͷɺΠϛϡʔλϒϧΦϒδΣΫτ ʹ͢Δͷɺঢ়ଶભҠΛ࠷খʹ͢ΔͨΊ ঢ়ଶભҠ͕͋ΔΦϒδΣΫτΛෳѻ͏ͱ؆୯ʹࢲ ͷཧղΛ͑ͯ͠·͏ ͦΜͳʹͨ͘͞Μͷ͜ͱΛؾʹ͢Δͱ͔ແཧͳͷͰɺ ؾʹ͠ͳ͖Ό͍͚ͳ͍͜ͱΛݮΒ੍ͯ͠ޚͰ͖Δঢ় ଶΛҡ࣋͢Δ ঢ়ଶભҠΛ͑Δଞͷࢦ͋ΔͣɻΕͨɻ ঢ়ଶભҠΛ࠷খʹ͢Δ
❌ ⭕ ίϯετϥΫλνΣΠϯ͢Δ Hoge(Object v) { this.v = v; }
Hoge() { this.v = "defaultValue"; } Hoge() { this("defaultValue"); }
w PQFOͨ͠ͷΛͦͷ··ʹ͠ͳ͍ w ҾΛউखʹDMPTF͠ͳ͍ ϥΠϑαΠΫϧͷରশੑΛอͭ PQFOͨ͠ՕॴͰDMPTF͢Δ USZXJUISFTPVSDFTΛ ͑ΔՕॴΛݟಀ͞ͳ͍
try (ExResource er = hoge::fuga) { //... } ----------------------------------- class
Hoge { void fuga() { ... } } interface ExResource extends AutoCloseable { @Override void close(); } USZXJUISFTPVSDFTΛ͏খٕ &YDFQUJPOΛ͛ͳ͍DMPTFϝιουΠϯλϑΣʔεʹϝιουϦϑΝϨϯεͰͭͳ͛Δɻ Ϋϥεʹ"VUP$MPTFBCMFΛ࣮ͤ͞Δͷݕ౼্ͨ͠Ͱɻ
IUUQJSPGIBUFCMPKQFOUSZQ
❌ ⭕ ಋग़Ͱ͖ΔΛอ࣋͠ͳ͍ boolean available; void setValue(Object value) { if
(value != null) this.available = true; } boolean isAvailable() { return this.value != null; }
҆৺ͯ͑͠Δ ΦϒδΣΫτΛ खʹ͠Α͏
List<Hoge> list = ... // ྫ֎͕ग़Δ͔͠Εͳ͍ list.add(hoge); // ྫ֎͕ग़Δ͔͠Εͳ͍ list.remove(hoge);
ྫ҆৺Ͱ͖ͳ͍ΦϒδΣΫτ
ޒஈ ͜ͷล͔Β ;Θͬͱͯ͘͠Δ
void method(Object parameter) { // ύϥϝʔλ͕null͔͠Εͳ͍ if (parameter == null)
{ return; } // ... } ޚతϓϩάϥϛϯάΛආ͚Δ ͷઌૹΓPSϊΠζ OVMMʹ͖߹͏
IUUQJSPGIBUFCMPKQFOUSZQ lؒͰաʹޚ͠߹͏ɺ୭৴͡ΒΕͳ͍ͱͰݴ͏ Α͏ͳٙ৺҉َͳίʔυΛॻ͍ͯͲ͏͢ΔΜͩͱ͔ࢥͬͨΓ ͠·͢ɻڍಈ͕Α͘Θ͔Βͳ͍֎෦ͷͷΛ͏ͳΒཧղग़ དྷ·͕͢ɺ੍͕ࣗͨͪޚͰ͖ΔൣғͳΒ༨ܭͳޚͰ ίʔυͷՄಡੑΛམͱ͖͢͡Όͳ͍ɻͩͬͯͦΕͬͯϊΠ ζ͡Όͳ͍Ͱ͔͢ɻ
৺ͨΓ͋Δͱࢥ͏ʢࢲ͜ͳ͍ͩͬͨ ࣈͰॻ͘ͱʮ΄͛ใʯͱ͔ʮ;͕ॲཧʯͱ͔ͦ ΕͬΆ͍͚Ͳɺຊޠ໊͔Βͯ͠Ͱ͋Δ ҙຯͷͳ໊͍લΛආ͚Δ .BOBHFS *OGP 1SPDFTT 4VNNBSZ 3FTVMU %BUB
w ಉ͡ͷʹҧ͏໊લΛ͚ͭͳ͍ w ҧ͏ͷʹಉ໊͡લΛ͚ͭͳ͍ ɾɾɾΛ͚ΒΕΔͱ͍͍ΑͶɻ ໊͚Ͱؾʹ͢Δ͜ͱ ಡΜͩ࣌ʹٙΛ๊͔ͳ໊͍લ
ίϝϯτͰؾʹ͢Δ͜ͱ ҆қʹίϝϯτΛॻ͔ͳ͍ ඞཁͳίϝϯτॻ͘ ίʔυίϝϯτಡΈखʹԿ͔Λ͑Δͷͳͷಉ͡ɻ ίϝϯτͷํ͕දݱྗ͕ߴ͘ɺίʔυͷํ͕ݫີɻ ίϝϯτ͚ͩͰಈ࡞͠ͳ͍͚ΕͲɺίʔυ͚ͩಈ࡞͢Δɻ ͦͷͨΊɺίʔυͷදݱྗ͕ेߴ͚Είʔυ͚ͩͰදݱ͢Δͷ͕͍͍ɻ ͱ͍͑ίʔυͷදݱྗʹͩ͜ΘΓ͗ͯ͢ݶ͋ΔͷͰɺίϝϯτඞཁɻ ݶ͖ͬͱࢥ͍ͬͯΔΑΓͣͬͱԕ͘ʹ͋Δͷ͚ͩΕͲɻ όϥϯεΛͱΔ
IUUQJSPGIBUFCMPKQFOUSZQ
❌ ⭕ ΑΓߴϨϕϧͷ"1*Λ༻͢Δ @RequestMapping(value = "/hoge", method = RequestMethod.GET) String
hoge() { ... @GetMapping("/hoge") String hoge() { ... ݻఆͷҾϝιουΛ࡞Δ
͋·Γͬͯཉ͘͠ͳ͍"1*͍͍γάω νϟɺҟ༷ͳ໊લʹ͢Δ ςετ༻ͷઃఆʢϝʔϧΞυϨεͳͲʣɺ։ൃ࣌ ʹ͏ελϒͷΓຊͬΆ͘͠ͳ͍ ҧ͏໘ͰͦΕ͕ΘΕͨ࣌ʹɺཱͭ͘͢͝Α͏ ʹ࡞͓ͬͯ͘ ؒҧ͍ؒҧ͍ͱΘ͔ΔΑ͏ʹ͢Δ
දݱྗʹͩ͜ΘΔ
ஈ
ೃછΈͷͳ͍"1*ɺҰΓա͗ͳ͘Β͍ͬ ͯݟ·͠ΐ͏ɻॻ͍ͯɺಡΜͰɺॻ͖ͯ͠ɺͲͬ Γਁ͔͍ͬͯͩ͘͞ɻ ͱΓ͋͑ͣɺ͋ΒΏΔίϨΫγϣϯૢ࡞Λ4USFBN "1*ͰͬͯΈΔͷ͕͍͍Ͱ͢ɻ దͳࡐ͕ͳ͍ͷͳΒɺ$PMMFDUPSTͷϝιουΛ ্͔Βॱ൪ʹҰ௨Γ৮Δͱ͍͍Ͱ͢ɻEPXOTUSFBN ͱ͔͑ΔΑ͏ʹͳΕ͍͍ͩͨେৎͰ͢ɻ 4USFBN"1*ͷ࿅श
❌ ⭕ ϝιουϦϑΝϨϯεΛΉ stream .filter(hoge -> hoge.isFuga()) ... stream .filter(Hoge::isHoge)
... ϥϜμࣜ؆୯ʹෳࡶͰಡΊͳ͍ͷʹͳΔɻϝιουϦϑΝϨϯεΛ͏΄͏͕͍͍श׳͕ͭ͘ɻ
4USFBN"1*ͷཚ༻Λආ͚Δ DPNJOH TPPO
ࣣஈ
class Hoge { LocalDate date = LocalDate.MIN; LocalDate getDate() {
return date; } } ಛघͳΛ֎ʹग़͞ͳ͍ .*/͕֎ʹग़ͯخ͍͜͠ͱͳ͍ OVMM͕Α͘ΘΕΔ͚ͲɺOVMMผ్ɻ OVMMʹ͖߹͏
❌ ⭕ ΪϦΪϦ·ͰจࣈྻʹཔΒͳ͍ if (foo.str().equals(bar.str()) { ... } if (foo.hasSameStr(bar))
{ ... } ʮ͍࣋ͬͯΔจࣈྻϓϩύςΟͰൺֱ͍ͨ͠ʯίʔυ͕ҰͭͰग़͖ͯͨΒɺ͜ͷΫϥεͦ͏ݴ͏ൺֱͷ͞ΕํΛ͢ΔͷͳͷͰ͢ɻ Ͱ͖Ε͜ͷൺֱʹదͳ໊લΛ༩͍͑ͨͱ͜Ζɻ͜͜Ͱڍ͛ͨΑ͏ͳػցతͳ໊લ͡Όͳ͘Ͷɻ
ϝιουҾͳͲͰ҆қʹΘΕΔจࣈྻɺΠϯ λϑΣʔεͱͯ͠ҙຯ͕͗͢Δ͠ɺશͯͷจࣈ ྻΛॲཧͰ͖ΔΑ͏ʹ࡞ΒΕ͍ͯΔ͔ඍົͩΖ͏͠ɺ ͦͦʮશͯͷจࣈྻΛॲཧ͢Δඞཁ͕͋Δʯͳ Μͯ͜ͱɺͦ͏ͦ͏ͳ͍ɻ จࣈྻͷΑ͏ͳܕͰΓͱΓ͢ΔͷͰͳ͘ɺϓϩ μΫτͷܕͰΪϦΪϦ·Ͱॲཧ͢ΔɻΪϦΪϦͬͯ ͷ6*ͱ͔ӬଓԽͱ͔ɺ+BWBͷ֎ʹग़Δલɻग़͞ ͳ͍͍ͯ͘ͳΒ࠷ޙ·Ͱग़͞ͳ͍ɻ ΪϦΪϦ·ͰจࣈྻʹཔΒͳ͍
FOVNΛ͍͢ ݸҎ্ͷΛ࣋ͯΔ ϝιουΛ࣋ͯΔ ΠϯλϑΣʔεΛ࣮Ͱ͖Δ
ʮࣅ͍ͯΔʯͱײͨ͡ͳΒɺܕͰറͬͯΈΔ ؾ͍͍ͮͯͳ͔ͬͨΓޱ͕ग़ͯ͘ΔՄೳੑߴ͘ ͯɺݴ༿͡ΌදݱͰ͖ͳ͔ͬͨڞ௨ੑ͕͋Δͷ͔ ͠Εͳ͍͠ɺશવҧ͏ͷ͕ۮવࣅͨܗʹͳ͍ͬͯ ͚͔ͨͩ͠Εͳ͍ ײੑΛ৴ͯ͡ίʔυʹͯ͠ΈΔ ຊʹಉͩͬͨ͡ΒܕʹͰ͖Δ͠ɺҰܕͰറΕΔ ͱ͍Ζ͍ΖָʹͳΔ͚Ͳɺͨ·ʹؒҧ͑Δ ࣅͨܗͷߏܕͰറΔ
// ͨͿΜ0Λ͍ͨ͘ͳ͍ int i = 0; // ͨͿΜۭจࣈʹ͍ͨ͠Θ͚Ͱͳ͍ String str
= ""; // ͨͿΜnullΛ͏͖͡Όͳ͍ Hoge hoge = null; OVMMʹ͖߹͏
OVMM ڻ͘΄Ͳ҆қʹΘΕ͍ͯΔ͕ɺ࣮ࡍ ΘΕ͍ͯΔҙຯΑΓͬͱ൚༻తͳମɻͳͷʹɺ Λ໌֬ʹ͢Δ͜ͱ͔Βಀ͛ͨ߹ʹແअؾʹબ͞ Εͯ͠·͏ɻ Λى͜͢ίʔυʹ݁ߏͳසͰ OVMMؚ͕ ·Ε͍ͯΔɻఆٛ͢Δ͜ͱ͔Βಀ͛ͨͷ͔ͩΒɺͦ͏
ݴ͏ͱ͜Ζʹෆ۩߹͕જΉͷࢸۃવɻ ͜ΕΒ໓ଟʹͰͯ͜ͳ͍ͣͳͷͰɺ͏ͱ͖શ ྗͰݴ͍༁ͷίϝϯτΛॻ͍ͯ͏ɻ OVMMʹ͖߹͏ ඞཁͳίϝϯτॻ͘
❌ ⭕ ۭΠϯελϯεΛ࡞Δલʹཱͪࢭ·Δ Hoge hoge = new Hoge(); hoge.setValue(value); ...
Hoge hoge = new Hoge(value); ΠϛϡʔλϒϧΦϒδΣΫτΛΉ
class Hoge { final Fuga fuga; final Piyo piyo; Hoge()
{ this(new Fuga(), new Piyo()); } Hoge(Fuga fuga, Piyo piyo) { this.fuga = fuga; this.piyo = piyo; ... ۭΠϯελϯεΛ࡞Δલʹཱͪࢭ·Δ ຊʹσϑΥϧτίϯετϥΫλΛ༻ͨ͠ΠϯελϯεͰ͍͍ͷʁ σϑΥϧτίϯετϥΫλͰੜ͞ΕΔΠϯελϯεશʹಈ࡞͢ΔΑ͏ʹ࡞ΒΕͯΔͷʁ ୯ͳΔͷίϯςφʹͳͬͯͳ͍ʁͦͷίʔυಡΈ͍͢ʁ
ͦͷΠϯελϯε͕Կऀ͔આ໌Ͱ͖Δ͔ʁ ͦͷΠϯελϯεۭͱͯ͠ਖ਼͘͠ৼΔ͏͔ʁ ͑ΒΕͳ͍ͷͰ͋ΕɺԿ͔ؒҧ͍͑ͯΔɻޚ తϓϩάϥϛϯάͤ͟ΔΛಘͳ͍ঢ়ଶʹͳΔͩΖ͏ ͠ɺޚతϓϩάϥϛϯάؒҧ͍ͬͨํͷष͍ Ͱ͢ɻ ۭΠϯελϯεΛ࡞Δલʹཱͪࢭ·Δ ޚతϓϩάϥϛϯάΛආ͚Δ
ീஈ
ಉ͡ϨΠϠͰಉ͡ਫ४ͷ"1*Λ༻͢Δ ϨΠϠʹԠͨ͡ਫ४ͷ"1*Λ͏ ରশੑ ରশੑஅยతͳίʔυ͚ͩͰͳ͘ɺ৭ʑͳ Ϩϕϧʹద༻͢Δͷɻ
❌ ⭕ ରশੑίʔυͷநΛἧ͑Δ void hoge() { add(); this.count++; save(); }
void fuga() { add(); countUp(); save(); }
ඍົͳίʔυΛॻ͔͟ΔΛಘͳ͍͕࣌͋Δɻ ͍ΖΜͳखΛਚͯ͘͠શྗͰճආΛࢼΈΔͷલఏ ͚ͩΕͲɺͦΕͰಧ͔ͳ͍࣌ɻ μϝͳͷΛཱͨͤΔɻμϝͩͱΘ͔ΔΑ͏ʹɻ μϝͳͷΛཱͨͤΔ ཱͭ͘Β͍μϝͳίʔυΛॻ͘
ஈ
ڧ͍ҙࢥͰ ϧʔϧΛഁΔ
Ϩϕϧͷ্͛ํ
ʮΘ͟Θ͟࡞Δʯ ͱ͖߹͏
ϝιουͷͱ͜ΖͰ৮Ε͚ͨΕͲɺΫϥεͰͳ ΜͰҰॹɻʮΘ͟Θ͟࡞Δʯͱݴ͏ߟ͑Λҙࣝత ʹࣺͯΒΕΔΑ͏ʹ܇࿅͢Δɻ ࡞͢ΔίετͳΜͯࠣࡉͳͷͰ͢ɻίετ͕ߴ ͍ͱײ͡Δͷɺ͖ͬͱ࡞Γ׳Ε͍ͯͳ͍͔Βɻ Ұํɺ͕૿͑ΔͱಡΈͮΒ͘ͳΔͷࣄ࣮ɻ͍ͭ ͩͬͯʮίʔυಡΈʯΛୈҰʹҙࣝ͢Δ͜ͱɻ Θ͟Θ͟࡞Δʁ
ʮؾΛ͚ͭΔʯ ͱ͖߹͏
ؾΛ͚ͭͯΘͳ͍ͱ͍͚ͳ͍"1*͚ͬ͜͏ଟ͘ ͯɺ൚༻తͰͳΜͰͰ͖ΔܥͷϨϕϧ"1*ͦ ͏ݴ͏ʹ͋Δ͔Βํ͕ͳ͍ Ͱɺͦͷ͍ํΛؒҧ͑ͨ࣌ʹέΞϨεϛεѻ͍ ͍ͯ͠ΔͱԿมΘΒͳ͍ ʮؾΛ͚ͭΔʯ ؾΛ͚ͭΔʹؒҧ͍͍͑ͯ
ʮؾΛ͚͍ͭͯΕى͜Βͳ͔ͬͨෆ۩߹ʯɺઃ ܭෆඋͷष͍Ͱ͢ ͦͷϨΠϠͰΘΕΔ͖Ͱͳ͍දݱྗநͷ "1*͕͍ࠞͬͯͯ͡ɺޡ͍ͬͨํΛ͞Ε͕ͨނʹ ൃੜͨ͠ͷͰ͠ΐ͏ ઃܭෆඋͷष͍ ؒҧ͑ΔՄೳੑ͕͋Εɺؒҧ͑Δ
͍ํΛنఆ͢ΔɻͦͷͨΊʹ൚༻తͳܕΛͦͷ· ·͏ͷΛආ͚Δɻ ಛԽ͍ͨ͠ํͷͨΊͷɺಛԽͨ͠ΫϥεͱɺಛԽ ͨ͠ϝιουΛ࡞͍ͬͯ͘ɻ 4USJOHͱ͔ΛͬͯΔͱɺ͍ͭ·ͰʮTVCTUSJOHͬ ͯʯͱ͔ɺϨϕϧͳ"1*ΛΔ͜ͱʹͳΔɻ ͦͦؒҧ͑ΒΕͳ͍Α͏ʹ
OVMMͱ͖߹͏
ʮ/VMM1PJOUFS&YDFQUJPOʹͳ͔ͬͨΒOVMMνΣοΫ ΛೖΕͨʯ༗ࡑɻ҆қʹOVMMνΣοΫΛೖΕΔͷ ͦͷ͠ͷ͗͗͢Δɻ ͦͦOVMM͕ͦ͜ʹݟ͑Δ͜ͱ͕͓͔͍͠ OVMMͷҾྗʹ߅͏ͨΊʹͯ͢ͷ͜ͱΛߟ͑ͯɺ࣮ ફ͢Δɻ0QUJPOBMʁͲ͏ͩΖ͏Ͷɻ ͨͱ͑ɾɾɾ ಛघͳΛ֎ʹग़͞ͳ͍
IUUQJSPGIBUFCMPKQFOUSZ 0QUJPOBM OVMMΑΓΑ΄Ͳྑ͍͕ɺ͑Ͱͳ͍ɻ
ܕͱ͖߹͏ ⾠ܕܯͷํ͓ؼΓ͍ͩ͘͞
ϓϦϛςΟϒܕ+BWBඪ४ϥΠϒϥϦɺ൚༻ϥΠϒ ϥϦʹଘࡏ͢ΔܕɺۀΛද͢ʹ൚༻త͗͢Δ ྫ͑จࣈྻ4USJOHͰද͞ΕΔͷ͔͠Εͳ͍͚ ΕͲɺࠓʮจࣈྻͱͯ͠ѻ͓͏ͱ͍ͯ͠ΔԿ͔ʯʹ 4USJOHͷͯ͢ͷػೳཁΒͳ͍͠ɺͬͱಛԽ͠ ͨػೳ͕͋ΔΜ͡Όͳ͍͔ɺͱ͔ ࢲͯ͢ͷจࣈྻʹରԠ͢ΔॲཧͳΜͯॻ͖ͨ͘ ͳ͍ʢগͳ͘ͱͦ͜ʹʣ ͖߹͍ํ
ʮཧͷίʔυʯ ʹ͖߹͏
IUUQJSPGIBUFCMPKQFOUSZQ ࢲࣗͷٻΊ͍ͯΔ$MFBO$PEF UIBUXPSLT Λڻ͖ ͷͳ͍ίʔυͱଊ͍͑ͯΔɻͭ·Γʮී௨ͦ͏ॻ͘ΑͶʯ ͱݴΘΕΔΑ͏ͳͷɻ͋Δͯࣗ͘͠વͱͦ͏ͳ͍ͬͯΔ ͷɻ͓ͦΒ͘ग़དྷ্͕Δίʔυڽͬͨίʔυͱݴ ΘΕͳ͍ɻ
൚༻ੑ߅͏
൚༻ੑ͕ߴ͍ਖ਼ٛͷΑ͏ʹޠΒΕ͕ͪɻ ͚ͩͲɺ൚༻ΛٻΊΔͱ؆୯ʹखଓ͖తʹͳΔɻ ੍ޚ༻ͷύϥϝʔλ͕େʹͳΓɺෳࡶͳঢ়ଶભҠ Λ࣋ͭΑ͏ʹͳΓɺ߹ͤരൃʹԡͭ͠Ϳ͞ΕΔɻ γεςϜ։ൃʹ͓͍ͯɺ൚༻ੑΑΓγεςϜʹద͠ ͨಛԽΛҙࣝ͢Δɻաͳ·ͰʹಛԽ͢Δɻอकੑ ͕མͪΔʁ൚༻తʹͯͯ͠อकੑΑ͔ͬͨʁ ൚༻ੑͷ༠
ಉ༷͡ͱ͖߹͏
͋ΒΏΔϨϕϧͷʮಉ༷͡ʯʹහײʹͳΔ ෳͷΫϥεͰࣅͨΑ͏ͳίʔυ͕͋ΔͳΒɺͦͷ ʮԿ͕ࣅ͍ͯΔͱײ͔ͨ͡ʯʹ͖߹͏ ίʔυ͚ͩ͡Όͳ͘ɺύοέʔδΫϥεߏɺΠ ϯελϯεͷϥΠϑαΠΫϧγʔέϯεʹʮಉ ༷͡ʯݟ͑ͯ͘Δͣ ײ֮Λେࣄʹͯ͠΄͍͠ ಉ༷͡
࠷ޙʹ
ࡾલͷࣗͷίʔυɺ ποίϛͲ͜ΖຬࡌͰࠔΔɻ ʢͱ͔ݴ͍ଓ͚ͯΔʣ