SlideShare une entreprise Scribd logo
1  sur  28
Industrial Logic, Inc.
   Twitter: @IndustrialLogic




Polymorphism
Crafting Interchangeable Parts
Interface
Polymorphism Example
Duplicated Code
public class SmsMessager
{
    private string[] _numbers;

    public void Numbers(string[] numbers)
    {
        _numbers = numbers;
    }

    public void SendMessage(string subject, string text)
    {
        SmsConnector.SendMessage(subject, text).To(_numbers).Execute();
    }
}

public class EmailMessager
{
    private List<string> _recipients;

    public void Recipients(List<string> recipients)
    {
        _recipients = recipients;
    }

    public void SendEmail(string subject, string body)
    {
        EmailGateway.Recipients(_recipients).Subject(subject).Body(body).Send();
    }
}
Caller repeats itself
public class MessagerCaller
{
    public static void BorrowFromPhoneFriends(SmsMessager sms, string[] friends)
    {
        sms.Numbers(friends);
        sms.SendMessage("Hello!", "Can I borrow $100.00?");
    }

    public static void BorrowFromEmailFriends(EmailMessager email, List<string> friends)
    {
        email.Recipients(friends);
        email.SendEmail("Hello!", "Can I borrow $100.00?");
    }
}
Duplicated Code
Make them the same
public class SmsMessager
{
    private List<string> _recipients;

    public void Recipients(List<string> recipients)
    {
        _recipients = recipients;
    }

    public void SendMessage(string subject, string text)
    {
        SmsConnector.SendMessage(subject, text).To(_recipients.ToArray()).Execute();
    }
}

public class EmailMessager
{
    private List<string> _recipients;

    public void Recipients(List<string> recipients)
    {
        _recipients = recipients;
    }

    public void SendMessage(string subject, string text)
    {
        EmailGateway.Recipients(_recipients).Subject(subject).Body(text).Send();
    }
}
Make them the same
Not really better yet
public class MessagerCaller
{
    public static void BorrowFromPhoneFriends(SmsMessager sms, List<string> friends)
    {
        sms.Recipients(friends);
        sms.SendMessage("Hello!", "Can I borrow $100.00?");
    }

    public static void BorrowFromEmailFriends(EmailMessager email, List<string> friends)
    {
        email.Recipients(friends);
        email.SendMessage("Hello!", "Can I borrow $100.00?");
    }
}
Extract a base class
abstract class BaseMessager
{
    protected List<string> _recipients;

    public void Recipients(List<string> recipients)
    {
        _recipients = recipients;
    }

    public abstract void SendMessage(string subject, string text);
}

public class SmsMessager : BaseMessager
{
    public override void SendMessage(string subject, string text)
    {
        SmsConnector.SendMessage(subject, text).To(_recipients.ToArray()).Execute();
    }
}

public class EmailMessager : BaseMessager
{
    public override void SendMessage(string subject, string text)
    {
        EmailGateway.Recipients(_recipients).Subject(subject).Body(text).Send();
    }
}
Extract a base class
Extract an interface
public interface Messager
{
    void Recipients(List<string> recipients);
    void SendMessage(string subject, string text);
}

abstract class BaseMessager : Messager
{
    protected List<string> _recipients;

    public void Recipients(List<string> recipients)
    {
        _recipients = recipients;
    }

    public abstract void SendMessage(string subject, string text);
}
Extract an interface
Caller uses Interface

public class MessagerCaller
{
    public static void Borrow(Messager messager, List<string> friends)
    {
        messager.Recipients(friends);
        messager.SendMessage("Hello!", "Can I borrow $100.00?");
    }
}
Polymorphism
Advantages and
Disadvantages
Duplication
Before
public class MessagerCaller
{
    public static void BorrowFromPhoneFriends(SmsMessager sms, string[] friends)
    {
        sms.Numbers(friends);
        sms.SendMessage("Hello!", "Can I borrow $100.00?");
    }

