徐州软件开发网、徐州程序员、徐州程序员招聘求职、徐州软件企业推荐、 软件项目交易。点击注册加入我们,进入论坛发布消息

【行动胜于雄辩】徐州程序员交流QQ群:324379(一群,满)1182709(二群).进群请先看公告! 招聘求职请先在本站'招聘\求职区'发布,只在群里发布的,将被请出程序员交流群! 谢谢合作

本主题共有 0 篇回复,最新回复发表于 10-10-2009 22:23,作者 xzsdn_08
页 1 / 1 (1 项)
帖子排序: 上一主题 下一主题
  • 10-10-2009 22:23

    • xzsdn_08
    • 灌水10强
      男
    • 注册时间 01-04-2009
    • 江苏徐州
    • 发帖总数 641

    31天重构指南 31使用多态代替条件判断

    Refactoring Day 31 : Replace conditional with Polymorphism

    The last day of refactoring comes from Fowlers refactoring catalog and can be found here.

    This shows one of the foundations of Object Oriented Programming which is Polymorphism. The concept here is that in instances where you are doing checks by type, and performing some type of operation, it’s a good idea to encapsulate that algorithm within the class and then use polymorphism to abstract the call to the code.

       1: public abstract class Customer
       2: {
       3: }
       4:  
       5: public class Employee : Customer
       6: {
       7: }
       8:  
       9: public class NonEmployee : Customer
      10: {
      11: }
      12:  
      13: public class OrderProcessor
      14: {
      15:     public decimal ProcessOrder(Customer customer, IEnumerable<Product> products)
      16:     {
      17:         // do some processing of order
      18:         decimal orderTotal = products.Sum(p => p.Price);
      19:  
      20:         Type customerType = customer.GetType();
      21:         if (customerType == typeof(Employee))
      22:         {
      23:             orderTotal -= orderTotal * 0.15m;
      24:         }
      25:         else if (customerType == typeof(NonEmployee))
      26:         {
      27:             orderTotal -= orderTotal * 0.05m;
      28:         }
      29:  
      30:         return orderTotal;
      31:     }
      32: }

    As you can see here, we’re not leaning on our inheritance hierarchy to put the calculation, or even the data needed to perform the calculation lest we have a SRP violation. So to refactor this we simply take the percentage rate and place that on the actual customer type that each class will then implement. I know this is really remedial but I wanted to cover this as well as I have seen it in code.

       1: public abstract class Customer
       2: {
       3:     public abstract decimal DiscountPercentage { get; }
       4: }
       5:  
       6: public class Employee : Customer
       7: {
       8:     public override decimal DiscountPercentage
       9:     {
      10:         get { return 0.15m; }
      11:     }
      12: }
      13:  
      14: public class NonEmployee : Customer
      15: {
      16:     public override decimal DiscountPercentage
      17:     {
      18:         get { return 0.05m; }
      19:     }
      20: }
      21:  
      22: public class OrderProcessor
      23: {
      24:     public decimal ProcessOrder(Customer customer, IEnumerable<Product> products)
      25:     {
      26:         // do some processing of order
      27:         decimal orderTotal = products.Sum(p => p.Price);
      28:  
      29:         orderTotal -= orderTotal * customer.DiscountPercentage;
      30:  
      31:         return orderTotal;
      32:     }
      33: }

    This is part of the 31 Days of Refactoring series. For a full list of Refactorings please see the original introductory post.

页 1 / 1 (1 项)
徐州软件开发网、徐州软件开发社区、徐州程序员
© Copyright 2008-2011 XZSDN.NET   Powered By communityserver   站长博客 我的Blog
苏ICP备09002379号 | 意见反馈 | 联系我