Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Python でファクトリメソッド?
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
kosuke naito
August 03, 2024
Programming
59
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Python でファクトリメソッド?
Python でファクトリメソッド?を使った開発を行なって学んだことを発表しました。
kosuke naito
August 03, 2024
More Decks by kosuke naito
See All by kosuke naito
V が名前に入ってる
kosuke222naito
0
110
Other Decks in Programming
See All in Programming
世界の中心で、AI(App Intents)をさけぶ ー App Intents中心設計の実践ガイド
touyou
0
660
LoopHub - ローカルで動く GitHub で、AI と共同開発
jugyo
1
570
[Rails World 2026] Durable orchestration on Rails: from continuation to workflow
palkan
0
190
JPUG勉強会 OSSデータベースの内部構造を理解しよう(第2回)
oga5
0
270
SREの越境 / SRE Collaboration
y0hgi
2
270
The Good Stuff, Not the Slop: Engineering High-Quality Android Apps with Modern AI Tooling
danybony
1
260
仕様駆動開発による爆速プロダクト開発 / Bakusoku Spec Driven Development
kobakei
0
130
WebMCP Challenge に星空観察アプリで参加した話
okajun35
0
190
巨大モノリシックアプリ モダン化大作戦
ktcryomm
1
1.1k
AgentCore CLI で進化した AWS での AI エージェントの作り方 : 必要な機能を必要な時に
icoxfog417
PRO
3
370
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
260
Agents on Rails - Rails at Scale 2026
irinanazarova
0
200
Featured
See All Featured
SEOcharity - Dark patterns in SEO and UX: How to avoid them and build a more ethical web
sarafernandez
0
290
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.8k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
Amusing Abliteration
ianozsvald
1
310
How to Build an AI Search Optimization Roadmap - Criteria and Steps to Take #SEOIRL
aleyda
1
2.2k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
19k
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
370
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
360
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
Highjacked: Video Game Concept Design
rkendrick25
PRO
1
460
Redefining SEO in the New Era of Traffic Generation
szymonslowik
1
430
The Cost Of JavaScript in 2023
addyosmani
55
10k
Transcript
Python でファクトリメソッド? ナイトウ Press Space for next page 8/5/2024, 6:54:18
AM
このスライドはこちらで https://kosuke222naito.github.io/20240803-lt/
旧 Twitter GitHub Zenn Table of Contents 1. Python でファクトリメソッド?
2. Table of Contents 3. 自己紹介 4. ファクトリメソッド 5. Python での例 6. ファクトリメソッドを採用した経緯 7. 実際の使用例(チャットbot) 8. ファクトリメソッドを採用してみての感想 9. まとめ
旧 Twitter GitHub Zenn I'm ナイトウ Python で Web バックエンド
Web フロントもやりたい Vue が好き since: 2021 (4年目) 駆け出しエンジニア? (非 CS 専攻) 柏 (東葛dev 運営?) 自作キーボード 新本格ミステリ エヴァ、シュタゲ 元ハロヲタ (until: 2022)
ファクトリメソッド
ファクトリメソッド #とは 他のクラスのコンストラクタをサブクラスで上書き可能な自分のメソッドに置き換えることで、 アプリケーションに特化したオブジェクトの生成をサブクラスに追い出し、クラスの再利用性を高めることを目的とする Factory Method パターン - Wikipedia クラス図
Animal +String name +String sound() Dog +String name +String sound() : "bark" Cat +String name +String sound() : "meow" AnimalFactory +createAnimal(type: String) : Animal
ファクトリメソッドのいいところ 1. 再利用性の向上 2. 変更の容易さ 3. 可読性の向上
Python での例 1 from abc import ABC, abstractmethod 2 3
class Animal(ABC): 4 @abstractmethod 5 def __init__(self, name): 6 self.name = name 7 8 @abstractmethod 9 def speak(self): 10 pass 11 12 class Dog(Animal): 13 def __init__(self, name): 14 super().__init__(name) 15 16 def speak(self): 17 return "Woof!" 18 19 class Cat(Animal): 20 def __init__(self, name): 21 super().__init__(name) 22 23 def speak(self): 24 return "Meow!" 37 dog = AnimalFactory.create("dog", "Buddy") 38 cat = AnimalFactory.create("cat", "Whiskers") 26 class AnimalFactory: 27 @staticmethod 28 def create(animal_type, name): 29 if animal_type == "dog": 30 return Dog(name) 31 elif animal_type == "cat": 32 return Cat(name) 33 else: 34 raise ValueError("Unknown type") 35 36 39 40 print(dog.speak()) # "Woof!" 41 print(cat.speak()) # "Meow!"
None
ファクトリメソッドを採用した経緯 現在チャットbotを利用したアプリを作成中(PoC) 外部のAPIサービスを利用して実現する必要がある しかしサービス選定の時間はない そこで利用するサービスに依存せず コードの変更を簡単にしたい
実際の使用例(チャットbot) インターフェース(抽象基底クラス)の定義 10 class ChatBotServices(Enum): 11 OPENAI = "openai" 1
from abc import ABC, abstractmethod 2 from enum import Enum 3 4 class ChatBot(ABC): 5 @abstractmethod 6 def genrerate_response(self, messages) -> str: 7 pass 8 9
実際の使用例(チャットbot) 具象クラスの作成(OpenAI API) 7 def genrerate_response(self, messages) -> str: 8
completion = self.chatgpt_client.chat.completions.create( 9 messages=messages, 10 model="gpt-4o", 11 ) 12 content = completion.choices[0].message.content 13 14 return content 1 from openai import OpenAI 2 3 class OpenAIChatBot(ChatBot): 4 def __init__(self): 5 self.chatgpt_client = OpenAI(api_key=OPEN_AI_API_KEY) 6
実際の使用例(チャットbot) ファクトリ https://zenn.dev/miyaji26/articles/fe4a50319ed799 17 ChatBotFactory.register(ChatBotServices.OPENAI)(OpenAIChatBot) 1 class ChatBotFactory: 2 _class:
dict[str, type[ChatBot]] = {} 3 4 @classmethod 5 def register(cls, chat_bot_service: ChatBotServices): 6 def wrapper(cls_): 7 cls._class[chat_bot_service.value] = cls_ 8 return cls._class[chat_bot_service.value] 9 10 return wrapper 11 12 @classmethod 13 def get_chat_bot_class(cls, chat_bot_service: ChatBotServices): 14 return cls._class[chat_bot_service.value] 15 16
実際の使用例(チャットbot) デコレータとして from openai import OpenAI class OpenAIChatBot(ChatBot): def __init__(self):
self.chatgpt_client = OpenAI(api_key=OPEN_AI_API_KEY) def genrerate_response(self, messages) -> str: completion = self.chatgpt_client.chat.completions.create( messages=messages, model="gpt-4o", ) content = completion.choices[0].message.content return content
実際の使用例(チャットbot) ファクトリでインスタンスを作成 1 ChatBotClass = ChatBotFactory.get_chat_bot_class(ChatBotServices.OPENAI) 2 chat_bot = ChatBotClass()
これがやりたかった 柔軟に使うサービスを選択できる 4 ChatBotFactory.register(ChatBotServices.MICROSOFT_BING)(MicrosoftBingChatBot) 1 ChatBotFactory.register(ChatBotServices.OPENAI)(OpenAIChatBot) 2 ChatBotFactory.register(ChatBotServices.CLAUDE)(ClaudeChatBot) 3 ChatBotFactory.register(ChatBotServices.GOOGLE_GEMINI)(GoogleGeminiChatBot)
ChatBotClass = ChatBotFactory.get_chat_bot_class(ChatBotServices.OPENAI) chat_bot = ChatBotClass() chat_bot_response = chat_bot.generate_response(messages) 1 2 3
ファクトリメソッドのあんまりよくないところ 使わない場合に比べてコードが複雑(難解)になる
補うために ドキュメントを作成 ペアプロ ドキュメントレビューでメンバーを巻き込む 1 # chat-app 2 3 ##
基本設計方針 4 5 利用する外部APIがまだ決まっていないのでファクトリメソッドに 6 抽象クラス(インタフェース)を各サービスの外部APIを用いて実装 7 呼び出し元でどのサービスを利用するかを決める。 8 9 ## ディレクトリ構成 10 11 `tree.txt` を参照 12 13 ### `services` ディレクトリ 14 15 ビジネスロジックなどを担う 16 17 - `chat` 18 19 - AI チャットサービス(Open AIなど)を利用したチャット生 20 - ChatGPT が苦手な最新情報(天気情報など)の取得を別の外部 21 22 - `geocoding` 23 24 - ジオコーディング処理を担う 25 - あくまでも関数としてのふるまいまでにとどめる
None
ファクトリメソッドを採用してみての感想 😇 過剰だったかもしれない 📕 ドキュメントの作成を丁寧に行う必要があった 🙋♂️ この作りにしたおかげで他メンバーへの引き継ぎスムーズにできた 🤔 コンストラクタで異なる引数を受け取る場合 🤫
Python の理解も足りてない 😵💫 果たしてこれは本当にファクトリメソッドだったのか
まとめ ファクトリメソッド: オブジェクトの生成をサブクラスに追い出し、クラスの再利用性を高める ファクトリメソッドを使うべき状況 オブジェクトの生成方法が複雑な場合 生成するオブジェクトの種類が増減する可能性がある場合 生成ロジックをカプセル化したい場合