SlideShare a Scribd company logo
1 of 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

More Related Content

Viewers also liked

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
 

Viewers also liked (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
 

Similar to 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
 

Similar to 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?
 

Recently uploaded

Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfhans926745
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesBoston Institute of Analytics
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 

Recently uploaded (20)

Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 

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

Editor's Notes

  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