    public static void BorrowFromEmailFriends(EmailMessager email, List<string> friends)
    {
        email.Recipients(friends);
        email.SendEmail("Hello!", "Can I borrow $100.00?");
    }
}




                                       After
public class MessagerCaller
{
    public static void Borrow(Messager messager, List<string> friends)
    {
        messager.Recipients(friends);
        messager.SendMessage("Hello!", "Can I borrow $100.00?");
    }
}
Extension
Message my Facebook
       friends
public class FacebookMessager : BaseMessager
{
    public override void SendMessage(string subject, string text)
    {
        List<Friend> friends = Facebook.FriendsFrom(_recipients);
        foreach(var friend in friends)
        {
            Post message = new FacebookMessage(friend, subject, text);
            Facebook.Message(message);
        }
    }
}
Message my Facebook friends
Speculative Generality
Combinatorial Explosion
Summary

• Use polymorphism to craft interchangeable
  parts by:
  • Implementing interfaces
  • Putting shared code in a base class
• Avoid speculative generality and combinatorial
  explosions
Polymorphism Exercise

Contenu connexe

En vedette

Hashing and Hash Tables
Hashing and Hash TablesHashing and Hash Tables
Hashing and Hash Tablesadil raja
 
12 couplingand cohesion-student
12 couplingand cohesion-student12 couplingand cohesion-student
12 couplingand cohesion-studentrandhirlpu
 
Iterator - a powerful but underappreciated design pattern
Iterator - a powerful but underappreciated design patternIterator - a powerful but underappreciated design pattern
Iterator - a powerful but underappreciated design patternNitin Bhide
 
Polymorphism and Software Reuse
Polymorphism and Software ReusePolymorphism and Software Reuse
Polymorphism and Software Reuseadil raja
 
The Physical Layer
The Physical LayerThe Physical Layer
The Physical Layeradil raja
 
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...cprogrammings
 
The Data Link Layer
The Data Link LayerThe Data Link Layer
The Data Link Layeradil raja
 
Data structure and algorithms in c++
Data structure and algorithms in c++Data structure and algorithms in c++
Data structure and algorithms in c++Karmjeet Chahal
 
Universal Declarative Services
Universal Declarative ServicesUniversal Declarative Services
Universal Declarative Servicesschemouil
 
XKE - Programming Paradigms & Constructs
XKE - Programming Paradigms & ConstructsXKE - Programming Paradigms & Constructs
XKE - Programming Paradigms & ConstructsNicolas Demengel
 
Classes And Methods
Classes And MethodsClasses And Methods
Classes And Methodsadil raja
 
C++: inheritance, composition, polymorphism
C++: inheritance, composition, polymorphismC++: inheritance, composition, polymorphism
C++: inheritance, composition, polymorphismJussi Pohjolainen
 
The Network Layer
The Network LayerThe Network Layer
The Network Layeradil raja
 

En vedette (20)

Hashing and Hash Tables
Hashing and Hash TablesHashing and Hash Tables
Hashing and Hash Tables
 
12 couplingand cohesion-student
12 couplingand cohesion-student12 couplingand cohesion-student
12 couplingand cohesion-student
 
C++ Inheritance
C++ InheritanceC++ Inheritance
C++ Inheritance
 
Iterator - a powerful but underappreciated design pattern
Iterator - a powerful but underappreciated design patternIterator - a powerful but underappreciated design pattern
Iterator - a powerful but underappreciated design pattern
 
Polymorphism and Software Reuse
Polymorphism and Software ReusePolymorphism and Software Reuse
Polymorphism and Software Reuse
 
The Physical Layer
The Physical LayerThe Physical Layer
The Physical Layer
 
Association agggregation and composition
Association agggregation and compositionAssociation agggregation and composition
Association agggregation and composition
 
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
 
