js |
JavaScriptタグが付けられた新着投稿 - Qiita |
【個人開発 #1】初学者がrailsでTODOアプリを作ってみる~要件定義、基本設計~ |
https://qiita.com/void_takazu/items/8e17330da17955ca2906
|
rails |
2023-03-13 04:27:20 |
Ruby |
Railsタグが付けられた新着投稿 - Qiita |
【個人開発 #1】初学者がrailsでTODOアプリを作ってみる~要件定義、基本設計~ |
https://qiita.com/void_takazu/items/8e17330da17955ca2906
|
rails |
2023-03-13 04:27:20 |
海外TECH |
MakeUseOf |
6 Ways to Use Facebook for Career Growth |
https://www.makeuseof.com/use-facebook-career-growth/
|
growth |
2023-03-12 19:45:16 |
海外TECH |
MakeUseOf |
How to Fix the "No Speaker or Headphones are Plugged In" Error on Windows |
https://www.makeuseof.com/no-speaker-headphones-plugged-in-windows/
|
How to Fix the amp quot No Speaker or Headphones are Plugged In amp quot Error on WindowsSometimes Windows will believe there are no audio devices plugged into the PC when there very clearly are Here s how to fix this annoying issue |
2023-03-12 19:15:16 |
海外TECH |
DEV Community |
Become a Python Design Strategist using the Strategy Pattern |
https://dev.to/fayomihorace/become-a-python-design-strategist-using-the-strategy-pattern-6ad
|
Become a Python Design Strategist using the Strategy PatternHey Python Lover today you ll become a Python strategist because we re going to illustrate with python a design pattern called the strategy pattern The strategy pattern is a behavioral pattern that allows you to define a family of algorithms or a family of functions and encapsulate them as objects to make them interchangeable It helps having a code easy to change and then to maintain Let s imagine that you re building a food delivery application and you want to support many restaurants But each restaurant has its own API and you have to interact with each restaurant s API to perform diverse actions such as getting the menu ordering food or checking the status of an order That means we need to write custom code for each restaurant First let s see how we can implement this using a simple and naive approach without bothering to think about the design or the architecture of our code from enum import Enumclass Restaurant Enum GERANIUM Geranium Copenhagen ATOMIX ATOMIX New York LE CLARENCE Le Clarence Paris France class Food Enum SHANGHAI DUMPLINGS Shanghai dumplings COQ AU VIN Coq au Vin CHEESEBURGER FRIES Cheeseburger fries CHAWARMA Chawarma NAZI GORENG Nasi goreng BIBIMBAP Bibimbap restaurants map foods Restaurant GERANIUM Food BIBIMBAP Food SHANGHAI DUMPLINGS Food COQ AU VIN Restaurant ATOMIX Food CHEESEBURGER FRIES Food CHAWARMA Food NAZI GORENG Restaurant LE CLARENCE Food COQ AU VIN Food BIBIMBAP def get restaurant food menu restaurant Restaurant gt list Food Get the list of food available for a given restaurant if restaurant Restaurant ATOMIX print f call ATOMIX API to get it available food menu elif restaurant Restaurant LE CLARENCE print f call LE CLARENCE API to get it available food menu elif restaurant Restaurant GERANIUM print f call GERANIUM API to get it available food menu return restaurants map foods restaurant def order food restaurant Restaurant food Food gt int Order food from a restaurant returns A integer representing the order ID if restaurant Restaurant ATOMIX print f send notification to ATOMIX restaurant API to order food elif restaurant Restaurant LE CLARENCE print f send notification to LE CLARENCE restaurant API to order food elif restaurant Restaurant GERANIUM print f send notification to GERANIUM restaurant API to order food order id Supposed to be retrieved from the right restaurant API call return order iddef check food order status restaurant Restaurant order id int gt bool Check of the food is ready for delivery returns True if the food is ready for delivery and False otherwise if restaurant Restaurant ATOMIX print f call ATOMIX API to check order status order id elif restaurant Restaurant LE CLARENCE print f call LE CLARENCE API to check order status order id elif restaurant Restaurant GERANIUM print f call GERANIUM API to check order status order id food is ready True Supposed to be retrieved from the right restaurant API call return food is readyif name main menu get restaurant food menu Restaurant ATOMIX print menu menu order food Restaurant ATOMIX menu food is ready check food order status Restaurant ATOMIX menu print food is ready food is ready We have some Enum classes to keep information about the restaurants and Foods and we have functions get restaurant food menu to get the menu of a restaurantorder food to order the food in a given restaurantcheck food order status to check if the food is ready In the real world we could create a task that will call that method each min or any other timeframe periodically to check if the food is ready to be delivered and let the customer knows And for simplicity reasons we have just printed what the code is supposed to do rather than real API calls The code works well and you should see this output call ATOMIX API to get it available food menu menu lt Food CHEESEBURGER FRIES Cheeseburger fries gt lt Food CHAWARMA Chawarma gt lt Food NAZI GORENG Nasi goreng gt send notification to ATOMIX restaurant API to order Food CHEESEBURGER FRIES call ATOMIX API to check order status Food CHEESEBURGER FRIES food is ready True But what is the problem with that implementation There are mainly problems here The logic related to each restaurant is scattered So to add or modify the code related to a restaurant we need to update all functions That s really bad Each of these functions contains too much codeRemember I have simplified the code to just print what the code is supposed to do but for each restaurant we have to write the code to handle the feature like calling the right API handling errors etc Try to imagine the size of each of these functions if there were restaurants And here is where the Strategy pattern enters the game Each time someone orders food from a given restaurant we can consider that he is performing the action of ordering a food using a given strategy And the strategy here is the restaurant So each strategy and all the functions related to it which constitute its family will be encapsulated into a class Let s do that First we create our restaurant strategy class interface or abstract class Let s name it RestaurantManager from abc import ABC abstractmethodclass RestaurantManager ABC Restaurants manager base class restaurant Restaurant None abstractmethod def get food menu self gt list Food Get the list of food available for a given restaurant pass abstractmethod def order food self food Food gt int Order food from a restaurant returns A integer representing the order ID pass abstractmethod def check food order status self order id int gt bool Check of the food is ready for delivery returns True if the food is ready for delivery and False otherwise passNow each restaurant code will be grouped inside its own class which just inherits from the base RestaurantManager and should implement all the required methods And our business class doesn t care which restaurant or I mean which strategy he is implementing it just performs the needed action And to create a strategy for a restaurant we just have to create a subclass of RestaurantManager Here is the code for that ATOMIX restaurant class AtomixRestaurantManager RestaurantManager ATOMIX Restaurant Manager restaurant Restaurant Restaurant ATOMIX def get food menu self gt list Food print f call ATOMIX API to get it available food menu return restaurants map foods self restaurant def order food self food Food gt int print f send notification to ATOMIX API to order food order id Supposed to be retrieved from the right restaurant API call return order id def check food order status self order id int gt bool print f call ATOMIX API to check order status order id food is ready True Supposed to be retrieved from the right restaurant API call return food is readyAnd we can add a business logic class that receives the strategy the restaurant class FoodOrderProcessor def init self restaurant manager RestaurantManager self restaurant manager restaurant manager def get food menu self return self restaurant manager get food menu def order food self food Food gt int return self restaurant manager order food food def check food order status self order id int gt bool return self restaurant manager check food order status order id And here is our new main code if name main order processor FoodOrderProcessor restaurant manager AtomixRestaurantManager menu order processor get food menu print menu menu order processor order food menu food is ready order processor check food order status menu print food is ready food is ready You can test it it should still work Now it s easy to add a new restaurant or a new strategy Let s add for geranium restaurant class GeraniumRestaurantManager RestaurantManager Geranium Restaurant Manager restaurant Restaurant Restaurant GERANIUM def get food menu self gt list Food print f call GERANIUM API to get it available food menu return restaurants map foods self restaurant def order food self food Food gt int print f send notification to GERANIUM API to order food order id Supposed to be retrieved from the right restaurant API call return order id def check food order status self order id int gt bool print f call GERANIUM API to check order status order id food is ready True Supposed to be retrieved from the right restaurant API call return food is readyAnd to change the strategy we just have to replace the restaurant manager attribute of our business class with GeraniumRestaurantManager instead of AtomixRestaurantManager previously and the remaining code is the same if name main order processor FoodOrderProcessor restaurant manager GeraniumRestaurantManager Remaining codeAnd that s it We ve successfully implemented the strategy pattern in Python to create interchangeable restaurant gateways for our delivery product Congratulations you re now a Python strategist I hope you found this explanation of the strategy pattern helpful Remember the strategy pattern is just one of many design patterns that can make your code more maintainable A video version of this tutorial will be available soon here Feel free to check it out and I see you in the next post Take care |
2023-03-12 19:16:37 |
ニュース |
@日本経済新聞 電子版 |
DeNA、「脱ゲーム依存」の難路 PBRは1倍割れ
https://t.co/0Yej7nMfNQ |
https://twitter.com/nikkei/statuses/1634997189113151488
|
難路 |
2023-03-12 19:18:01 |
ニュース |
@日本経済新聞 電子版 |
習氏の統制、新興製造業まで 挙国体制が招く外国人売り
https://t.co/x7X9TSh1ZD |
https://twitter.com/nikkei/statuses/1634993144352874497
|
製造業 |
2023-03-12 19:01:56 |
ニュース |
BBC News - Home |
Gary Lineker: More BBC sport shows cancelled by presenter boycott |
https://www.bbc.co.uk/news/uk-64930957?at_medium=RSS&at_campaign=KARANGA
|
boycottfootball |
2023-03-12 19:42:16 |
ニュース |
BBC News - Home |
Silicon Valley Bank: Offer made for UK arm of failed US lender |
https://www.bbc.co.uk/news/business-64934348?at_medium=RSS&at_campaign=KARANGA
|
valley |
2023-03-12 19:50:24 |
ニュース |
BBC News - Home |
San Diego: Eight dead after boats, possibly used for people smuggling, capsize |
https://www.bbc.co.uk/news/world-us-canada-64932817?at_medium=RSS&at_campaign=KARANGA
|
california |
2023-03-12 19:26:27 |
ニュース |
BBC News - Home |
Newcastle 2-1 Wolves: Alexander Isak and Miguel Almiron goals earn home victory |
https://www.bbc.co.uk/sport/football/64852077?at_medium=RSS&at_campaign=KARANGA
|
Newcastle Wolves Alexander Isak and Miguel Almiron goals earn home victoryMiguel Almiron and Alexander Isak score as Newcastle beat Wolves to revive their hopes of finishing in the Premier League s top four |
2023-03-12 19:18:36 |
ビジネス |
ダイヤモンド・オンライン - 新着記事 |
漫画『アオアシ』に学ぶビジネスの「俯瞰力」、名経営者も“左サイドバック”型が多い!?【入山章栄・動画】 - 入山章栄の世界標準の経営理論 |
https://diamond.jp/articles/-/318741
|
世界標準 |
2023-03-13 04:50:00 |
ビジネス |
ダイヤモンド・オンライン - 新着記事 |
マンション&戸建ての失敗しない購入術!価格高騰と金利上昇のバブルへの賢い対処法 - 高騰と金利上昇に勝つ!「マンション&戸建て」購入術大全 |
https://diamond.jp/articles/-/319220
|
マンション戸建ての失敗しない購入術価格高騰と金利上昇のバブルへの賢い対処法高騰と金利上昇に勝つ「マンション戸建て」購入術大全多くの人にとって、生涯で最も高額な買い物がマイホームだ。 |
2023-03-13 04:45:00 |
ビジネス |
ダイヤモンド・オンライン - 新着記事 |
東レでまたもや品質不正発覚!生かされない再発防止策と不祥事連発を招く「背信」の正体 - 東レの背信 LEVEL3 |
https://diamond.jp/articles/-/319213
|
|
2023-03-13 04:40:00 |
ビジネス |
ダイヤモンド・オンライン - 新着記事 |
テルモが2割増収!シスメックスも15%超増収で売上高「過去最高」連発、では利益面は? - ダイヤモンド 決算報 |
https://diamond.jp/articles/-/319288
|
テルモが割増収シスメックスも超増収で売上高「過去最高」連発、では利益面はダイヤモンド決算報新型コロナウイルス禍に円安、資源・原材料の高騰、半導体不足など、日本企業にいくつもの試練が今もなお襲いかかっている。 |
2023-03-13 04:35:00 |
ビジネス |
ダイヤモンド・オンライン - 新着記事 |
異次元緩和立て直し委ねられる「植田新総裁」、4つの主要課題 - 政策・マーケットラボ |
https://diamond.jp/articles/-/319268
|
情報発信 |
2023-03-13 04:30:00 |
ビジネス |
ダイヤモンド・オンライン - 新着記事 |
「脳の最盛期」は50代だった!大人脳の特性を生かしたすごい勉強法とは - 要約の達人 from flier |
https://diamond.jp/articles/-/319270
|
大人の脳は学生時代よりも学びに適した状態になっている。 |
2023-03-13 04:25:00 |
ビジネス |
ダイヤモンド・オンライン - 新着記事 |
年収が高い会社ランキング2022最新版【従業員の平均年齢30代後半】2000万円超の1位は? - ニッポンなんでもランキング! |
https://diamond.jp/articles/-/319198
|
上場企業 |
2023-03-13 04:20:00 |
ビジネス |
ダイヤモンド・オンライン - 新着記事 |
年収が高い会社ランキング2022最新版【従業員の平均年齢30代後半・完全版】任天堂やファストリは何位? - ニッポンなんでもランキング! |
https://diamond.jp/articles/-/319197
|
三井住友 |
2023-03-13 04:20:00 |
ビジネス |
ダイヤモンド・オンライン - 新着記事 |
「3月は株の仕込み時」でも焦らない!金融機関の要注意の営業トークとは? - 今週の週刊ダイヤモンド ここが見どころ |
https://diamond.jp/articles/-/319243
|
見どころ |
2023-03-13 04:15:00 |
ビジネス |
ダイヤモンド・オンライン - 新着記事 |
サッカー日本代表監督は、やはり変えるべきだった理由 - 組織の病気~成長を止める真犯人~ 秋山進 |
https://diamond.jp/articles/-/319277
|
関係者 |
2023-03-13 04:10:00 |
ビジネス |
ダイヤモンド・オンライン - 新着記事 |
中国で今なぜ「上野千鶴子」ブーム?入籍発覚とは別の“炎上”で逆に人気沸騰も - DOL特別レポート |
https://diamond.jp/articles/-/319156
|
|
2023-03-13 04:05:00 |
ビジネス |
東洋経済オンライン |
「誰も取り残さないUX」先進企業はこう実践する 難解な「本人確認フロー」が劇的に改善するまで | インターネット | 東洋経済オンライン |
https://toyokeizai.net/articles/-/654488?utm_source=rss&utm_medium=http&utm_campaign=link_back
|
事業活動 |
2023-03-13 04:50:00 |
ビジネス |
東洋経済オンライン |
小田急車両基地「伊勢原に新設」で起こる大再編 相模大野から機能移転、海老名はどうなる? | 経営 | 東洋経済オンライン |
https://toyokeizai.net/articles/-/658612?utm_source=rss&utm_medium=http&utm_campaign=link_back
|
小田急電鉄 |
2023-03-13 04:30:00 |
海外TECH |
reddit |
Team Vitality vs. Excel Esports - Post Match Discussion |
https://www.reddit.com/r/leagueoflegends/comments/11pn9qh/team_vitality_vs_excel_esports_post_match/
|
Team Vitality vs Excel Esports Post Match DiscussionPost match thread missing will delete when it s up VIT win through pure dominance was the delay worth it submitted by u TheAleqZ to r leagueoflegends link comments |
2023-03-12 19:24:46 |
コメント
コメントを投稿