SlideShare a Scribd company logo
1 of 19
Download to read offline
Developer	
  Exchange	
  Group
深入淺出設計模式#9:	
  
命令模式	
  
Prepare	
  by	
  Sean	
  Hsieh	
  
Scene
Scene
•  Remote	
  controller	
  
– Many	
  specific	
  vendor	
  wai5ng	
  to	
  be	
  added.	
  
What’s	
  the	
  problem?
•  We	
  can	
  expect	
  more	
  vendor	
  classes	
  in	
  the	
  
future	
  with	
  just	
  as	
  diverse	
  methods	
  
– Whenever	
  a	
  new	
  vendor	
  class	
  comes	
  out,	
  we’d	
  
have	
  to	
  go	
  and	
  modify	
  the	
  code,	
  potenFally	
  
creaFng	
  bugs	
  and	
  more	
  works	
  for	
  ourselves.	
  
BuJon	
  
1
BuJon	
  
2
Light
TV
on()
on()
The	
  strategy	
  to	
  solve	
  the	
  problem	
  
•  SeparaFon	
  
– 1.The	
  remote	
  should	
  know	
  how	
  to	
  interpret	
  
buJon	
  press	
  and	
  make	
  requests.	
  
– 2.The	
  remote	
  shouldn’t	
  know	
  a	
  lot	
  about	
  the	
  
specifics	
  of	
  the	
  vendors	
  classes.	
  
BuJon	
  1	
  
(Client)
 Light	
  
(Reciever)
Invoker	
  
object
Command	
  
object
AcFon()
SetCommand()
execute()
on()
Command	
  paJern
What	
  is	
  command	
  object
•  DefiniFon	
  
– Encapsulates	
  a	
  request	
  as	
  an	
  object	
  
– LeXng	
  you	
  parameterize	
  other	
  objects	
  with	
  
different	
  requests,	
  queue	
  or	
  log	
  requests,	
  and	
  
support	
  undoable	
  operaFons
How	
  to	
  use	
  command	
  paJern?
•  Concept
How	
  to	
  use	
  command	
  paJern?
•  Class	
  diagram
How	
  to	
  use	
  command	
  paJern?
•  Command	
  object
How	
  to	
  use	
  command	
  paJern?
•  Reciever
How	
  to	
  use	
  command	
  paJern?
•  Client
There	
  are	
  no	
  dumb	
  quesFons
•  1.Why	
  can’t	
  the	
  command	
  object	
  implement	
  the	
  
details	
  of	
  execute	
  method?	
  
–  You’ll	
  no	
  longer	
  have	
  the	
  same	
  level	
  of	
  decoupling	
  
between	
  the	
  invoker	
  and	
  reciever.	
  
–  You’ll	
  be	
  unable	
  to	
  parameterize	
  your	
  commands	
  with	
  
receivers.	
  
•  2.How	
  can	
  I	
  implement	
  a	
  history	
  of	
  undo	
  operaFons?	
  
•  3.Could	
  I	
  put	
  the	
  calls	
  to	
  execute	
  the	
  other	
  commands	
  
in	
  execute	
  method?
Undo	
  operaFon	
  in	
  command	
  object
Using	
  macro	
  command(1/2)
Using	
  macro	
  command(2/2)
Queuing	
  request	
  
Logging	
  request
More
•  Scheduler

More Related Content

Viewers also liked

可抽換元件設計模式
可抽換元件設計模式可抽換元件設計模式
可抽換元件設計模式
Pete Chen
 
Designs, Lessons and Advice from Building Large Distributed Systems
Designs, Lessons and Advice from Building Large Distributed SystemsDesigns, Lessons and Advice from Building Large Distributed Systems
Designs, Lessons and Advice from Building Large Distributed Systems
Chris Huang
 
重構—改善既有程式的設計(chapter 2,3)
重構—改善既有程式的設計(chapter 2,3)重構—改善既有程式的設計(chapter 2,3)
重構—改善既有程式的設計(chapter 2,3)
Chris Huang
 
重構—改善既有程式的設計(chapter 4,5)
重構—改善既有程式的設計(chapter 4,5)重構—改善既有程式的設計(chapter 4,5)
重構—改善既有程式的設計(chapter 4,5)
Chris Huang
 
