0

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"

1

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).

  • So how should this be implemented? My Scenario is that I have one interface which is implemented by base class but other interfaces are only implemented by some classes. Then I want to use factory pattern? Do I create different factory classes for each car class? Like BmwFactory and MercedesFactory classes? – Learn AspNet Feb 6 at 16:13
  • 1
    @LearnAspNet: Think about the factory methods you're writing. The one in your question is the equivalent of "give me a car", which is not the same as "give me a BMW". If you expect to be given a BMW, then the factory method you call should have the BMW return type. It makes no sense to ask for any car and then (unspoken) expect to be given a BMW. (As a sidenote: there's no inherent issue with returning a BMW or Mercedes type from a CarFactory - they are still derived types of car) – Flater Feb 6 at 16:14
  • Does this look correct then? class CarFactoryPattern { private string CarColor; private string CarModel; private string CarType; private double CarPrice; public Bmw GetBCarInstance() { return new Bmw( CarColor, CarModel, CarPrice, CarType); } public Mercedes GetMCarInstance() { return new Mercedes(); } CarFactoryPattern cf = new CarFactoryPattern(); Bmw c1 = cf.GetBCarInstance(); c1.brakeSystem(); c1.BmwFunction1(); – Learn AspNet Feb 6 at 16:30
  • @LearnAspNet that is in no way readable. I also can't confirm whether the factory does what you need it to. I don't know what your business requirements are and this isn't really the place to hash all that out. – Flater Feb 6 at 16:31
  • How can I make it readable in the comment section? Requirements are to use to factory pattern to create car objects without exposing the creation logic to the client? – Learn AspNet Feb 6 at 16:35

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.