What is the need for Factory Method in C#?When your
application has an abstract base class which is inherited by different
derived classes, any derived class can be instantiated and the base class
methods can be executed using that derived class instance.
Each derived class overrides the base class methods to provide a different implementation of the method. Now all that you require is a method which decides on which derived class has to be instantiated. This decision making method is known as factory method. Here is an example: public abstract
class number { Output of this code will be: Odd number In this example, sampleFactoryMethod makes the decision of which derived class instance has to be created. Hence this method implements the Factory Method Pattern in C#.
|