重構—改善既有程式的設計(chapter 1)
重構—改善既有程式的設計(chapter 1)重構—改善既有程式的設計(chapter 1)
重構—改善既有程式的設計(chapter 1)
Chris Huang
 
設計模式的解析與活用讀後心得
設計模式的解析與活用讀後心得設計模式的解析與活用讀後心得
設計模式的解析與活用讀後心得
昱劭 劉
 
模式入門第一堂課: 30分鐘寫出一個模式
模式入門第一堂課: 30分鐘寫出一個模式模式入門第一堂課: 30分鐘寫出一個模式
模式入門第一堂課: 30分鐘寫出一個模式
teddysoft
 
Design Patterns這樣學就會了:入門班 Day1 教材
Design Patterns這樣學就會了:入門班 Day1 教材Design Patterns這樣學就會了:入門班 Day1 教材
Design Patterns這樣學就會了:入門班 Day1 教材
teddysoft
 

Viewers also liked (19)

可抽換元件設計模式
可抽換元件設計模式可抽換元件設計模式
可抽換元件設計模式
 
設計模式的解析與活用 -開拓視野
設計模式的解析與活用 -開拓視野設計模式的解析與活用 -開拓視野
設計模式的解析與活用 -開拓視野
 
Designs, Lessons and Advice from Building Large Distributed Systems
Designs, Lessons and Advice from Building Large Distributed SystemsDesigns, Lessons and Advice from Building Large Distributed Systems
Designs, Lessons and Advice from Building Large Distributed Systems
 
重構—改善既有程式的設計(chapter 2,3)
重構—改善既有程式的設計(chapter 2,3)重構—改善既有程式的設計(chapter 2,3)
重構—改善既有程式的設計(chapter 2,3)
 
Entity Framework實戰
Entity Framework實戰Entity Framework實戰
Entity Framework實戰
 
[教材] 例外處理設計與重構實作班201309
[教材] 例外處理設計與重構實作班201309[教材] 例外處理設計與重構實作班201309
[教材] 例外處理設計與重構實作班201309
 
購物車程式架構簡介
購物車程式架構簡介購物車程式架構簡介
購物車程式架構簡介
 
重構—改善既有程式的設計(chapter 4,5)
重構—改善既有程式的設計(chapter 4,5)重構—改善既有程式的設計(chapter 4,5)
重構—改善既有程式的設計(chapter 4,5)
 
那一夜我們說Pattern design patterns 20周年-published
那一夜我們說Pattern design patterns 20周年-published那一夜我們說Pattern design patterns 20周年-published
那一夜我們說Pattern design patterns 20周年-published
 
重構—改善既有程式的設計(chapter 1)
重構—改善既有程式的設計(chapter 1)重構—改善既有程式的設計(chapter 1)
重構—改善既有程式的設計(chapter 1)
 
常見設計模式介紹
常見設計模式介紹常見設計模式介紹
常見設計模式介紹
 
設計模式的解析與活用讀後心得
設計模式的解析與活用讀後心得設計模式的解析與活用讀後心得
設計模式的解析與活用讀後心得
 
模式入門第一堂課: 30分鐘寫出一個模式
模式入門第一堂課: 30分鐘寫出一個模式模式入門第一堂課: 30分鐘寫出一個模式
模式入門第一堂課: 30分鐘寫出一個模式
 
用十分鐘 學會《資料結構、演算法和計算理論》
用十分鐘  學會《資料結構、演算法和計算理論》用十分鐘  學會《資料結構、演算法和計算理論》
用十分鐘 學會《資料結構、演算法和計算理論》
 
用十分鐘搞懂《離散數學》
用十分鐘搞懂《離散數學》用十分鐘搞懂《離散數學》
用十分鐘搞懂《離散數學》
 
用十分鐘搞懂 《系統分析、軟體工程、專案管理與設計模式》
用十分鐘搞懂   《系統分析、軟體工程、專案管理與設計模式》用十分鐘搞懂   《系統分析、軟體工程、專案管理與設計模式》
用十分鐘搞懂 《系統分析、軟體工程、專案管理與設計模式》
 
Design Patterns這樣學就會了:入門班 Day1 教材
Design Patterns這樣學就會了:入門班 Day1 教材Design Patterns這樣學就會了:入門班 Day1 教材
Design Patterns這樣學就會了:入門班 Day1 教材
 