04 design concepts_n_principles
04 design concepts_n_principles04 design concepts_n_principles
04 design concepts_n_principles
 
The Data Link Layer
The Data Link LayerThe Data Link Layer
The Data Link Layer
 
Data structure and algorithms in c++
Data structure and algorithms in c++Data structure and algorithms in c++
Data structure and algorithms in c++
 
Universal Declarative Services
Universal Declarative ServicesUniversal Declarative Services
Universal Declarative Services
 
XKE - Programming Paradigms & Constructs
XKE - Programming Paradigms & ConstructsXKE - Programming Paradigms & Constructs
XKE - Programming Paradigms & Constructs
 
Classes And Methods
Classes And MethodsClasses And Methods
Classes And Methods
 
Syntax part 6
Syntax part 6Syntax part 6
Syntax part 6
 
WSO2 Complex Event Processor
WSO2 Complex Event ProcessorWSO2 Complex Event Processor
WSO2 Complex Event Processor
 
C++: inheritance, composition, polymorphism
C++: inheritance, composition, polymorphismC++: inheritance, composition, polymorphism
C++: inheritance, composition, polymorphism
 
Cohesion & Coupling
Cohesion & Coupling Cohesion & Coupling
Cohesion & Coupling
 
Cohesion and coherence
Cohesion and coherenceCohesion and coherence
Cohesion and coherence
 
The Network Layer
The Network LayerThe Network Layer
The Network Layer
 

Similaire à Polymorphism

Ext GWT 3.0 Data Widgets
Ext GWT 3.0 Data WidgetsExt GWT 3.0 Data Widgets
Ext GWT 3.0 Data WidgetsSencha
 
First few months with Kotlin - Introduction through android examples
First few months with Kotlin - Introduction through android examplesFirst few months with Kotlin - Introduction through android examples
First few months with Kotlin - Introduction through android examplesNebojša Vukšić
 
Creating a Whatsapp Clone - Part III.pdf
Creating a Whatsapp Clone - Part III.pdfCreating a Whatsapp Clone - Part III.pdf
Creating a Whatsapp Clone - Part III.pdfShaiAlmog1
 
Creating a Facebook Clone - Part XX.pdf
Creating a Facebook Clone - Part XX.pdfCreating a Facebook Clone - Part XX.pdf
Creating a Facebook Clone - Part XX.pdfShaiAlmog1
 

Similaire à Polymorphism (6)

Ext GWT 3.0 Data Widgets
Ext GWT 3.0 Data WidgetsExt GWT 3.0 Data Widgets
Ext GWT 3.0 Data Widgets
 
First few months with Kotlin - Introduction through android examples
First few months with Kotlin - Introduction through android examplesFirst few months with Kotlin - Introduction through android examples
First few months with Kotlin - Introduction through android examples
 
Creating a Whatsapp Clone - Part III.pdf
Creating a Whatsapp Clone - Part III.pdfCreating a Whatsapp Clone - Part III.pdf
Creating a Whatsapp Clone - Part III.pdf
 
OWASP Proxy
OWASP ProxyOWASP Proxy
OWASP Proxy
 
Creating a Facebook Clone - Part XX.pdf
Creating a Facebook Clone - Part XX.pdfCreating a Facebook Clone - Part XX.pdf
Creating a Facebook Clone - Part XX.pdf
 
Spring4 whats up doc?
Spring4 whats up doc?Spring4 whats up doc?
Spring4 whats up doc?
 

Dernier

New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 

Dernier (20)

DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 

