AWS Dev Day 2023 Tokyo登壇資料です。
© 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 3A W S D E V D A Y 2 0 2 3 T O K Y OB - 1
View Slide
© 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歩のテック)
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.アジェンダ• GoFデザインパターンとは• 本セッションの⽬的と注意• GoFデザインパターン × AWS CDK実例集• まとめ
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.GoFデザインパターンとは
© 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) の内部実装にも⼀部⽤いられている※『オブジェクト指向における再利⽤のためのデザインパターン』 (ソフトバンクパブリッシング)著: エーリヒ・ガンマ、ラルフ・ジョンソン、リチャード・ヘルム、ジョン・ブリシディース監訳: 本位⽥真⼀, 吉⽥和樹
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.© 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デザインパターンを学ぶ• AWS CDKコードを効率的にする※もちろんCDKコードの効率性に繋がる点もたくさんありますが、あくまで本セッションではそれらを推奨・⽬的とするものではありません。AWSユーザに馴染み深いAWS CDKを通してGoFデザインパターンを学ぼう︕
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.注意• 本来のGoFデザインパターンと表現が多少異なる場合があります。§ CDKコードにうまく適⽤するため• CDKコードを書く上で、通常と⽐べて冗⻑になる場合があります。§ GoFデザインパターンを適⽤・説明しやすくするため
© 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実例集
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.GoFデザインパターン × AWS CDK実例集①②③④⑤⑥⑦⑧⑨⑩
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.①Compositeパターン
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.①Compositeパターン• ⽊構造を伴う再起的なデータ構造を利⽤した⼿法§ 容器と中⾝を同⼀視することで再起的な構造の⾛査が可能に
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.①Compositeパターン【CDKでのシチュエーション】§ 特定のリソース群に対して⼀元的にパラメータ設定などの操作を適⽤したい【使⽤例】§ AWS CDKの特徴であるリソースのツリー構造を利⽤して、特定のコンストラクト内の全リソース、またはスタック内のすべてのリソースに対して⼀括で「RemovalPolicy」パラメータを指定する
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.①Compositeパターン【CDKクラス図】
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.①Compositeパターン【CDKクラス図 簡略版】
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.①Compositeパターン【⽐較】
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.①Compositeパターン【コード例】
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.①Compositeパターン【コード例】→ResourceならRemovalPolicy適⽤→Constructなら、さらにその⼦供達に対して同メソッドを再起呼び出し※applyRemovalPolicy: Resourceに定義されたRemovalPolicy適⽤メソッド※RetainConstruct:Retainにしたいリソースを集めた⾃作Construct
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.GoFデザインパターン × AWS CDK実例集①Compositeパターン②③④⑤⑥⑦⑧⑨⑩
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.②Facadeパターン
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.②Facadeパターン• 複数のクラス群を組み合わせて⾏う複雑な⼿順を、⼀つの窓⼝を設けることで、それを呼び出すだけで簡単に⾏えるようにする⼿法
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.②Facadeパターン【CDKでのシチュエーション】§ 複雑なリソース構築をうまくまとめてシンプルに呼び出したい【使⽤例】§ Constructを作成してその中に⼀連のリソース定義をまとめて定義し、Stackからはそれを呼び出すだけでシンプルなリソース構築を⾏う
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.②Facadeパターン【CDKクラス図】
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.②Facadeパターン【⽐較】
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.②Facadeパターン【コード例】Facade≒Constructみたいなもの①①② ③②③
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.GoFデザインパターン × AWS CDK実例集①Compositeパターン②Facadeパターン③④⑤⑥⑦⑧⑨⑩
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.③Adapterパターン
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.③Adapterパターン• 既存のクラスに⼿を加えられない時に、そのクラスを変更することなく機能拡張をするための⼿法§ ライブラリの拡張など後⽅互換性を損ないたくないケースで有効
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.③Adapterパターン• 継承でなく委譲(コンポジションを利⽤した技法)する形も可§ AdapterがAdapteeをプロパティとして持ち、Adapterのメソッド内部でAdapteeのメソッドを呼び出す• 本セッション例ではCDKとの相性の理由で継承を使⽤しているが、本来は継承より委譲の⽅が望ましい§ “クラス継承よりもオブジェクトコンポジションを多⽤すること”– GoF本 第1章『概論』より
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.③Adapterパターン【CDKでのシチュエーション】§ CDK提供のリソース定義(BucketやQueue等)に、新たなメソッドを追加したい【使⽤例】§ あるリソースに複数のリソースポリシーを⼀度の呼び出しで⼀気に付けたいが、単⼀ポリシーを付けるメソッド(addToResourcePolicy)しか⽤意されていない§ そこで、それらのリソースクラスを拡張する新たなクラスを作り、そのクラスに⾃作のメソッド(addManyToResourcePolicy)を定義する
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.③Adapterパターン【CDKクラス図】
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.③Adapterパターン【⽐較】
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.③Adapterパターン【コード例】※Bucketをextendsしているので、⾃⾝(this)がaddToResourcePolicyメソッドを持っている →それをループで回すことで⼀気にポリシーをアタッチするように⾒せる①②①②
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.③Adapterパターン【コード例】
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.GoFデザインパターン × AWS CDK実例集①Compositeパターン②Facadeパターン③Adapterパターン④⑤⑥⑦⑧⑨⑩
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.④Decoratorパターン
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.④Decoratorパターン• 基となるオブジェクトに動的に飾りつけを重ねていき、柔軟に機能の拡張を⾏うための⼿法§ オブジェクトをラップすることで、飾りつけ(機能追加)が可能に
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.④Decoratorパターン【CDKでのシチュエーション】§ リソースを作成するときに、あらかじめ特定の設定が適⽤されたものが作られるようにしたい【使⽤例】§ リソースを作成する際にいちいちapplyRemovalPolicyメソッドを呼び出さずにRemovalPolicyが適⽤できるようなラップクラスを定義する
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.④Decoratorパターン【CDKクラス図】
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.④Decoratorパターン【CDKクラス図 簡略版】
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.④Decoratorパターン【⽐較】
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.④Decoratorパターン【コード例】※若⼲無理やりパターンを実現をしていますconstructor内で、サブクラスのgetOwnRemovalPolicyでRETAIN|DESTROYを呼び、それを⾃⾝のapplyRemovalPolicyに渡している。これによってnewと同時にRemovalPolicyの適⽤ができるようになる。①② ③①② ③
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.④Decoratorパターン【コード例】
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.GoFデザインパターン × AWS CDK実例集①Compositeパターン②Facadeパターン③Adapterパターン④Decoratorパターン⑤⑥⑦⑧⑨⑩
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.⑤Singletonパターン
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.⑤Singletonパターン• あるクラスのコンストラクタが、何度呼び出されても⼀つしかインスタンスが作成されないことを保証する⼿法§ ⼀度作られたインスタンスを使い回す※今はあまり推奨されていないパターン
© 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
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.⑤Singletonパターン【CDKクラス図】
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.⑤Singletonパターン【⽐較】
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.⑤Singletonパターン【コード例】constructorがprivateなのでクラス内部からしか呼び出せない①①
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.⑤Singletonパターン【コード例】
© 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コード】
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.GoFデザインパターン × AWS CDK実例集①Compositeパターン②Facadeパターン③Adapterパターン④Decoratorパターン⑤Singletonパターン⑥⑦⑧⑨⑩
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.⑥Strategyパターン
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.⑥Strategyパターン• クライアントと独⽴してアルゴリズムを定義し、かつクライアントが意識せず動的に振る舞いを変更可能にする⼿法§ 戦略(アルゴリズム)/型(クラス)ごとの分岐処理いらず
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.⑥Strategyパターン【CDKでのシチュエーション】【使⽤例】§ CDKで提供されるValidation機能の内部実装において使⽤されている
© 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#addwbrvalidationvalidationaddValidationによりバリデーションルール(IValidation)を登録し、cdk synth時にvalidationメソッドが発⽕しバリデーションが⾛る
© 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.htmlvalidate()というメソッドを実装すれば良い
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.⑥Strategyパターン【CDKクラス図】
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.⑥Strategyパターン【CDKクラス図 簡略版】
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.⑥Strategyパターン【⽐較】
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.⑥Strategyパターン【コード例】①①addValidationにより登録されたバリデーション処理(validationメソッド)がcdk synth時に発⽕する
© 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()の中⾝がどんな処理かも知らずに実⾏できる。= ポリモーフィズム
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.GoFデザインパターン × AWS CDK実例集①Compositeパターン②Facadeパターン③Adapterパターン④Decoratorパターン⑤Singletonパターン⑥Strategyパターン⑦⑧⑨⑩
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.⑦Template Methodパターン
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.⑦Template Methodパターン• 処理の⼤枠の⼿順を組んでおき、各ステップの具体的な内容はサブクラスで実装する⼿法
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.⑦Template Methodパターン【CDKでのシチュエーション】§ Validation機能(⑥Strategyパターン参照)において、各環境(dev | prod)ごとでバリデーションの内容を変えたい【使⽤例】§ IValidationを実装するValidatorを抽象クラスとし、そこでバリデーションロジックの⼤枠を組む§ それを継承するdev | prod⽤サブクラスを作成し、上記⼤枠の中の各ステップでのロジックをそれぞれのサブクラスごとで組む
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.⑦Template Methodパターン【CDKクラス図】
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.⑦Template Methodパターン【⽐較】
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.⑦Template Methodパターン【コード例】①①②②③③
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.⑦Template Methodパターン【コード例】
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.GoFデザインパターン × AWS CDK実例集①Compositeパターン②Facadeパターン③Adapterパターン④Decoratorパターン⑤Singletonパターン⑥Strategyパターン⑦Template Methodパターン⑧⑨⑩
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.⑧Factory Methodパターン
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.⑧Factory Methodパターン• 操作の中でオブジェクト⽣成部分は抽象的に定義しておき、実際にどのクラスをインスタンス化するかはサブクラスが決める⼿法
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.⑧Factory Methodパターン【CDKでのシチュエーション】§ あるリソースを⽣成する際に、あらかじめ特定の設定が適⽤されたものが作られるようにしたい– ④Decoratorパターン章とほぼ同じシチュエーション– Decoratorでは「どのリソースも」、こちらは「あるリソースを」【使⽤例】§ あるリソースの⽣成処理だけ抽象的に定義しておき、それによって⽣成されたオブジェクトに対して特定の設定(RemovalPolicy)を適⽤する処理を定義する§ 抽象定義した⽣成部分は、サブクラスで実装する
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.⑧Factory Methodパターン【CDKクラス図】
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.⑧Factory Methodパターン【⽐較】
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.⑧Factory Methodパターン【コード例】①①②②③③何が作られるかはサブクラス次第だが、作られるものはResource型である。Resource型はapplyRemovalPolicyを持つので、作られるものが何だろうと実⾏できる
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.⑧Factory Methodパターン【コード例】
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.GoFデザインパターン × AWS CDK実例集①Compositeパターン②Facadeパターン③Adapterパターン④Decoratorパターン⑤Singletonパターン⑥Strategyパターン⑦Template Methodパターン⑧Factory Methodパターン⑨⑩
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.⑨Abstract Factoryパターン
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.⑨Abstract Factoryパターン• 特定の組み合わせのオブジェクト群を⽣成するためのインターフェース(受け⼝)を提供する⼿法
© 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などもつけたい
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.⑨Abstract Factoryパターン【CDKクラス図】
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.⑨Abstract Factoryパターン【CDKクラス図 簡略版】
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.⑨Abstract Factoryパターン【⽐較】
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.⑨Abstract Factoryパターン【コード例】①② ③③①②
© 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もほぼ同様コード
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.⑨Abstract Factoryパターン【コード例】これを呼んでconfigを作り、Stack呼び出しで渡すこれだけで、dev | prdで違うセットのリソースが作れ、条件分岐の数も最⼩(余計な分岐が出てこない)さらにパラメータも違うものが作れる(以下参照)
© 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パターン⑩
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.⑩Visitorパターン
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.⑩Visitorパターン• データ構造から操作を分離することで、既存のオブジェクトに対する新しい操作をデータ構造に変更を加えずに追加できる⼿法
© 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
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.⑩Visitorパターン【CDKクラス図】
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.⑩Visitorパターン【CDKクラス図 簡略版】
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.⑩Visitorパターン【⽐較】
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.⑩Visitorパターン【コード例】①①データ構造と操作を分離することがVisitorパターンの本質データ構造: ResourceやConstruct構造に対する操作: Aspect(visit)
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.⑩Visitorパターン【コード例】② ③②③※これはただのAspectsの使い⽅ (Visitorパターンにあまり関係ない)例︓Bucketを定義したスタック(③)に対してAspectを当てる
© 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コード】④④
© 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を取得してループ
© 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パターン② ①③
© 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パターン
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.© 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パターン / 全23パターン• 本セッションの⽬的§ AWS CDKを通してGoFデザインパターンを学ぶ§ AWS CDKコードを効率的にするAWSユーザに馴染み深いAWS CDKを通してGoFデザインパターンを学ぼう︕
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.参考︓ GitHub(参考コード・クラス図)• 本セッションで挙げたCDKコード実例・クラス図など全てGitHubにて公開しています。– https://github.com/go-to-k/cdk-gof-design-pattern
© 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
© 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