用20分鐘搞懂 《系統分析、軟體工程、專案管理與設計模式》
用20分鐘搞懂   《系統分析、軟體工程、專案管理與設計模式》用20分鐘搞懂   《系統分析、軟體工程、專案管理與設計模式》
用20分鐘搞懂 《系統分析、軟體工程、專案管理與設計模式》
 
了解模式讓你更敏捷 (C C Agile 活動分享)
了解模式讓你更敏捷 (C C Agile 活動分享)了解模式讓你更敏捷 (C C Agile 活動分享)
了解模式讓你更敏捷 (C C Agile 活動分享)
 

Similar to 命令模式

Patterns&Practices-Errai
Patterns&Practices-ErraiPatterns&Practices-Errai
Patterns&Practices-Errai
hbraun
 
The Basic Concept Of IOC
The Basic Concept Of IOCThe Basic Concept Of IOC
The Basic Concept Of IOC
Carl Lu
 

Similar to 命令模式 (20)

Patterns&Practices-Errai
Patterns&Practices-ErraiPatterns&Practices-Errai
Patterns&Practices-Errai
 
Jug Zurich Slides
Jug Zurich SlidesJug Zurich Slides
Jug Zurich Slides
 
Dark Fairytales from a Phisherman (Vol. II)
Dark Fairytales from a Phisherman (Vol. II)Dark Fairytales from a Phisherman (Vol. II)
Dark Fairytales from a Phisherman (Vol. II)
 
Symfony2 for legacy app rejuvenation: the eZ Publish case study
Symfony2 for legacy app rejuvenation: the eZ Publish case studySymfony2 for legacy app rejuvenation: the eZ Publish case study
Symfony2 for legacy app rejuvenation: the eZ Publish case study
 
Ccna icnd1-labs - exercices
Ccna icnd1-labs - exercicesCcna icnd1-labs - exercices
Ccna icnd1-labs - exercices
 
Interconnecting cisco networking devices part-1
Interconnecting cisco networking devices part-1Interconnecting cisco networking devices part-1
Interconnecting cisco networking devices part-1
 
Exploiting the windows kernel
Exploiting the windows kernelExploiting the windows kernel
Exploiting the windows kernel
 
GWT Jug Stuttgart
GWT Jug StuttgartGWT Jug Stuttgart
GWT Jug Stuttgart
 
BlackHat EU 2012 - Zhenhua Liu - Breeding Sandworms: How To Fuzz Your Way Out...
BlackHat EU 2012 - Zhenhua Liu - Breeding Sandworms: How To Fuzz Your Way Out...BlackHat EU 2012 - Zhenhua Liu - Breeding Sandworms: How To Fuzz Your Way Out...
BlackHat EU 2012 - Zhenhua Liu - Breeding Sandworms: How To Fuzz Your Way Out...
 
BinProxy: New Paradigm of Binary Analysis With Your Favorite Web Proxy
BinProxy: New Paradigm of Binary Analysis With Your Favorite Web ProxyBinProxy: New Paradigm of Binary Analysis With Your Favorite Web Proxy
BinProxy: New Paradigm of Binary Analysis With Your Favorite Web Proxy
 
Burp Suite Extension Development
Burp Suite Extension DevelopmentBurp Suite Extension Development
Burp Suite Extension Development
 
Owasp AppSecEU 2015 - BeEF Session
Owasp AppSecEU 2015 - BeEF SessionOwasp AppSecEU 2015 - BeEF Session
Owasp AppSecEU 2015 - BeEF Session
 
Whats New In Entity Framework 4
Whats New In Entity Framework 4Whats New In Entity Framework 4
Whats New In Entity Framework 4
 
Challenges in Debugging Bootstraps of Reflective Kernels
Challenges in Debugging Bootstraps of Reflective KernelsChallenges in Debugging Bootstraps of Reflective Kernels
Challenges in Debugging Bootstraps of Reflective Kernels
 
Puppet at DemonWare - Ruaidhri Power - Puppetcamp Dublin '12
Puppet at DemonWare - Ruaidhri Power - Puppetcamp Dublin '12Puppet at DemonWare - Ruaidhri Power - Puppetcamp Dublin '12
Puppet at DemonWare - Ruaidhri Power - Puppetcamp Dublin '12
 
