Abstract Factory Pattern

Unknownahmad
Posted by Unknownahmad under ASP.NET category on | Points: 40 | Views : 474
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AbstractFactory
{
interface ShaleemarExpress
{
string ReservePassenger();
}
interface KarakaramExpress
{
string ReservePassenger1();
}
//shaleemar
public class Student : ShaleemarExpress
{
public string ReservePassenger()
{
return "Student has been Reserved";
}
}

public class Traveller : ShaleemarExpress
{
public string ReservePassenger()
{
return "Traveler has been Reserved";
}
}

public class Employee : ShaleemarExpress
{
public string ReservePassenger()
{
return "Employee has been Reserved";
}
}

//karakaram
public class Student1 : KarakaramExpress
{
public string ReservePassenger1()
{
return "Student1 has been Reserved";
}
}

public class Traveller1 : KarakaramExpress
{
public string ReservePassenger1()
{
return "Traveler1 has been Reserved";
}
}

public class Employee1 : KarakaramExpress
{
public string ReservePassenger1()
{
return "Employee1 has been Reserved";
}
}

interface ReservingFactory
{
ShaleemarExpress Reservepass();
KarakaramExpress Reservepass1();
}
//concrete factories
class ShaleemarStudentFactory : ReservingFactory
{
public ShaleemarExpress Reservepass()
{
return new Student();

}

public KarakaramExpress Reservepass1()
{
return new Student1();
}
}
//concrete factories
class ShaleemarEmployeeFactory : ReservingFactory
{
public ShaleemarExpress Reservepass()
{
return new Employee();
// return new Employee();
//return new Traveller();
}

public KarakaramExpress Reservepass1()
{
return new Employee1();
}
}
//concrete factories
class ShaleemarTravelerFactory : ReservingFactory
{
public ShaleemarExpress Reservepass()
{
return new Traveller();
// return new Employee();
//return new Traveller();
}

public KarakaramExpress Reservepass1()
{
return new Traveller1();
}
}

//this is client
enum MANUFACTURERS
{
Student,
Employee,
Traveler
}
class Reserver
{
//ShaleemarExpress shal;
//KarakaramExpress kar;
ReservingFactory factory;
MANUFACTURERS manu;

public Reserver(MANUFACTURERS m)
{
manu = m;
}
public void CheckAll()
{
switch (manu)
{
case MANUFACTURERS.Student:
factory = new ShaleemarStudentFactory();
break;
case MANUFACTURERS.Employee:
factory = new ShaleemarEmployeeFactory();
break;
case MANUFACTURERS.Traveler:
factory = new ShaleemarTravelerFactory();
break;
}

Console.WriteLine(manu.ToString() + ":\nShaleemar Passenger: " +
factory.Reservepass().ReservePassenger() + "\nKarakaram Passenger: " + factory.Reservepass1().ReservePassenger1());

// Console.WriteLine(manu.ToString() + ":\nShaleemar Passenger: " +
//factory.Reservepass2().ReservePassenger() + "\nKarakaram Passenger: " + factory.Reservepass3().ReservePassenger());

}
}
class Program
{
static void Main(string[] args)
{
//for Traveler
Reserver res = new Reserver(MANUFACTURERS.Traveler);
res.CheckAll();

Console.WriteLine("----------------------");

//for Student
res = new Reserver(MANUFACTURERS.Student);
res.CheckAll();

Console.WriteLine("----------------------");

//for Employee
res = new Reserver(MANUFACTURERS.Employee);
res.CheckAll();
Console.ReadLine();
}
}
}

Comments or Responses

Posted by: Rajeshatkiit on: 12/17/2015 Level:Starter | Status: [Member] | Points: 10
Plz simplify it.

Login to post response