Replace conditional with Polymorphism

namespace RefactoringLib.Ploymorphism.Before
{
    public class Customer { }
    
    public class Employee : Customer { }
    
    public class NonEmployee : Customer { }
    
    public class OrderProcessor
    {
        public decimal ProcessOrder(Customer customer, IEnumerable<Product> products)
        {
            decimal orderTotal = products.Sum(p => p.Price);
            
            Type customerType = customer.GetType();
            if (customerType == typeof(Employee))
            {
                orderTotal -= orderTotal * 0.15m;
            }
            else if (customerType == typeof(NonEmployee))
            {
                orderTotal -= orderTotal * 0.05m;
            }

            return orderTotal;
        }
    }
}
View Code

相关文章:

  • 2021-08-15
  • 2021-06-06
  • 2021-06-04
  • 2021-06-18
  • 2022-03-06
  • 2022-12-23
  • 2021-07-30
猜你喜欢
  • 2021-07-19
  • 2021-09-26
  • 2021-08-28
  • 2021-12-02
  • 2021-11-21
  • 2022-12-23
  • 2022-02-28
相关资源
相似解决方案