Factory Design Pattern Implementation with multiple interfaces and base class

  softwareengineering

I know it is lot of code. But I am trying to understand the concept of Factory pattern with interfaces and base class. I have a base class of Car that implements an interface which will be implemented by every derived class.

Then I have another interface that will be only implemented by few derived classes. OnlyImplementedByBMW.

Then I have derived classes that inherits from base class and also implements an interface. Now I want to use factory pattern to create instance of BMW.
CarFactoryPattern.cs works fine and returns the instance of Bmw object but it is only able to call functions in base class. How can I call BMWFunction1 from C1 object created from factory pattern?

Here is my code

Base Class:

 abstract class Car : ICarFunctions
    {
        string color { get; set; }
        double price { get; set; }
        string Model { get; set; }
        string carType { get; set; }

        public virtual void brakeSystem(){ }

        public virtual void drivingModes(string mode) {}

        public virtual void entertainmentSystem() {}

    }

Interface:

 interface ICarFunctions
    {
        void brakeSystem();

        void drivingModes(string mode);

        void entertainmentSystem();
    }

Derived Class:

    class Bmw : Car, OnlyImplementedByBmw
    {
        public string carType{ get;set;}

        public string color { get; set; }

        public string Model { get; set; }
        public double price { get; set; }

        public Bmw(string CarColor,string CarModel,double CarPrice, string CarType)
        {
            carType = CarType;
            color = CarColor;
            Model = CarModel;
            price = CarPrice;
        }

        public override void brakeSystem()
        {
            Console.WriteLine("Brake in less than 0.02ms");
        }

        public override void drivingModes(string mode)
        {
            switch (mode)

            {
                case "mountain":
                    {
                        Console.WriteLine("Switching to 4x4 mode");
                        break;
                    }

                default:
                    {
                        Console.WriteLine("Normal Mode");
                        break;
                    }
            }

        }

        public override void entertainmentSystem()
        {
            Console.WriteLine("Music, Navigation");
        }

        public void BmwFunction1()
        {
            Console.WriteLine("New Function");
        }
    }

New Interface:

 interface OnlyImplementedByBmw
    {
         void BmwFunction1();
    }

Factory Pattern Class:

 class CarFactoryPattern
    {
        private string CarColor;
        private string CarModel;
        private string CarType;
        private double CarPrice;
        public Car GetCarInstance(int id)
        {           

            switch (id)
            {
                case 1:
                    {

                        return new Bmw( CarColor,  CarModel,  CarPrice,  CarType);
                    }

                case 2:
                    {
                        return new Mercedes();
                    }
                default:
                    {  return null;}

            }
        }     
    }

Main program:

 class Program
    {
        static void Main(string[] args)
        {
            CarFactoryPattern cf = new CarFactoryPattern();
            Car c1 = cf.GetCarInstance(1);
            c1.brakeSystem();
            c1.BmwFunction1() // This does not work

        }
}

How can I call BMWFunction1 from c1 object created from factory pattern? c1 object is Bmw type of object so shouldn’t it has all the functions defined in class for BMW which includes the interface function of “onlyImplementedByBMW”

No, because you are returning a Car, not a BMW. The type of a variable matters as you can only access members that are accessible to the current type of the variable.

Let’s say you are using my factory method, and you do not know what I choose to return:

public static class FlaterCarFactory
{

    public static Car GetCar()
    {
        // TOP SECRET!
    }
}

And you use my factory:

Car myCar = FlaterCarFactory.GetCar();

You have no idea which car you received. All you know is that it is a car. Maybe I returned a BMW, maybe I returned a Mercedes, maybe I returned a ReliantRobin.

It wouldn’t make sense for you to call:

myCar.BmwMethod();

because you have no reasonable expectation to know that the car you received is a Bmw.


There are workarounds to this, but first I want to mention the most important thing: These workarounds are commonly abused. Nine times out of ten, you should revisit your intended logic because you got yourself into a bd situation by abusing/overusing polymorphism.

Nonetheless, there are ways to convert your Car into a BMW.

  1. Hard casting
BMW myBMW = (BMW)myCar;

This will throw an exception if myCar is not a BMW. Don’t use this unless you know for a fact that you are dealing with a BMW object.

  1. Soft casting
BMW myBMW = myCar as BMW;

This will return null if myCar is not a BMW. While not as bad as hard casting, null reference exceptions are still bad and should be avoided, so don’t use this unless you know for a fact that you are dealing with a BMW object.

  1. Pre-checking for a valid cast
if(myCar is BMW)
{
    BMW myBMW = myCar as BMW;
    //or
    BMW myBMW = (BMW)myCar;
}

It doesn’t matter whether you hard/soft cast here as you’ve already confirmed that the cast will succeed.

However, I want to stress again that while this is syntactically valid, do not abuse it or use it as the goto solution. In pretty much every case, when you run into this issue this means you’ve misused polymorphism somewhere down the line (because you said you needed a Car but really were hoping to get a BMW, which means your code does not match your expectations).

7

LEAVE A COMMENT