Polymorphism

  • 1. Industrial Logic, Inc. Twitter: @IndustrialLogic Polymorphism Crafting Interchangeable Parts
  • 2.
  • 3.
  • 4.
  • 5.
  • 8. Duplicated Code public class SmsMessager { private string[] _numbers; public void Numbers(string[] numbers) { _numbers = numbers; } public void SendMessage(string subject, string text) { SmsConnector.SendMessage(subject, text).To(_numbers).Execute(); } } public class EmailMessager { private List<string> _recipients; public void Recipients(List<string> recipients) { _recipients = recipients; } public void SendEmail(string subject, string body) { EmailGateway.Recipients(_recipients).Subject(subject).Body(body).Send(); } }
  • 9. Caller repeats itself public class MessagerCaller { public static void BorrowFromPhoneFriends(SmsMessager sms, string[] friends) { sms.Numbers(friends); sms.SendMessage("Hello!", "Can I borrow $100.00?"); } public static void BorrowFromEmailFriends(EmailMessager email, List<string> friends) { email.Recipients(friends); email.SendEmail("Hello!", "Can I borrow $100.00?"); } }
  • 11. Make them the same public class SmsMessager { private List<string> _recipients; public void Recipients(List<string> recipients) { _recipients = recipients; } public void SendMessage(string subject, string text) { SmsConnector.SendMessage(subject, text).To(_recipients.ToArray()).Execute(); } } public class EmailMessager { private List<string> _recipients; public void Recipients(List<string> recipients) { _recipients = recipients; } public void SendMessage(string subject, string text) { EmailGateway.Recipients(_recipients).Subject(subject).Body(text).Send(); } }
  • 13. Not really better yet public class MessagerCaller { public static void BorrowFromPhoneFriends(SmsMessager sms, List<string> friends) { sms.Recipients(friends); sms.SendMessage("Hello!", "Can I borrow $100.00?"); } public static void BorrowFromEmailFriends(EmailMessager email, List<string> friends) { email.Recipients(friends); email.SendMessage("Hello!", "Can I borrow $100.00?"); } }
  • 14. Extract a base class abstract class BaseMessager { protected List<string> _recipients; public void Recipients(List<string> recipients) { _recipients = recipients; } public abstract void SendMessage(string subject, string text); } public class SmsMessager : BaseMessager { public override void SendMessage(string subject, string text) { SmsConnector.SendMessage(subject, text).To(_recipients.ToArray()).Execute(); } } public class EmailMessager : BaseMessager { public override void SendMessage(string subject, string text) { EmailGateway.Recipients(_recipients).Subject(subject).Body(text).Send(); } }
  • 15. Extract a base class
  • 16. Extract an interface public interface Messager { void Recipients(List<string> recipients); void SendMessage(string subject, string text); } abstract class BaseMessager : Messager { protected List<string> _recipients; public void Recipients(List<string> recipients) { _recipients = recipients; } public abstract void SendMessage(string subject, string text); }
  • 18. Caller uses Interface public class MessagerCaller { public static void Borrow(Messager messager, List<string> friends) { messager.Recipients(friends); messager.SendMessage("Hello!", "Can I borrow $100.00?"); } }
  • 21. Before public class MessagerCaller { public static void BorrowFromPhoneFriends(SmsMessager sms, string[] friends) { sms.Numbers(friends); sms.SendMessage("Hello!", "Can I borrow $100.00?"); } public static void BorrowFromEmailFriends(EmailMessager email, List<string> friends) { email.Recipients(friends); email.SendEmail("Hello!", "Can I borrow $100.00?"); } } After public class MessagerCaller { public static void Borrow(Messager messager, List<string> friends) { messager.Recipients(friends); messager.SendMessage("Hello!", "Can I borrow $100.00?"); } }
  • 23. Message my Facebook friends public class FacebookMessager : BaseMessager { public override void SendMessage(string subject, string text) { List<Friend> friends = Facebook.FriendsFrom(_recipients); foreach(var friend in friends) { Post message = new FacebookMessage(friend, subject, text); Facebook.Message(message); } } }
  • 27. Summary • Use polymorphism to craft interchangeable parts by: • Implementing interfaces • Putting shared code in a base class • Avoid speculative generality and combinatorial explosions

Notes de l'éditeur

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n