Hi What is Factory Pattern anyone know could you explain in simple with real time example....
Thank you
Factory pattern
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Vivek KumarRank #272 · 9.3K pointsPosted Jun 8, 2017, 2:50 AM EDT
Vijay KumarRank #6,237 · 272 pointsPosted Jun 8, 2017, 3:17 AM EDT
enum FanType { TableFan, CeilingFan, ExhaustFan } interface IFan { void SwitchOn(); void SwitchOff(); } class TableFan : IFan {.... } class CeilingFan : IFan {.... } class ExhaustFan : IFan {..... } interface IFanFactory { IFan CreateFan(FanType type); } class FanFactory : IFanFactory { public IFan CreateFan(FanType type) { switch (type) { case FanType.TableFan: return new TableFan(); case FanType.CeilingFan: return new CeilingFan(); case FanType.ExhaustFan: return new ExhaustFan(); default: return new TableFan(); } } } //The client code is as follows: static void Main(string[] args) { IFanFactory simpleFactory = new FanFactory(); // Creation of a Fan using Simple Factory IFan fan = simpleFactory.CreateFan(FanType.TableFan); // Use created object fan.SwitchOn(); Console.ReadLine(); }