Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • Factory Design Pattern in .NET

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 49
    Comment on it

    In this design pattern interface is used to create an object but let the subclass decide which class to instantiate

    Factory design pattern we will create the base interface called as factory

    After that we will create the methods associated with it

    Then we will create the classes that will implement these interfaces and invoke these classes

     

     

        using System;
        namespace Factory
        {
         /// <summary>
         /// The 'Product' interface
         /// </summary>
         public interface IFactory
         {
         void Drive(int miles);
         }
         
         /// <summary>
         /// A 'ConcreteProduct' class
         /// </summary>
         public class Scooter : IFactory
         {
         public void Drive(int miles)
         {
         Console.WriteLine("Drive the Scooter : " + miles.ToString() + "km");
         }
         }
         
         /// <summary>
         /// A 'ConcreteProduct' class
         /// </summary>
         public class Bike : IFactory
         {
         public void Drive(int miles)
         {
         Console.WriteLine("Drive the Bike : " + miles.ToString() + "km");
         }
         }
         
         /// <summary>
         /// The Creator Abstract Class
         /// </summary>
         public abstract class VehicleFactory
         {
         public abstract IFactory GetVehicle(string Vehicle);
         
         }
         
         /// <summary>
         /// A 'ConcreteCreator' class
         /// </summary>
         public class ConcreteVehicleFactory : VehicleFactory
         {
         public override IFactory GetVehicle(string Vehicle)
         {
         switch (Vehicle)
         {
         case "Scooter":
         return new Scooter();
         case "Bike":
         return new Bike();
         default:
         throw new ApplicationException(string.Format("Vehicle '{0}' cannot be created", Vehicle));
         }
         }
         
         }
         
         /// <summary>
         /// Factory Pattern Demo
         /// </summary>
         class Program
         {
         static void Main(string[] args)
         {
         VehicleFactory factory = new ConcreteVehicleFactory();
         
         IFactory scooter = factory.GetVehicle("Scooter");
         scooter.Drive(10);
         
         IFactory bike = factory.GetVehicle("Bike");
         bike.Drive(20);
         
         Console.ReadKey();
         
         }
         }
        }

     

     

     

    .net

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: