SlideShare uma empresa Scribd logo
1 de 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

Mais conteúdo relacionado

Destaque

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
 

Destaque (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
 

Semelhante a 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
 

Semelhante a 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?
 

Último

MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityWSO2
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...apidays
 
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
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
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
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfOrbitshub
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
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
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Bhuvaneswari Subramani
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Zilliz
 

Último (20)

MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
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
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
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
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
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
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 

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

Notas do Editor

  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