Implementing Dependency Inversion & Dependency Injection both together by defining structure

44 Views Asked by At

I have subcategories defined under Vehicle Class as :- Bike Car truck bus

Further, there are different variants under each four categories

  1. Bike :- sport, standard
  2. Car :- sport, standard, electric 3.Truck :- Mini Truck , Power Truck
  3. Bus:- Mini Bus, AC Bus

We have to calculate the price of each variants defined in each category.

Example 1 : With Constructor overloading

 public interface ivehicle
    {
        public void calculateprice();
    }

    public class calculatprice : ivehicle
    {
        public void calculateprice() { 
        
        }
    }

    public class bike_1
    {
        public readonly ivehicle vehicle;
        public bike_1(string model) {
            if (model == "STANDARD")
            {
                Console.WriteLine("THe price of standard is 1 lac");
            }
            else if (model == "SPORTS")
            {
                Console.WriteLine("The price of sports is 2 lacs");
            }
        
        }
    }

Example 2:- With using overiding function and inheritance

    public class vehicle_3
    {
       
    }

    public class bike_3: vehicle_3
    
    {
        public virtual void Calculateprice_3() { }

    }

    public class sports_3 :bike_3
    {

        public override void Calculateprice_3()
        {
            base.Calculateprice_3();
            Console.WriteLine("The price of Sports is 2 lacs");
        }

    }

    public class standard_3 : bike_3
    {
        public override void Calculateprice_3()
        {
            base.Calculateprice_3();
            Console.WriteLine("The price of standard is 1 lacs");
        }
    }

Example 3:- With interface

    public class vehicle_4
    {

    }

    public interface ivehicle_4
    {
        void CalculatePrice_4();
    }

    public class bike_4 :ivehicle_4

    {
        public virtual void CalculatePrice_4() { }

    }
    public class sports_4 : bike_4
    {

        public override void CalculatePrice_4()
        {
            base.CalculatePrice_4();
            Console.WriteLine("The price of Sports is 2 lacs");
        }

    }

    public class standard_4 : bike_4
    {
        public override void CalculatePrice_4()
        {
            base.CalculatePrice_4();
            Console.WriteLine("The price of standard is 1 lacs");
        }
    }
0

There are 0 best solutions below