Advertisement
Guest User

Untitled

a guest
May 25th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.37 KB | None | 0 0
  1.     using System;
  2.     namespace Factory
  3.     {
  4.      public interface IFactory
  5.      {
  6.      void Drive(int miles);
  7.      }
  8.      
  9.      public class Scooter : IFactory
  10.      {
  11.      public void Drive(int miles)
  12.      {
  13.      Console.WriteLine("Drive the Scooter : " + miles.ToString() + "km");
  14.      }
  15.      }
  16.      
  17.      public class Bike : IFactory
  18.      {
  19.      public void Drive(int miles)
  20.      {
  21.      Console.WriteLine("Drive the Bike : " + miles.ToString() + "km");
  22.      }
  23.      }
  24.      
  25.      public abstract class VehicleFactory
  26.      {
  27.      public abstract IFactory GetVehicle(string Vehicle);
  28.      
  29.      }
  30.      
  31.      public class ConcreteVehicleFactory : VehicleFactory
  32.      {
  33.      public override IFactory GetVehicle(string Vehicle)
  34.      {
  35.      switch (Vehicle)
  36.      {
  37.      case "Scooter":
  38.      return new Scooter();
  39.      case "Bike":
  40.      return new Bike();
  41.      default:
  42.      throw new ApplicationException(string.Format("Vehicle '{0}' cannot be created", Vehicle));
  43.      }
  44.      }
  45.      
  46.      }
  47.      
  48.      class Program
  49.      {
  50.      static void Main(string[] args)
  51.      {
  52.      VehicleFactory factory = new ConcreteVehicleFactory();
  53.      
  54.      IFactory scooter = factory.GetVehicle("Scooter");
  55.      scooter.Drive(10);
  56.      
  57.      IFactory bike = factory.GetVehicle("Bike");
  58.      bike.Drive(20);
  59.      
  60.      Console.ReadKey();
  61.      
  62.      }
  63.      }
  64.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement