SlideShare a Scribd company logo
1 of 20
Download to read offline
Developer	
  Exchange	
  Group
深入淺出設計模式#1:	
  
策略模式	
  
Prepare	
  by	
  Sean	
  Hsieh	
  
Scene
•  The	
  story	
  started	
  from…	
  
New	
  requirement
•  New	
  requirement:	
  simulate	
  a	
  flying	
  duck	
  
•  So	
  we	
  add	
  a	
  fly	
  funcGon	
  in	
  Duck	
  class	
  	
  
Fly
()
Add	
  a	
  fucGon	
  
Fly
()
Override	
  the	
  	
  fucGon	
  
Fly
()
Override	
  the	
  fucGon	
  
Look	
  good,	
  but…
•  Now	
  we	
  has	
  another	
  requirement,	
  we	
  need	
  a	
  rubber	
  
duck
Fly
()
Add	
  a	
  fucGon	
  
Fly
()
Override	
  the	
  	
  fucGon	
  
Fly
()
Override	
  the	
  fucGon	
  
Fly
()
However,	
  	
  
the	
  rubber	
  duck	
  can’t	
  fly
Now	
  we	
  know	
  that…
•  Disadvantage	
  of	
  using	
  Inheritance:	
  
– Code	
  is	
  duplicate	
  accoss	
  subclasses	
  
– RunGme	
  behavior	
  change	
  is	
  difficult	
  
– We	
  can’t	
  make	
  duck	
  dance	
  
– Hard	
  to	
  gain	
  knowledge	
  of	
  all	
  duck	
  behaviors	
  
– Ducks	
  can’t	
  fly	
  and	
  quack	
  at	
  same	
  Gme	
  
– Change	
  can	
  unintenGonally	
  affect	
  other	
  ducks
If	
  We	
  using	
  Interface?
•  If	
  we	
  using	
  interface	
  to	
  define	
  every	
  acGon	
  
•  There	
  will	
  be	
  thousands	
  of	
  classes	
  in	
  a	
  program
So,	
  How	
  do	
  you	
  think?
Try	
  Strategy	
  PaUern
Design	
  principle	
  1	
  
? IdenGfy	
  the	
  aspects	
  of	
  your	
  applicaGon	
  that	
  vary	
  and	
  	
  	
  
	
  	
  	
  	
  separate	
  them	
  from	
  what	
  stays	
  the	
  same.	
  
•  For	
  eample:
MallardDuck
 RedheadDuck
 RubberDuck
 DescoyDuck
 Duck
Display
 P
 P
 P
 P
 P
Fly
 P
 P
Quack
 P
 P
 P
Take	
  the	
  parts	
  that	
  vary	
  and	
  encapsulate	
  them	
  
to	
  extend	
  them	
  
More	
  detail…
Duck	
  class
 Duck	
  acBon	
  that	
  vary
Quack
Fly
Display
Take	
  the	
  parts	
  that	
  vary	
  and	
  encapsulate	
  them	
  
to	
  extend	
  them	
  
Design	
  principle	
  2(1/2)
?Program	
  to	
  interface/supertype,	
  not	
  an	
  	
  	
  	
  	
  
	
  	
  	
  	
  implementaGon.	
  
•  1.Interface:	
  
– Ex.	
  Use	
  FlyBehavior	
  (interface)	
  define	
  Fly()	
  
Design	
  principle	
  2(2/2)
•  2.	
  SuperType	
  
–  Ex.	
  Dog	
  d	
  =	
  new	
  Dog();	
  
	
  	
  	
  	
  	
  	
  	
  d.bark();	
  
	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  Animal	
  animal	
  =	
  new	
  Dog();	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  animal.makesound();	
  
X	
  	
  Locked	
  in	
  the	
  code
Design	
  principle	
  3
•  Favor	
  composiGon	
  over	
  inheritance
Do	
  you	
  remember?
•  Disadvantage	
  of	
  using	
  Inheritance:	
  
– Code	
  is	
  duplicate	
  accoss	
  subclasses	
  
– RunGme	
  behavior	
  change	
  is	
  difficult	
  
– We	
  can’t	
  make	
  duck	
  dance	
  
– Hard	
  to	
  gain	
  knowledge	
  of	
  all	
  duck	
  behaviors	
  
– Ducks	
  can’t	
  fly	
  and	
  quack	
  at	
  same	
  Gme	
  
– Change	
  can	
  unintenGonally	
  affect	
  other	
  ducks
More
A	
  Case	
  of	
  Portable	
  Monitor	
  prototype
But	
  it	
  has	
  some	
  requirement	
  before	
  
shipping…	
  
•  PM:	
  
– The	
  different	
  type	
  packages	
  must	
  to	
  be	
  different	
  
colors.	
  
– The	
  different	
  type	
  packages	
  must	
  to	
  be	
  different	
  
direcGons.	
  
– The	
  user	
  can	
  switch	
  different	
  type	
  package	
  flow.
Let’s	
  trying	
  strategy	
  paUern
Design	
  Architecture

More Related Content

Similar to 策略模式

Greenfoot presentation
Greenfoot presentationGreenfoot presentation
Greenfoot presentation
leannejoy_13
 
Approaching Patterns How to Apply
Approaching Patterns How to ApplyApproaching Patterns How to Apply
Approaching Patterns How to Apply
Lloyd Monteiro
 

Similar to 策略模式 (19)

Greenfoot presentation
Greenfoot presentationGreenfoot presentation
Greenfoot presentation
 
Welcome to Design patterns
Welcome to Design patternsWelcome to Design patterns
Welcome to Design patterns
 
"The worst code I ever wrote"
"The worst code I ever wrote""The worst code I ever wrote"
"The worst code I ever wrote"
 
The Worst Code I Ever Wrote
The Worst Code I Ever WroteThe Worst Code I Ever Wrote
The Worst Code I Ever Wrote
 
Deploying puppet code at light speed
Deploying puppet code at light speedDeploying puppet code at light speed
Deploying puppet code at light speed
 
Deploying Puppet Code At Light Speed - Puppet Camp Silicon Valley
Deploying Puppet Code At Light Speed - Puppet Camp Silicon ValleyDeploying Puppet Code At Light Speed - Puppet Camp Silicon Valley
Deploying Puppet Code At Light Speed - Puppet Camp Silicon Valley
 
Terraforming
Terraforming Terraforming
Terraforming
 
Approaching Patterns How to Apply
Approaching Patterns How to ApplyApproaching Patterns How to Apply
Approaching Patterns How to Apply
 
PuppetConf 2014 Killer R10K Workflow With Notes
PuppetConf 2014 Killer R10K Workflow With NotesPuppetConf 2014 Killer R10K Workflow With Notes
PuppetConf 2014 Killer R10K Workflow With Notes
 
Design Patterns Illustrated
Design Patterns IllustratedDesign Patterns Illustrated
Design Patterns Illustrated
 
10 strategy pattern
10 strategy pattern10 strategy pattern
10 strategy pattern
 
A Partial Multiverse Model of Time Travel for Debugging
A Partial Multiverse Model of Time Travel for DebuggingA Partial Multiverse Model of Time Travel for Debugging
A Partial Multiverse Model of Time Travel for Debugging
 
Web Zurich - Make your animations perform well
Web Zurich - Make your animations perform wellWeb Zurich - Make your animations perform well
Web Zurich - Make your animations perform well
 
Intro to MIDP Development
Intro to MIDP DevelopmentIntro to MIDP Development
Intro to MIDP Development
 
Scala - the good, the bad and the very ugly
Scala - the good, the bad and the very uglyScala - the good, the bad and the very ugly
Scala - the good, the bad and the very ugly
 
Icinga Director
Icinga DirectorIcinga Director
Icinga Director
 
Strategy and Template Pattern
Strategy and Template PatternStrategy and Template Pattern
Strategy and Template Pattern
 
DevOps: Building by feature with immutable infrastructure at Serv.sg
DevOps: Building by feature with immutable infrastructure at Serv.sgDevOps: Building by feature with immutable infrastructure at Serv.sg
DevOps: Building by feature with immutable infrastructure at Serv.sg
 
Data Science Salon: Deep Learning as a Product @ Scribd
Data Science Salon: Deep Learning as a Product @ ScribdData Science Salon: Deep Learning as a Product @ Scribd
Data Science Salon: Deep Learning as a Product @ Scribd
 

More from Jen-Hsuan Hsieh (9)

Node.js筆記(1)
Node.js筆記(1)Node.js筆記(1)
Node.js筆記(1)
 
觀察者模式
觀察者模式 觀察者模式
觀察者模式
 
轉接器模式與表象模式
轉接器模式與表象模式 轉接器模式與表象模式
轉接器模式與表象模式
 
獨體模式
獨體模式 獨體模式
獨體模式
 
裝飾者模式
裝飾者模式 裝飾者模式
裝飾者模式
 
狀態模式
狀態模式 狀態模式
狀態模式
 
命令模式
命令模式 命令模式
命令模式
 
反覆器模式與合成模式
反覆器模式與合成模式 反覆器模式與合成模式
反覆器模式與合成模式
 
工廠模式
工廠模式 工廠模式
工廠模式
 

Recently uploaded

+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 

Recently uploaded (20)

Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Pharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodologyPharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodology
 
%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
 
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verifiedSector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
 
%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
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
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 Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
%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
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 

策略模式

  • 1. Developer  Exchange  Group 深入淺出設計模式#1:   策略模式   Prepare  by  Sean  Hsieh  
  • 2. Scene •  The  story  started  from…  
  • 3. New  requirement •  New  requirement:  simulate  a  flying  duck   •  So  we  add  a  fly  funcGon  in  Duck  class     Fly () Add  a  fucGon   Fly () Override  the    fucGon   Fly () Override  the  fucGon  
  • 4. Look  good,  but… •  Now  we  has  another  requirement,  we  need  a  rubber   duck Fly () Add  a  fucGon   Fly () Override  the    fucGon   Fly () Override  the  fucGon   Fly () However,     the  rubber  duck  can’t  fly
  • 5. Now  we  know  that… •  Disadvantage  of  using  Inheritance:   – Code  is  duplicate  accoss  subclasses   – RunGme  behavior  change  is  difficult   – We  can’t  make  duck  dance   – Hard  to  gain  knowledge  of  all  duck  behaviors   – Ducks  can’t  fly  and  quack  at  same  Gme   – Change  can  unintenGonally  affect  other  ducks
  • 6. If  We  using  Interface? •  If  we  using  interface  to  define  every  acGon   •  There  will  be  thousands  of  classes  in  a  program
  • 7. So,  How  do  you  think?
  • 9. Design  principle  1   ? IdenGfy  the  aspects  of  your  applicaGon  that  vary  and              separate  them  from  what  stays  the  same.   •  For  eample: MallardDuck RedheadDuck RubberDuck DescoyDuck Duck Display P P P P P Fly P P Quack P P P Take  the  parts  that  vary  and  encapsulate  them   to  extend  them  
  • 10. More  detail… Duck  class Duck  acBon  that  vary Quack Fly Display Take  the  parts  that  vary  and  encapsulate  them   to  extend  them  
  • 11. Design  principle  2(1/2) ?Program  to  interface/supertype,  not  an                  implementaGon.   •  1.Interface:   – Ex.  Use  FlyBehavior  (interface)  define  Fly()  
  • 12. Design  principle  2(2/2) •  2.  SuperType   –  Ex.  Dog  d  =  new  Dog();                d.bark();                        Animal  animal  =  new  Dog();                      animal.makesound();   X    Locked  in  the  code
  • 13. Design  principle  3 •  Favor  composiGon  over  inheritance
  • 14. Do  you  remember? •  Disadvantage  of  using  Inheritance:   – Code  is  duplicate  accoss  subclasses   – RunGme  behavior  change  is  difficult   – We  can’t  make  duck  dance   – Hard  to  gain  knowledge  of  all  duck  behaviors   – Ducks  can’t  fly  and  quack  at  same  Gme   – Change  can  unintenGonally  affect  other  ducks
  • 15.
  • 16. More
  • 17. A  Case  of  Portable  Monitor  prototype
  • 18. But  it  has  some  requirement  before   shipping…   •  PM:   – The  different  type  packages  must  to  be  different   colors.   – The  different  type  packages  must  to  be  different   direcGons.   – The  user  can  switch  different  type  package  flow.