How to deal with Java Polymorphism in Service Oriented Architecture

前端 未结 5 2168
情歌与酒
情歌与酒 2020-12-28 18:42

What is the path of least evil when dealing with polymorphism and inheritance of entity types in a service-oriented architecture?

A principle of SOA (as I understand

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-28 19:20

    You can avoid this problem by designing the business logic in different classes based on the entity type, based on single responsibility principle it would be the best way to go when you place business logic in a service layer and use a factory to create logic implementation, for example

    enum ProductType
    {
        Physical,
        Service
    }
    
    
    interface IProduct
    {
        double getRate();
        ProductType getProductType();    
    }
    
    class PhysicalProduct implements IProduct
    {
        private double rate;
    
        public double getRate()
        {
            return rate;
        }
    
        public double getProductType()
        {
            return ProductType.Physical;
        }
    }
    
    class ServiceProduct implements IProduct 
    {
        private double rate;
        private double overTimeRate;
        private double maxHoursPerDayInNormalRate;
    
        public double getRate()
        {
            return rate;
        }
    
        public double getOverTimeRate()
        {
            return overTimeRate;
        }
    
        public double getMaxHoursPerDayInNormalRate;()
        {
            return maxHoursPerDayInNormalRate;
        }
    
        public double getProductType()
        {
            return ProductType.Service;
        }
    }
    
    interface IProductCalculator
    {
        double calculate(double units);
    }
    
    class PhysicalProductCalculator implements IProductCalculator
    {
        private PhysicalProduct product;
    
        public PhysicalProductCalculator(IProduct product)
        {
            this.product = (PhysicalProduct) product;
        }
    
        double calculate(double units)
        {
            //calculation logic goes here
        }
    }
    
    class ServiceProductCalculator implements IProductCalculator
    {
        private ServiceProduct product;
    
        public ServiceProductCalculator(IProduct product)
        {
            this.product = (ServiceProduct) product;
        }
    
        double calculate(double units)
        {
            //calculation logic goes here
        }
    }
    
    class ProductCalculatorFactory
    {
        public static IProductCalculator createCalculator(IProduct product)
        {
            switch (product.getProductType)
            {
                case Physical:
                    return new PhysicalProductCalculator ();
                case Service:
                    return new ServiceProductCalculator ();
            }
        }
    }
    
    //this can be used to execute the business logic
    ProductCalculatorFactory.createCalculator(product).calculate(value);
    

提交回复
热议问题