.net - Factory pattern in C# -


i bit able understand factory pattern , came implementation.

class program {     static void main(string[] args)     {         console.writeline("enter fruit name fruit!");         string fruit = console.readline();          fruitslist fruits;         if (enum.tryparse(fruit, true, out fruits))         {             var fruitweight = getfruitweight(fruits);             console.readline();         }         else         {             console.writeline("fruit name undefined!");             console.readline();         }     }      private static object getfruitweight(fruitslist fruitnumber)     {         switch (fruitnumber)         {             case fruitslist.banana:                 return new banana();             case fruitslist.apple:                 return new apple();         }          return null;     } }  internal class banana : ifruits {     public float returnfruitweight()     {         return (float)10.00;     } }  public interface ifruits {     float returnfruitweight(); }  public class apple : ifruits {     public float returnfruitweight()     {         return (float)30.00;     } }  public enum fruitslist {     apple,     banana, } 

the whole idea of mine (from theoretical understanding) getfruitweights function shall accept enum type , return fruit weight. able object of particular fruit stuck how fruit object, weight of fruit.

also, add doubts list, following factory pattern in implementation? , also, materials in internet uses abstract class ? should follow ? interface implementation or should use abstract class , override ?

thanks in advance help.

if have no intention of using fruit object when it's returned return weight directly rather fruit object. factory method name returns weight, not object, in theory it's correct approach i.e.

const float appleweight = 10; const float bananaweight = 10.4; ... public static float getfruitweight(fruitlist fruittype) {     switch (fruittype)     {         case fruitslist.apple:             return appleweight;         case fruitslist.banana:             return bananaweight;         default:             return 0;     } } 

however, if intend use fruit object rename method getfruit, return ifruits (don't box) , call returnfruitweight on fruit instance weight.

am following factory pattern in implementation?

the point of factory pattern allow create objects without knowing concrete type, yes have here basic example of factory method pattern.

some materials in internet uses abstract class ? should follow ? interface implementation or should use abstract class , override ?

this dependant on application design. example, introduce abstract base class if:

  • there common code share across classes
  • i needed way of determining particular class belonged family of particular type i.e. banana type of fruit.

other that, go interfaced type approach. bare in mind, can have both, there's no reason why couldn't have abstract base class supports particular interface...


Comments