Upgrade to Pro — share decks privately, control downloads, hide ads and more …

AWS CDKで学ぶGoFデザインパターン 〜IaCにもコード設計〜 / aws dev day cdk gof design patterns

k.goto
June 21, 2023

AWS CDKで学ぶGoFデザインパターン 〜IaCにもコード設計〜 / aws dev day cdk gof design patterns

AWS Dev Day 2023 Tokyo登壇資料です。

k.goto

June 21, 2023
Tweet

More Decks by k.goto

Other Decks in Programming

Transcript

  1. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS CDKで学ぶGoFデザインパターン 〜IaCにもコード設計〜 後藤 健太 (k.goto) 2 0 2 3 . 0 6 . 2 2 - 2 3 A W S D E V D A Y 2 0 2 3 T O K Y O B - 1
  2. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ⾃⼰紹介 • 後藤 健太 (k.goto) § テックリード § AWS Community Builder (DevTools) § 365歩のテック (技術ブログ) § ⾃作AWSツールのOSS開発 § AWS CDK コントリビューター § Twitter: @365_step_tech – k.goto(365歩のテック)
  3. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. アジェンダ • GoFデザインパターンとは • 本セッションの⽬的と注意 • GoFデザインパターン × AWS CDK実例集 • まとめ
  4. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. GoFデザインパターンとは
  5. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. GoFデザインパターンとは • オブジェクト指向におけるコード設計のデザインパターン § 書籍『オブジェクト指向における再利⽤のためのデザインパターン』 (※) – 通称『GoF本』 – GoF(Gang of Four) = この共著者の4⼈ – 全23パターン • 古くからあるが、AWS Cloud Development Kit (AWS CDK) の 内部実装にも⼀部⽤いられている ※『オブジェクト指向における再利⽤のためのデザインパターン』 (ソフトバンクパブリッシング) 著: エーリヒ・ガンマ、ラルフ・ジョンソン、リチャード・ヘルム、ジョン・ブリシディース 監訳: 本位⽥真⼀, 吉⽥和樹
  6. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 本セッションの⽬的と注意
  7. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 本セッションの⽬的 • AWS CDKを通してGoFデザインパターンを学ぶ • AWS CDKコードを効率的にする ※もちろんCDKコードの効率性に繋がる点もたくさんありますが、あくまで 本セッションではそれらを推奨・⽬的とするものではありません。 AWSユーザに馴染み深いAWS CDKを通して GoFデザインパターンを学ぼう︕
  8. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 注意 • 本来のGoFデザインパターンと表現が多少異なる場合があります。 § CDKコードにうまく適⽤するため • CDKコードを書く上で、通常と⽐べて冗⻑になる場合があります。 § GoFデザインパターンを適⽤・説明しやすくするため
  9. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. GoFデザインパターン × AWS CDK実例集
  10. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. GoFデザインパターン × AWS CDK実例集 ① ② ③ ④ ⑤ ⑥ ⑦ ⑧ ⑨ ⑩
  11. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. ①Compositeパターン
  12. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ①Compositeパターン • ⽊構造を伴う再起的なデータ構造を利⽤した⼿法 § 容器と中⾝を同⼀視することで再起的な構造の⾛査が可能に
  13. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ①Compositeパターン 【CDKでのシチュエーション】 § 特定のリソース群に対して⼀元的にパラメータ設定などの操作を適⽤したい 【使⽤例】 § AWS CDKの特徴であるリソースのツリー構造を利⽤して、 特定のコンストラクト内の全リソース、またはスタック内のすべてのリソースに 対して⼀括で「RemovalPolicy」パラメータを指定する
  14. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ①Compositeパターン 【CDKクラス図】
  15. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ①Compositeパターン 【CDKクラス図 簡略版】
  16. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ①Compositeパターン 【⽐較】
  17. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ①Compositeパターン 【コード例】
  18. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ①Compositeパターン 【コード例】 →ResourceならRemovalPolicy適⽤ →Constructなら、さらにその⼦供達に対して同メソッドを再起呼び出し ※applyRemovalPolicy: Resourceに定義されたRemovalPolicy適⽤メソッド ※RetainConstruct: Retainにしたいリソースを集めた⾃作Construct
  19. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. GoFデザインパターン × AWS CDK実例集 ①Compositeパターン ② ③ ④ ⑤ ⑥ ⑦ ⑧ ⑨ ⑩
  20. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. ②Facadeパターン
  21. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ②Facadeパターン • 複数のクラス群を組み合わせて⾏う複雑な⼿順を、⼀つの窓⼝を 設けることで、それを呼び出すだけで簡単に⾏えるようにする⼿法
  22. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ②Facadeパターン 【CDKでのシチュエーション】 § 複雑なリソース構築をうまくまとめてシンプルに呼び出したい 【使⽤例】 § Constructを作成してその中に⼀連のリソース定義をまとめて定義し、 Stackからはそれを呼び出すだけでシンプルなリソース構築を⾏う
  23. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ②Facadeパターン 【CDKクラス図】
  24. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ②Facadeパターン 【⽐較】
  25. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ②Facadeパターン 【コード例】 Facade≒Constructみたいなもの ① ① ② ③ ② ③
  26. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. GoFデザインパターン × AWS CDK実例集 ①Compositeパターン ②Facadeパターン ③ ④ ⑤ ⑥ ⑦ ⑧ ⑨ ⑩
  27. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. ③Adapterパターン
  28. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ③Adapterパターン • 既存のクラスに⼿を加えられない時に、そのクラスを変更すること なく機能拡張をするための⼿法 § ライブラリの拡張など後⽅互換性を損ないたくないケースで有効
  29. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ③Adapterパターン • 継承でなく委譲(コンポジションを利⽤した技法)する形も可 § AdapterがAdapteeをプロパティとして持ち、Adapterのメソッド内部でAdapteeの メソッドを呼び出す • 本セッション例ではCDKとの相性の理由で継承を使⽤しているが、 本来は継承より委譲の⽅が望ましい § “クラス継承よりもオブジェクトコンポジションを多⽤すること” – GoF本 第1章『概論』より
  30. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ③Adapterパターン 【CDKでのシチュエーション】 § CDK提供のリソース定義(BucketやQueue等)に、新たなメソッドを追加したい 【使⽤例】 § あるリソースに複数のリソースポリシーを⼀度の呼び出しで⼀気に付けたいが、 単⼀ポリシーを付けるメソッド(addToResourcePolicy)しか⽤意されていない § そこで、それらのリソースクラスを拡張する新たなクラスを作り、そのクラスに ⾃作のメソッド(addManyToResourcePolicy)を定義する
  31. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ③Adapterパターン 【CDKクラス図】
  32. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ③Adapterパターン 【⽐較】
  33. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ③Adapterパターン 【コード例】 ※Bucketをextendsしているので、⾃⾝(this)が addToResourcePolicyメソッドを持っている →それをループで回すことで⼀気にポリシーをアタッチするように⾒せる ① ② ① ②
  34. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ③Adapterパターン 【コード例】
  35. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. GoFデザインパターン × AWS CDK実例集 ①Compositeパターン ②Facadeパターン ③Adapterパターン ④ ⑤ ⑥ ⑦ ⑧ ⑨ ⑩
  36. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. ④Decoratorパターン
  37. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ④Decoratorパターン • 基となるオブジェクトに動的に飾りつけを重ねていき、柔軟に機能の 拡張を⾏うための⼿法 § オブジェクトをラップすることで、飾りつけ(機能追加)が可能に
  38. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ④Decoratorパターン 【CDKでのシチュエーション】 § リソースを作成するときに、あらかじめ特定の設定が適⽤されたものが 作られるようにしたい 【使⽤例】 § リソースを作成する際にいちいちapplyRemovalPolicyメソッドを呼び出さずに RemovalPolicyが適⽤できるようなラップクラスを定義する
  39. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ④Decoratorパターン 【CDKクラス図】
  40. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ④Decoratorパターン 【CDKクラス図 簡略版】
  41. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ④Decoratorパターン 【⽐較】
  42. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ④Decoratorパターン 【コード例】 ※若⼲無理やりパターンを実現をしています constructor内で、サブクラスのgetOwnRemovalPolicyでRETAIN|DESTROYを呼び、 それを⾃⾝のapplyRemovalPolicyに渡している。 これによってnewと同時にRemovalPolicyの適⽤ができるようになる。 ① ② ③ ① ② ③
  43. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ④Decoratorパターン 【コード例】
  44. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. GoFデザインパターン × AWS CDK実例集 ①Compositeパターン ②Facadeパターン ③Adapterパターン ④Decoratorパターン ⑤ ⑥ ⑦ ⑧ ⑨ ⑩
  45. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. ⑤Singletonパターン
  46. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ⑤Singletonパターン • あるクラスのコンストラクタが、何度呼び出されても⼀つしか インスタンスが作成されないことを保証する⼿法 § ⼀度作られたインスタンスを使い回す ※今はあまり推奨されていないパターン
  47. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ⑤Singletonパターン 【CDKでのシチュエーション】 § あるスタック内で何度呼び出されても作られるリソースは⼀つであって欲しい – Custom ResourceのハンドラーとしてLambda関数を使う場合など 【使⽤例】 § CDK提供のSingletonFunctionクラスを使う ※CDK提供のSingletonFunctionは、挙動はいわゆるSingletonパターンと同じ ですが内部の実装がCDK⾊強いため、今回は⾃前のSingleton実装を出します。 ※参考: 『AWS CDK コンストラクトライブラリ開発に関する5つのTips』 https://tmokmss.hatenablog.com/entry/20220517/1652755027
  48. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ⑤Singletonパターン 【CDKクラス図】
  49. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ⑤Singletonパターン 【⽐較】
  50. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ⑤Singletonパターン 【コード例】 constructorがprivateなのでクラス内部からしか呼び出せない ① ①
  51. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ⑤Singletonパターン 【コード例】
  52. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ⑤Singletonパターン https://github.com/aws/aws-cdk/blob/a2c633f1e698249496f11338312ab42bd7b1e4f0/packages/aws-cdk-lib/aws-lambda/lib/singleton-lambda.ts 【参考: SingletonFunctionコード】
  53. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. GoFデザインパターン × AWS CDK実例集 ①Compositeパターン ②Facadeパターン ③Adapterパターン ④Decoratorパターン ⑤Singletonパターン ⑥ ⑦ ⑧ ⑨ ⑩
  54. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. ⑥Strategyパターン
  55. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ⑥Strategyパターン • クライアントと独⽴してアルゴリズムを定義し、かつクライアントが 意識せず動的に振る舞いを変更可能にする⼿法 § 戦略(アルゴリズム)/型(クラス)ごとの分岐処理いらず
  56. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ⑥Strategyパターン 【CDKでのシチュエーション】 【使⽤例】 § CDKで提供されるValidation機能の内部実装において使⽤されている
  57. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ⑥Strategyパターン 【addValidation / IValidation】 ※https://docs.aws.amazon.com/cdk/api/v2/docs/constructs.Node.html#addwbrvalidationvalidation addValidationによりバリデーションルール(IValidation)を登録し、 cdk synth時にvalidationメソッドが発⽕しバリデーションが⾛る
  58. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ⑥Strategyパターン 【addValidation / IValidation】 ※https://docs.aws.amazon.com/cdk/api/v2/docs/constructs.IValidation.html validate()というメソッドを 実装すれば良い
  59. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ⑥Strategyパターン 【CDKクラス図】
  60. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ⑥Strategyパターン 【CDKクラス図 簡略版】
  61. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ⑥Strategyパターン 【⽐較】
  62. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ⑥Strategyパターン 【コード例】 ① ① addValidationにより登録されたバリデーション処理 (validationメソッド)がcdk synth時に発⽕する
  63. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ⑥Strategyパターン https://github.com/aws/constructs/blob/10.x/src/construct.ts 【validateコード】 IValidationというinterfaceであればvalidate()を持っているので _validationsの各要素が何の実クラスなのか気にしなくて良い。 validate()の中⾝がどんな処理かも知らずに実⾏できる。 = ポリモーフィズム
  64. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. GoFデザインパターン × AWS CDK実例集 ①Compositeパターン ②Facadeパターン ③Adapterパターン ④Decoratorパターン ⑤Singletonパターン ⑥Strategyパターン ⑦ ⑧ ⑨ ⑩
  65. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. ⑦Template Methodパターン
  66. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ⑦Template Methodパターン • 処理の⼤枠の⼿順を組んでおき、各ステップの具体的な内容は サブクラスで実装する⼿法
  67. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ⑦Template Methodパターン 【CDKでのシチュエーション】 § Validation機能(⑥Strategyパターン参照)において、各環境(dev | prod)ごとで バリデーションの内容を変えたい 【使⽤例】 § IValidationを実装するValidatorを抽象クラスとし、 そこでバリデーションロジックの⼤枠を組む § それを継承するdev | prod⽤サブクラスを作成し、上記⼤枠の中の 各ステップでのロジックをそれぞれのサブクラスごとで組む
  68. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ⑦Template Methodパターン 【CDKクラス図】
  69. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ⑦Template Methodパターン 【⽐較】
  70. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ⑦Template Methodパターン 【コード例】 ① ① ② ② ③ ③
  71. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ⑦Template Methodパターン 【コード例】
  72. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. GoFデザインパターン × AWS CDK実例集 ①Compositeパターン ②Facadeパターン ③Adapterパターン ④Decoratorパターン ⑤Singletonパターン ⑥Strategyパターン ⑦Template Methodパターン ⑧ ⑨ ⑩
  73. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. ⑧Factory Methodパターン
  74. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ⑧Factory Methodパターン • 操作の中でオブジェクト⽣成部分は抽象的に定義しておき、実際に どのクラスをインスタンス化するかはサブクラスが決める⼿法
  75. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ⑧Factory Methodパターン 【CDKでのシチュエーション】 § あるリソースを⽣成する際に、あらかじめ特定の設定が適⽤されたものが 作られるようにしたい – ④Decoratorパターン章とほぼ同じシチュエーション – Decoratorでは「どのリソースも」、こちらは「あるリソースを」 【使⽤例】 § あるリソースの⽣成処理だけ抽象的に定義しておき、それによって⽣成された オブジェクトに対して特定の設定(RemovalPolicy)を適⽤する処理を定義する § 抽象定義した⽣成部分は、サブクラスで実装する
  76. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ⑧Factory Methodパターン 【CDKクラス図】
  77. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ⑧Factory Methodパターン 【⽐較】
  78. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ⑧Factory Methodパターン 【コード例】 ① ① ② ② ③ ③ 何が作られるかはサブクラス次第だが、作られるものはResource型である。 Resource型はapplyRemovalPolicyを持つので、作られるものが何だろうと実⾏できる
  79. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ⑧Factory Methodパターン 【コード例】
  80. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. GoFデザインパターン × AWS CDK実例集 ①Compositeパターン ②Facadeパターン ③Adapterパターン ④Decoratorパターン ⑤Singletonパターン ⑥Strategyパターン ⑦Template Methodパターン ⑧Factory Methodパターン ⑨ ⑩
  81. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. ⑨Abstract Factoryパターン
  82. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ⑨Abstract Factoryパターン • 特定の組み合わせのオブジェクト群を⽣成するためのインターフェー ス(受け⼝)を提供する⼿法
  83. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ⑨Abstract Factoryパターン 【CDKでのシチュエーション】 § 環境ごとでコンポーネントの組み合わせの違う、環境特有のセットを作りたい 【使⽤例】 § 細かいコンポーネント(App群, Batch群, WAF等)を各Constructで定義する § Dev | Prodでそれぞれ使⽤するコンポーネント(Construct)の組み合わせを さらにConstructとして定義し、環境ごとに個別で構築できるようにする – 例︓Devはアプリだけでいいが、Prodは管理⽤バッチやWAFなどもつけたい
  84. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ⑨Abstract Factoryパターン 【CDKクラス図】
  85. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ⑨Abstract Factoryパターン 【CDKクラス図 簡略版】
  86. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ⑨Abstract Factoryパターン 【⽐較】
  87. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ⑨Abstract Factoryパターン 【コード例】 ① ② ③ ③ ① ②
  88. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ⑨Abstract Factoryパターン 【コード例】 ④ ⑤ ⑥ ⑥ ④ ⑤ AppModule, BatchModule, WafModuleはそれぞれConstruct。 例︓AppModuleにはLambdaがn個、BatchにはLambdaが1個、WAFにはWAFやIPSetなど・・・ ※ProductBもほぼ同様コード
  89. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ⑨Abstract Factoryパターン 【コード例】 これを呼んでconfigを作り、Stack呼び出しで渡す これだけで、dev | prdで違うセットのリソースが作れ、 条件分岐の数も最⼩(余計な分岐が出てこない) さらにパラメータも違うものが作れる(以下参照)
  90. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. GoFデザインパターン × AWS CDK実例集 ①Compositeパターン ②Facadeパターン ③Adapterパターン ④Decoratorパターン ⑤Singletonパターン ⑥Strategyパターン ⑦Template Methodパターン ⑧Factory Methodパターン ⑨Abstract Factoryパターン ⑩
  91. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. ⑩Visitorパターン
  92. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ⑩Visitorパターン • データ構造から操作を分離することで、既存のオブジェクトに対する 新しい操作をデータ構造に変更を加えずに追加できる⼿法
  93. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ⑩Visitorパターン 【CDKでのシチュエーション】 【使⽤例】 § “Aspects employ the visitor pattern.”(※) § スコープ内の全ての要素に操作の適⽤・検証する機能 – コンプライアンス・セキュリティのガードレールなどのために使われることが多い ※https://docs.aws.amazon.com/cdk/v2/guide/aspects.html
  94. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ⑩Visitorパターン 【CDKクラス図】
  95. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ⑩Visitorパターン 【CDKクラス図 簡略版】
  96. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ⑩Visitorパターン 【⽐較】
  97. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ⑩Visitorパターン 【コード例】 ① ① データ構造と操作を分離することが Visitorパターンの本質 データ構造: ResourceやConstruct 構造に対する操作: Aspect(visit)
  98. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ⑩Visitorパターン 【コード例】 ② ③ ② ③ ※これはただのAspectsの使い⽅ (Visitorパターンにあまり関係ない) 例︓Bucketを定義したスタック(③)に対してAspectを当てる
  99. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ⑩Visitorパターン https://github.com/aws/aws-cdk/blob/a2c633f1e698249496f11338312ab42bd7b1e4f0/packages/aws-cdk-lib/core/lib/aspect.ts 【参考: Aspectsコード】 ④ ④
  100. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. ⑩Visitorパターン 【参考: synthesizeコード】 https://github.com/aws/aws-cdk/blob/a2c633f1e698249496f11338312ab42bd7b1e4f0/packages/aws-cdk-lib/core/lib/private/synthesis.ts データ構造に対して⾛査し 分離して定義した操作を当てる =Visitorパターンの本質 再起呼び出し (データ構造の⾛査) visit(操作) Aspectsを取得 してループ
  101. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. • ※補⾜︓よく使われるVisitorパターンのセオリー(以下例)とは細かい実装⽅法が異なる § ①構造⾃体(CDKで⾔うconstruct) がacceptというメソッドを持つ § ②visitor側がデータ構造の各サブクラスごとの型の引数のメソッドを全て⽤意 § ③acceptメソッドの引数でvisitorを渡し、その中でvisitor.visit(this)呼び出し – ⾃分⾃⾝を渡すことで(動的に)メソッド/型の選択を⾏うダブルディスパッチ § ④ObjectStructure(構造保持オブジェクト)により構造を⾛査 (これはデータ構造の種類が限られている前提であり、CDKの様に構造の種類が膨⼤・かつ可変なケースでは現実的に難しい。 そのためCDK(Aspects)ではダブルディスパッチでなく抽象を受け取るvisitメソッドでinstanceofでの分岐が必要になる) ⑩Visitorパターン ② ① ③
  102. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. GoFデザインパターン × AWS CDK実例集 ①Compositeパターン ②Facadeパターン ③Adapterパターン ④Decoratorパターン ⑤Singletonパターン ⑥Strategyパターン ⑦Template Methodパターン ⑧Factory Methodパターン ⑨Abstract Factoryパターン ⑩Visitorパターン
  103. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. まとめ
  104. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. まとめ • GoFデザインパターン × AWS CDKの実例紹介 § 10パターン / 全23パターン • 本セッションの⽬的 § AWS CDKを通してGoFデザインパターンを学ぶ § AWS CDKコードを効率的にする AWSユーザに馴染み深いAWS CDKを通して GoFデザインパターンを学ぼう︕
  105. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 参考︓ GitHub(参考コード・クラス図) • 本セッションで挙げたCDKコード実例・クラス図など全てGitHubにて 公開しています。 – https://github.com/go-to-k/cdk-gof-design-pattern
  106. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 宣伝︓⾃作AWSツールOSS • delstack: AWS CloudFormationスタック強制削除ツール – https://go-to-k.hatenablog.com/entry/delstack • cls3: S3バケット⾼速削除・空にするツール(バージョニング対応) – https://go-to-k.hatenablog.com/entry/cls3 • lamver: Lambdaランタイム/バージョン検索ツール(リージョン横断) – https://go-to-k.hatenablog.com/entry/lamver
  107. © 2023, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Thank you! © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 後藤 健太 Twitter: @365_step_tech