
What
is the need for Factory Method
|
Here is an example:
public abstract
class number {
public abstract void displayType();
}
public class oddNo:number {
public override void displayType() { Console.WriteLine(Odd number);
}
}
public class evenNo:number {
public override void displayType() { Console.WriteLine(Even number);
}
}
public class testClass {
public static number sampleFactoryMethod(int sampleNo) {
number num;
if(sampleNo%2==0) { num = new evenNo(); }
else { num = new oddNo(); }
return num;
}
public static void Main() {
int data = 11;
number obj = sampleFactoryMethod(data);
obj.displayType();
}
}
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#.
_______________________________________________________________________
FREE Subscription
Subscribe
to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.
Note
: We never rent, trade, or sell my email lists to
anyone.
We assure that your privacy is respected
and protected.
Visit .NET Programming Tutorial Homepage
______________________________________________________