Nokogiri
December 24, 2023
58

FizzBuzzで学ぶOCP

Nokogiri

December 24, 2023

Transcript

3. FizzBuzzͱ͸ʁ • ϓϩάϥϛϯάͨ͜͠ͱ͕͋ΔਓͳΒҰ౓͸ฉ͍ͨ͜ͱ͕͋Δݴ༿༡ ͼͰ͢ɻ • ྫʣ̏ͷഒ਺Ͱ Fizzɺ̑ͷഒ਺ͰBuzzɺ̏ͱ̑ͷഒ਺ͷͱ͖ʹ FizzBuzzͱ͍͏ • ϓϩάϥϛϯάͷ࿅शͱͯ͠Α͘࢖ΘΕ·͢ɻ

• ࠓ೔͸͜ͷFizzBuzzΛ࢖ͬͯOCPʹ͍ͭͯઆ໌͠·͢ɻ
4. OCPͬͯͳʹʁ • ΦϒδΣΫτࢦ޲ͷݪଇ SOLIDͷҰ෦ • ։์ด࠯ݪଇOpen Closed PrincipleʢOCPʣͷུ • ֦ுʹରͯ͠Φʔϓϯ

• มߋʹରͯ͠Ϋϩʔζυ
5. γϯϓϧͳ FizzBuzz // 3ͷഒ਺ͷ৔߹͸Fizz // 5ͷഒ਺ͷ৔߹͸Buzz // 3ͱ5ͷഒ਺ͷ৔߹͸FizzBuzz // ͱදࣔ͢Δ

export const fizzBuzz = (num: number): string => { if (num % 3 === 0 && num % 5 === 0) { return "FizzBuzz"; } if (num % 3 === 0) { return "Fizz"; } if (num % 5 === 0) { return "Buzz"; } return ""; }; 3ͷഒ਺ͷ৔߹͸Fizz 5ͷഒ਺ͷ৔߹͸Buzz 3ͱ5ͷഒ਺ͷ৔߹͸FizzBuzz ͦΕҎ֎ͷ৔߹͸"" Λฦ͢

7. ͜Εͬͯ࢓༷มߋʹڧ͍ʁ • ഒ਺͸̏΍̑Ͱ͸ͳ͍͔΋͠Εͳ͍ʁ • ϧʔϧ͸௥Ճ͞ΕΔ͔΋͠Εͳ͍ʁ • ฦ͢จࣈྻ͸ʮFizzʯʮBuzzʯ ͔ΒʮΘΜʯʮʹΌΜʯʹมΘΔ͔΋ʁ → ͜ͷͨͼʹ

fi zzBuzzؔ਺Λमਖ਼͢Δͷ͸ෆ۩ ߹ͷݪҼʹͳΔͷͰ͸ʁ
8. ࢓༷ͷຊ࣭ // 3ͷഒ਺ͷ৔߹͸Fizz // 5ͷഒ਺ͷ৔߹͸Buzz // 3ͱ5ͷഒ਺ͷ৔߹͸FizzBuzz // ͱදࣔ͢Δ export

const fizzBuzz = (num: number): string => { if (num % 3 === 0 && num % 5 === 0) { return "FizzBuzz"; } if (num % 3 === 0) { return "Fizz"; } if (num % 5 === 0) { return "Buzz"; } return ""; }; ੔਺Λೖྗ͢ΔͱจࣈྻΛฦ ͢ ೚ҙͷม׵ϧʔϧΛෳ਺ఆٛͰ ͖Δ ม׵ϧʔϧ͸ԿΒ͔ͷ൑ఆ৚݅ Λຬͨͨ͠ͱ͖ʹదԠ͞ΕΔ

10. ίʔυҊ // 3ͷഒ਺ͷ৔߹͸Fizz // 5ͷഒ਺ͷ৔߹͸Buzz // 3ͱ5ͷഒ਺ͷ৔߹͸FizzBuzz // ͱදࣔ͢Δ export

const fizzBuzz = (num: number): string => { if (num % 3 === 0 && num % 5 === 0) { return "FizzBuzz"; } if (num % 3 === 0) { return "Fizz"; } if (num % 5 === 0) { return "Buzz"; } return ""; }; RuleΛ࡞Δ Matchɿೖྗʹ߹க͢Δ৚ ݅Λදݱ͢Δ apply: ৚݅ʹ߹கͨ͠৔߹ ͷॲཧΛදݱ͢Δ RULE_SETS: దԠ͞ΕΔ ϧʔϧͱॱ൪Λදݱ͢Δ
11. ίʔυҊ // 3ͷഒ਺ͷ৔߹͸Fizz // 5ͷഒ਺ͷ৔߹͸Buzz // 3ͱ5ͷഒ਺ͷ৔߹͸FizzBuzz // ͱදࣔ͢Δ export

const fizzBuzz = (num: number): string => { if (num % 3 === 0 && num % 5 === 0) { return "FizzBuzz"; } if (num % 3 === 0) { return "Fizz"; } if (num % 5 === 0) { return "Buzz"; } return ""; }; ֦ுʹΦʔϓϯɿϧʔϧΛ௥ Ճ͢Δ৔߹͸৽͘͠ϧʔϧΛ ఆٛͯ͠RULE_STESʹ௥Ճ ͢Δ͚ͩ มߋʹΫϩʔζυɿϝΠϯͷ ॲཧͰ͋Δ fi zzBuzzʹ͸Өڹ Λ༩͑ͳ͍ɻมߋର৅ͷϧʔ ϧͷΈमਖ਼Ͱ͖Δ
12. ·ͱΊ • ίʔυͷຊ࣭తͳ࢓༷ʹண໨ͯ͠Ͳ͕֦͜ுɾมߋ͞Ε΍͍͢ͷ͔ߟ ͑Δ • FizzBuzz͸αϯϓϧͰ͍ΖΜͳॲཧʹԠ༻Մೳ • ྫʣೖྗͷHTML/JSON/Text ʹ߹ΘͤͯύʔαʔͷॲཧΛ੾Γସ͑ Δ

• ࢓༷ͷ௥Ճɾมߋ͕ϝΠϯͷॲཧʹӨڹΛ༩͑ͳ͍Α͏ʹ͢Δͷ͕େ ࣄ