O slideshow foi denunciado.
Utilizamos seu perfil e dados de atividades no LinkedIn para personalizar e exibir anúncios mais relevantes. Altere suas preferências de anúncios quando desejar.

Polymorphism

1.907 visualizações

Publicada em

Show the basics of inheritance and polymorphism in object-oriented programming. Introductory. Part of Industrial Logic's Object-Oriented Design Workshop. http://industriallogic.com/shop

Publicada em: Tecnologia, Negócios
  • Entre para ver os comentários

  • Seja a primeira pessoa a gostar disto

Polymorphism

  1. 1. Industrial Logic, Inc. Twitter: @IndustrialLogicPolymorphismCrafting Interchangeable Parts
  2. 2. Interface
  3. 3. Polymorphism Example
  4. 4. Duplicated Codepublic 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(); }}
  5. 5. Caller repeats itselfpublic 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?"); }}
  6. 6. Duplicated Code
  7. 7. Make them the samepublic 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(); }}
  8. 8. Make them the same
  9. 9. Not really better yetpublic 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?"); }}
  10. 10. Extract a base classabstract 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(); }}
  11. 11. Extract a base class
  12. 12. Extract an interfacepublic 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);}
  13. 13. Extract an interface
  14. 14. Caller uses Interfacepublic class MessagerCaller{ public static void Borrow(Messager messager, List<string> friends) { messager.Recipients(friends); messager.SendMessage("Hello!", "Can I borrow $100.00?"); }}
  15. 15. PolymorphismAdvantages andDisadvantages
  16. 16. Duplication
  17. 17. Beforepublic 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?"); }} Afterpublic class MessagerCaller{ public static void Borrow(Messager messager, List<string> friends) { messager.Recipients(friends); messager.SendMessage("Hello!", "Can I borrow $100.00?"); }}
  18. 18. Extension
  19. 19. Message my Facebook friendspublic 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); } }}
  20. 20. Message my Facebook friends
  21. 21. Speculative Generality
  22. 22. Combinatorial Explosion
  23. 23. Summary• Use polymorphism to craft interchangeable parts by: • Implementing interfaces • Putting shared code in a base class• Avoid speculative generality and combinatorial explosions
  24. 24. Polymorphism Exercise

×