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
650
Celeryの紹介と本番運用のTips
hankehly
0
1.6k
ChatGPTを活用した 便利ツールの紹介
hankehly
1
1.4k
Efficient Energy Analytics with Airflow, Spark, and MLFlow
hankehly
0
400
Deferrable Operators入門
hankehly
0
760
【初心者/ハンズオン】Dockerコンテナの基礎知識
hankehly
0
590
Compositeパターン: オブジェクトの階層関係をエレガントに表現する方法
hankehly
0
350
10/29 Airflowの基礎を学ぶハンズオンワークショップ
hankehly
0
310
システム/データ品質保証のための Airflow 活用法
hankehly
0
680
Other Decks in Technology
See All in Technology
Lightning近況報告
kozy4324
0
210
iAEONの段階的リアーキテクト戦略 / iAEON's_Gradual_Re-architecture_Strategy
aeonpeople
0
230
SONiCで構築・運用する生成AI向けパブリッククラウドネットワーク ~実装編~
sonic
0
290
2026 TECHFRESH 畢業分享會 - AI-Native 重塑軟體工程與虛擬講師
line_developers_tw
PRO
0
1.3k
2026TECHFRESH畢業分享會 - 葬送的通靈師:化系統與用戶雜訊成行動訊號
line_developers_tw
PRO
0
1.3k
LayerX コーポレートエンジニアリング室におけるサプライチェーンセキュリティへの取り組み / Supply Chain Security at LayerX Corporate Engineering
yuyatakeyama
2
690
SteampipeとExcel Power QueryでAWS構成定義書の作成を自動化する
jhashimoto
0
160
Oracle AI Database@Azure:サービス概要のご紹介
oracle4engineer
PRO
6
2k
人材育成分科会.pdf
_awache
4
300
ロボティクスの技術 / Robotics Technology
ks91
PRO
0
110
200個のGitHubリポジトリを横断調査したかった
icck
0
140
インシデントレスポンス演習 I / Incident Response Exercise I
ks91
PRO
0
100
Featured
See All Featured
16th Malabo Montpellier Forum Presentation
akademiya2063
PRO
0
150
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
360
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
2
860
The Mindset for Success: Future Career Progression
greggifford
PRO
0
360
Building a Scalable Design System with Sketch
lauravandoore
463
34k
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
310
Leo the Paperboy
mayatellez
7
1.8k
Statistics for Hackers
jakevdp
799
230k
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.6k
Design of three-dimensional binary manipulators for pick-and-place task avoiding obstacles (IECON2024)
konakalab
0
460
SEOcharity - Dark patterns in SEO and UX: How to avoid them and build a more ethical web
sarafernandez
0
210
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
19k
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株式会社 ご清聴ありがとうございます