Fiware Developers Week Iot exercises (Advanced)
Fiware Developers Week Iot exercises (Advanced)Fiware Developers Week Iot exercises (Advanced)
Fiware Developers Week Iot exercises (Advanced)
 
The Basic Concept Of IOC
The Basic Concept Of IOCThe Basic Concept Of IOC
The Basic Concept Of IOC
 
Command pattern vs. MVC: Lean Beans (are made of this)
Command pattern vs. MVC: Lean Beans (are made of this)Command pattern vs. MVC: Lean Beans (are made of this)
Command pattern vs. MVC: Lean Beans (are made of this)
 
Software Qualitiy with Sonar Tool 2013_4
Software Qualitiy with Sonar Tool 2013_4Software Qualitiy with Sonar Tool 2013_4
Software Qualitiy with Sonar Tool 2013_4
 
Puppet Integration Adapter - Datasheet
Puppet Integration Adapter - DatasheetPuppet Integration Adapter - Datasheet
Puppet Integration Adapter - Datasheet
 

Recently uploaded

Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
VictoriaMetrics
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 
Abortion Pill Prices Boksburg [(+27832195400*)] 🏥 Women's Abortion Clinic in ...
Abortion Pill Prices Boksburg [(+27832195400*)] 🏥 Women's Abortion Clinic in ...Abortion Pill Prices Boksburg [(+27832195400*)] 🏥 Women's Abortion Clinic in ...
Abortion Pill Prices Boksburg [(+27832195400*)] 🏥 Women's Abortion Clinic in ...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 

Recently uploaded (20)

%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
WSO2CON 2024 Slides - Unlocking Value with AI
WSO2CON 2024 Slides - Unlocking Value with AIWSO2CON 2024 Slides - Unlocking Value with AI
WSO2CON 2024 Slides - Unlocking Value with AI
 
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
 
Abortion Pill Prices Boksburg [(+27832195400*)] 🏥 Women's Abortion Clinic in ...
Abortion Pill Prices Boksburg [(+27832195400*)] 🏥 Women's Abortion Clinic in ...Abortion Pill Prices Boksburg [(+27832195400*)] 🏥 Women's Abortion Clinic in ...
Abortion Pill Prices Boksburg [(+27832195400*)] 🏥 Women's Abortion Clinic in ...
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 

命令模式

  • 1. Developer  Exchange  Group 深入淺出設計模式#9:   命令模式   Prepare  by  Sean  Hsieh  
  • 3. Scene •  Remote  controller   – Many  specific  vendor  wai5ng  to  be  added.  
  • 4. What’s  the  problem? •  We  can  expect  more  vendor  classes  in  the   future  with  just  as  diverse  methods   – Whenever  a  new  vendor  class  comes  out,  we’d   have  to  go  and  modify  the  code,  potenFally   creaFng  bugs  and  more  works  for  ourselves.   BuJon   1 BuJon   2 Light TV on() on()
  • 5. The  strategy  to  solve  the  problem   •  SeparaFon   – 1.The  remote  should  know  how  to  interpret   buJon  press  and  make  requests.   – 2.The  remote  shouldn’t  know  a  lot  about  the   specifics  of  the  vendors  classes.   BuJon  1   (Client) Light   (Reciever) Invoker   object Command   object AcFon() SetCommand() execute() on()
  • 7. What  is  command  object •  DefiniFon   – Encapsulates  a  request  as  an  object   – LeXng  you  parameterize  other  objects  with   different  requests,  queue  or  log  requests,  and   support  undoable  operaFons
  • 8. How  to  use  command  paJern? •  Concept
  • 9. How  to  use  command  paJern? •  Class  diagram
  • 10. How  to  use  command  paJern? •  Command  object
  • 11. How  to  use  command  paJern? •  Reciever
  • 12. How  to  use  command  paJern? •  Client
  • 13. There  are  no  dumb  quesFons •  1.Why  can’t  the  command  object  implement  the   details  of  execute  method?   –  You’ll  no  longer  have  the  same  level  of  decoupling   between  the  invoker  and  reciever.   –  You’ll  be  unable  to  parameterize  your  commands  with   receivers.   •  2.How  can  I  implement  a  history  of  undo  operaFons?   •  3.Could  I  put  the  calls  to  execute  the  other  commands   in  execute  method?
  • 14. Undo  operaFon  in  command  object