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
Strategyパターン
Search
Hank Ehly
June 29, 2022
Technology
520
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Strategyパターン
https://qiita.com/hankehly
Hank Ehly
June 29, 2022
More Decks by Hank Ehly
See All by Hank Ehly
Fivetranでデータ移動を自動化する
hankehly
0
660
Celeryの紹介と本番運用のTips
hankehly
0
1.7k
ChatGPTを活用した 便利ツールの紹介
hankehly
1
1.4k
Efficient Energy Analytics with Airflow, Spark, and MLFlow
hankehly
0
410
Deferrable Operators入門
hankehly
0
770
【初心者/ハンズオン】Dockerコンテナの基礎知識
hankehly
0
610
Compositeパターン: オブジェクトの階層関係をエレガントに表現する方法
hankehly
0
360
10/29 Airflowの基礎を学ぶハンズオンワークショップ
hankehly
0
320
システム/データ品質保証のための Airflow 活用法
hankehly
0
690
Other Decks in Technology
See All in Technology
Eight Engineering Unit 紹介資料
sansan33
PRO
3
8.1k
トヨタ⽣産⽅式(TPS)⼊⾨
recruitengineers
PRO
3
660
MIRU 2026 チュートリアル
keisuke198619
0
870
モバイルアプリ開発概論2026
recruitengineers
PRO
5
510
Sansan Engineering Unit 紹介資料
sansan33
PRO
1
4.9k
【CEDEC2026】次世代デジタルカードゲームのサーバー設計と運用 〜『Shadowverse: Worlds Beyond』の舞台裏~
cygames
PRO
1
980
制約理論(ToC)入門 2026版
recruitengineers
PRO
7
2.1k
クラウドセキュリティ入門 ~安全なクラウド利用のための基礎知識~
lhazy
12
8.4k
サイバー捜査員研修(前半)
nomizone
1
1.9k
Goでデータパイプラインを作ろう
sansantech
PRO
1
360
モノリス Rails でも日中に rails db:migrate を走らせたい! / Daytime rails db:migrate on Monolithic Rails!
euglena1215
4
550
侵入は突然に 〜 IoTマルウェアと悪用される家庭の機器 ~ / When Intrusion Strikes: IoT Malware and the Abuse of Home Devices
nttcom
0
1.6k
Featured
See All Featured
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
2.1k
The Cost Of JavaScript in 2023
addyosmani
55
10k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.8k
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
450
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
650
Faster Mobile Websites
deanohume
310
32k
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
740
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
990
Are puppies a ranking factor?
jonoalderson
1
3.8k
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
400
GraphQLとの向き合い方2022年版
quramy
50
15k
Thoughts on Productivity
jonyablonski
76
5.3k
Transcript
Strategyパターン 2022/06/29
自己紹介 • Hank Ehly (ハンク イーリー) • ENECHANGE株式会社 • qiita.com/hankehly
• connpass.com/user/hankehly • github.com/hankehly
アジェンダ • 概要 • いつ使うか • 構造 • コード例
概要 • 交換可能な「アルゴリズム」を作る • Strategyと呼ぶ • Strategyは「複数あるやり方の中の一つのやり方」 • 例)ファイルをアップロードする機能 ◦
S3 ◦ Google Cloud Storage ◦ ローカルファイルシステム Strategy
概要 • コンテキスト(Context)に Strategy を渡す • コンポジション • コンテキストが Strategy
に処理を委託する class Context: def __init__(self, strategy): self.strategy = strategy def operation(self): self.strategy.operation()
概要 • Strategy は、ランタイムで選択できる if url.startswith("s3://"): context = Context(strategy=S3()) elif
url.startswith("gs://"): context = Context(strategy=GoogleCloudStorage()) else: context = Context(strategy=LocalStorage()) • Contextの中身を修正せずに振る舞いを変えている • Contextを「拡張」している ◦ テスト修正 / デグレ確認が減る
いつ使うか 1. 関連しているアルゴリズムの「やること」が同じで「やり方」だけ違う Random Forest と Deep Learning 2. ディスク容量、実行時間、ネットワーク速度などの考慮
ネットワークが遅い時は、画像の画質を多少落として送信する 3. メソッドの振る舞いを if/else で分岐して実装している時 いくつかの「Strategy」に分ける
構造 • Strategy ◦ 共通のインターフェイス
構造 • Strategy ◦ 共通のインターフェイス • ConcreteStrategy ◦ Strategyの実装
構造 • Strategy ◦ 共通のインターフェイス • ConcreteStrategy ◦ Strategyの実装 •
Context ◦ Strategyを持つ/使用する
class Storage: def upload(self, data, path): pass class S3(Storage): def
upload(self, data, path): print("S3の {path} に {data} をアップロードする ") class Context: def __init__(self, storage: Storage): self.storage = storage def upload(self, data, path): self.storage.upload(data, path) context = Context(storage=S3()) context.upload(b"hello world", "s3://bucket/file.csv") コード例
class Storage: def upload(self, data, path): pass class S3(Storage): def
upload(self, data, path): print("S3の {path} に {data} をアップロードする ") class Context: def __init__(self, storage: Storage): self.storage = storage def upload(self, data, path): self.storage.upload(data, path) context = Context(storage=S3()) context.upload(b"hello world", "s3://bucket/file.csv") コード例
class Storage: def upload(self, data, path): pass class S3(Storage): def
upload(self, data, path): print("S3の {path} に {data} をアップロードする ") class Context: def __init__(self, storage: Storage): self.storage = storage def upload(self, data, path): self.storage.upload(data, path) context = Context(storage=S3()) context.upload(b"hello world", "s3://bucket/file.csv") コード例
コード例 class Storage: def upload(self, data, path): pass class S3(Storage):
def upload(self, data, path): print("S3の {path} に {data} をアップロードする") class Context: def __init__(self, storage: Storage): self.storage = storage def upload(self, data, path): self.storage.upload(data, path) context = Context(storage=S3()) context.upload(b"hello world", "s3://bucket/file.csv")
Qiita ENECHANGE株式会社 ご清聴ありがとうございます