Factory Design Pattern Implementation with multiple interfaces and base class

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật