Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace Factory
- {
- public interface IFactory
- {
- void Drive(int miles);
- }
- public class Scooter : IFactory
- {
- public void Drive(int miles)
- {
- Console.WriteLine("Drive the Scooter : " + miles.ToString() + "km");
- }
- }
- public class Bike : IFactory
- {
- public void Drive(int miles)
- {
- Console.WriteLine("Drive the Bike : " + miles.ToString() + "km");
- }
- }
- public abstract class VehicleFactory
- {
- public abstract IFactory GetVehicle(string Vehicle);
- }
- 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));
- }
- }
- }
- 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();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement