SlideShare a Scribd company logo
1 of 25
优秀的代码 70% 使用设计
     模式




       Michael
• 基础知识介绍

• 设计模式是什么?

• 怎样使用设计模式?
普通类 VS 抽象类 VS 接口
普通类 : 就是一般的类

抽象类 : 不能直接被初始化,只提供了部分实现 ,剩下的由子类去实
现。

接口 : 定义了一个规范,每一个实现类按照规范去实现方法。




          闭嘴 !
           我们都知道
设计模式是什么?

           • A design pattern is a general
           repeatable solution to a commonly
           occurring problem in software design.

           • A design pattern is not a finished
           design that can be transformed directly
           into code.

           • Algorithms are not thought of as
           design patterns.
开始设计模式之旅吧!
•需求 1 :
       开发一款鸭子游戏,游戏中
会出现各种外观不同的鸭子,这些鸭子
一边游泳,一边呱呱叫。现在知道有绿
头鸭、红头鸭、黄头鸭等等。
Duck
             -outward
             +quack()
             +swim()
             +display ()


 GreenDuck       RedDuck       YellowDuck OtherDuck


+display()      +display() +display()
•需求 2 :
       现在增加一种会飞的鸭子。
Duck
             -outward
             +quack()
             +swim()
             +display ()
             +fly()

 GreenDuck       RedDuck       RubberDuck OtherDuck


+display()      +display() +display()
Duck
              -outward
              +quack ()
              +swim ()
              +display ()
              +fly()

 GreenDuck        RedDuck       RubberDuck   OtherDuck


+display ()      +display () +display ()
                             +fly()
                             +{//什么也
                             不干}()
•需求 3 :
                       增加一种诱饵鸭,诱饵鸭是
                木头假鸭,不会飞又不会叫。




•需求 4 、需求 5 、需求 6 、……需求 N 。 N 种鸭子。
<<interface >>    <<interface >>        Duck
   Flyable          Quackable
+fly()           +quack()
                                    +swim()
                                    +display ()



  GreenDuck            RubberDuck
                                           DecoyDuck
+swim ()             +swim()
+display ()          +display ()         +swim ()
+fly()               +quack ()           +display ()
+quack ()
<<interface >>    <<interface >>        Duck
   Flyable          Quackable
+fly()           +quack ()
                                    +swim()
                                    +display ()



  GreenDuck            RubberDuck
                                           DecoyDuck
+swim ()             +swim()
+display ()          +display ()         +swim ()
+fly()               +quack ()           +display ()
+quack ()
据统计:有 48 种鸭子是“呱呱叫”,
32 种鸭子是“吱吱叫”,剩下的不叫
。
GaGaQuack   ZhiZhiQuack               Duck


+gaga()       +zhizhi()


                          GaGaDuck 1                GaGaDuck 48
                                               -GaGaQuack gagaQuack
                 -GaGaQuack gagaQuack



                      ZhiZhiDuck 1                    ZhiZhiDuck 32
               -ZhiZhiQuack zhizhiQuack        -ZhiZhiQuack zhizhiQuack
<<interface >>
                         FlyBehavior
                       +fly()


        FlyWithWings                         FlyNoWay

  +fly()                                 +fly()
  +{实现鸭子的飞行行为}()                         +{不会飞,什么也不做}()

                         <<interface >>
                         QuackBehavior
                        +quack ()


   GaGaQuack                ZhiZhiQuack                 QuackNoWay

+fly()                 +fly()                  +fly()
+{实现鸭子呱呱叫}()           +{实现鸭子吱吱叫}()            +{不会叫,什么也不做}()
Duck
       -FlyBehavior flyBeha
       -QuackBehavior quackBehavior
       +swim ()
       +display ()
       +getQuack ()
       +setQuack ()
       +getFly ()
       +setFly ()
       +//鸭子其它行为()


   RedDuck        RubberDuck          DecoyDuck


+display ()    +display ()       +display ()
+{//红头鸭}()     +{//橡皮鸭}()        +{//诱饵鸭}()
策略模式


     定义了算法族,分别封装起来,让它们之间可以相互替
换,此模式让算法的变化独立于使用算法的客户。
代码
代码
代码
代码
代码
代码

More Related Content

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

设计模式分享

  • 3. 普通类 VS 抽象类 VS 接口 普通类 : 就是一般的类 抽象类 : 不能直接被初始化,只提供了部分实现 ,剩下的由子类去实 现。 接口 : 定义了一个规范,每一个实现类按照规范去实现方法。 闭嘴 ! 我们都知道
  • 4.
  • 5. 设计模式是什么? • A design pattern is a general repeatable solution to a commonly occurring problem in software design. • A design pattern is not a finished design that can be transformed directly into code. • Algorithms are not thought of as design patterns.
  • 7. •需求 1 : 开发一款鸭子游戏,游戏中 会出现各种外观不同的鸭子,这些鸭子 一边游泳,一边呱呱叫。现在知道有绿 头鸭、红头鸭、黄头鸭等等。
  • 8. Duck -outward +quack() +swim() +display () GreenDuck RedDuck YellowDuck OtherDuck +display() +display() +display()
  • 9. •需求 2 : 现在增加一种会飞的鸭子。
  • 10. Duck -outward +quack() +swim() +display () +fly() GreenDuck RedDuck RubberDuck OtherDuck +display() +display() +display()
  • 11. Duck -outward +quack () +swim () +display () +fly() GreenDuck RedDuck RubberDuck OtherDuck +display () +display () +display () +fly() +{//什么也 不干}()
  • 12. •需求 3 : 增加一种诱饵鸭,诱饵鸭是 木头假鸭,不会飞又不会叫。 •需求 4 、需求 5 、需求 6 、……需求 N 。 N 种鸭子。
  • 13. <<interface >> <<interface >> Duck Flyable Quackable +fly() +quack() +swim() +display () GreenDuck RubberDuck DecoyDuck +swim () +swim() +display () +display () +swim () +fly() +quack () +display () +quack ()
  • 14. <<interface >> <<interface >> Duck Flyable Quackable +fly() +quack () +swim() +display () GreenDuck RubberDuck DecoyDuck +swim () +swim() +display () +display () +swim () +fly() +quack () +display () +quack ()
  • 15. 据统计:有 48 种鸭子是“呱呱叫”, 32 种鸭子是“吱吱叫”,剩下的不叫 。
  • 16. GaGaQuack ZhiZhiQuack Duck +gaga() +zhizhi() GaGaDuck 1 GaGaDuck 48 -GaGaQuack gagaQuack -GaGaQuack gagaQuack ZhiZhiDuck 1 ZhiZhiDuck 32 -ZhiZhiQuack zhizhiQuack -ZhiZhiQuack zhizhiQuack
  • 17. <<interface >> FlyBehavior +fly() FlyWithWings FlyNoWay +fly() +fly() +{实现鸭子的飞行行为}() +{不会飞,什么也不做}() <<interface >> QuackBehavior +quack () GaGaQuack ZhiZhiQuack QuackNoWay +fly() +fly() +fly() +{实现鸭子呱呱叫}() +{实现鸭子吱吱叫}() +{不会叫,什么也不做}()
  • 18. Duck -FlyBehavior flyBeha -QuackBehavior quackBehavior +swim () +display () +getQuack () +setQuack () +getFly () +setFly () +//鸭子其它行为() RedDuck RubberDuck DecoyDuck +display () +display () +display () +{//红头鸭}() +{//橡皮鸭}() +{//诱饵鸭}()
  • 19. 策略模式 定义了算法族,分别封装起来,让它们之间可以相互替 换,此模式让算法的变化独立于使用算法的客户。