Participants
·
AbstractFactory: declares an interface for operations that
create abstract products
·
ConcreteFactory: implements the operations to create concrete
product objects
·
AbstractProduct: declares an interface for a type of product
object
·
Product: defines a product object to be created by the
corresponding concrete factory implements theAbstractProduct interface
·
Client: uses interfaces declared by AbstractFactory and AbstractProduct classes
Points to
remember about abstract Factory :
Ø
AbstractFactory
class declares only an interface for creating the products. The actual creation
is the task of the ConcreteProduct classes, where a good approach is applying
the Factory Method design pattern for each product of the family.
Ø
Extending
factories can be done by using one Create method for all products and attaching
information about the type of product needed.
Where to use
abstract factory :
Ø
The
system needs to be independent from the way the products it works with are
created.
Ø
The
system is or should be configured to work with multiple families of products.
Ø
A
family of products is designed to work only all together.
Ø
The
creation of a library of products is needed, for which is relevant only the
interface, not the implementation, too.
Step wise
creation
Ø
Step
1: Define Abstract Product
Ø
Step
2: Define Abstract Factory
Ø
Step
3: Define Concrete Products
Ø
Step
4: Define Concrete Factory
Ø
Step
5: Define Client
Implimentation
//Step 1: Define Abstract Product
// Abstract Products
interface ITwoGService
{
string LowSpeedInternet();
}
// Abstract Products
interface IThreeGService
{
string HighSpeedInternet();
}
//Step 2: Define Abstract Factory
//Abstract Factory
interface ISeviceProvider
{
ITwoGService GetInternetSlowService();
IThreeGService GetInternetFastService();
}
//Step 3: Define Concrete Products
//Concrete Products
class LowSpeedInternetIdea
: ITwoGService
{
string ITwoGService.LowSpeedInternet()
{
return "Low Speed
Service 4MPS";
}
}
//Concrete Products
class HighInternetIdea
: IThreeGService
{
public string
HighSpeedInternet()
{
return "High Speed
Service 10MPS";
}
}
//Concrete Products
class LowSpeedInternetAirtel
: ITwoGService
{
string ITwoGService.LowSpeedInternet()
{
return "Low Speed
Service 4MPS";
}
}
//Concrete Products
class HighInternetAirtel
: IThreeGService
{
public string
HighSpeedInternet()
{
return "High Speed
Service 10MPS";
}
}
//Step 4: Define Concrete Factory
//Concrete Factory
class AirtelFactory
: ISeviceProvider
{
public ITwoGService
GetInternetSlowService()
{
return new LowSpeedInternetAirtel();
}
public IThreeGService
GetInternetFastService()
{
return new HighInternetAirtel();
}
}
//Concrete Factory
class IdeaFactory
: ISeviceProvider
{
public ITwoGService
GetInternetSlowService()
{
return new LowSpeedInternetIdea();
}
public IThreeGService
GetInternetFastService()
{
return new HighInternetIdea();
}
}
//Step 5: Define Client
enum ServiceProviders
{
Idea,
Airtel
}
class MobileInternetServiceProvider
{
IThreeGService two;
ITwoGService threeg;
ISeviceProvider factory;
ServiceProviders manu;
public MobileInternetServiceProvider(ServiceProviders m)
{
manu = m;
}
public void
CheckProducts()
{
switch (manu)
{
case ServiceProviders.Idea:
factory = new IdeaFactory();
break;
case ServiceProviders.Airtel:
factory = new AirtelFactory();
break;
}
Console.WriteLine(manu.ToString() + ":\n Low Speed Internet: " +
factory.GetInternetSlowService().LowSpeedInternet() + "\n High Speed Internet: " +
factory.GetInternetFastService().HighSpeedInternet());
}
}
class Program
{
static void Main(string[] args)
{
MobileInternetServiceProvider checker = new MobileInternetServiceProvider(ServiceProviders.Airtel);
checker.CheckProducts();
checker = new MobileInternetServiceProvider(ServiceProviders.Idea);
checker.CheckProducts();
}
}
No comments:
Post a Comment