Design Pattern (Abstract Factory Pattern)
Hi Friends U can get help from this
The intent of abstract factory Pattern is to provide interface of creating family of related or dependent object without specifying concert product class.
Let’s take example for developing web and windows application control. We have factory for Button controls having WebButton, WindowsButton and we have factory for TextBox control WebTextBox, WindowsTextBox. Now I want to create object for web or windows without specifying there concrete classes. In this situation I will use abstract factory.
The participant in the Abstract factory Pattern:
- AbstractFactory: declare interface that create abstract product. In our example Application is AbstractFactory class.
- ConcreteFactory: implement operation to create concrete product object. In our case webApplication and WindowApplication is ConcreteFactory
- AbstractProduct: Declare interface for type of product. In our case control is Abstract product.
- Product: define product which is created by correspondent ConcreteFactory. In our example Button and TextBox is Prroduct.
- Client: use interface declare by abstractFactory and AbstractProduct classes.
Class Implementation
namespace FactoryPattern
{
///
/// Abstract Factory Class
///
public abstract class MyApplicationFactory
{
public abstract MyButton CreateButton();
public abstract MyTextBox CreateText();
}
{
///
/// Abstract Factory Class
///
public abstract class MyApplicationFactory
{
public abstract MyButton CreateButton();
public abstract MyTextBox CreateText();
}
///
/// ConcreteFactory Class1
///
/// ConcreteFactory Class1
///
public class WebFactory : MyApplicationFactory
{
public override MyButton CreateButton()
{
return new WebButton();
}
{
public override MyButton CreateButton()
{
return new WebButton();
}
public override MyTextBox CreateText()
{
return new WebTextBox();
}
}
{
return new WebTextBox();
}
}
///
/// ConcreteFactory Class2
///
/// ConcreteFactory Class2
///
public class WindowFactory : MyApplicationFactory
{
public override MyButton CreateButton()
{
return new WindowButton();
}
{
public override MyButton CreateButton()
{
return new WindowButton();
}
public override MyTextBox CreateText()
{
return new WindowTextBox();
}
}
{
return new WindowTextBox();
}
}
///
/// Abstract product 1
///
/// Abstract product 1
///
public abstract class MyButton
{
public abstract void GetInformation();
}
{
public abstract void GetInformation();
}
///
/// Abstract product 2
///
public abstract class MyTextBox
{
public abstract void GetInformation();
}
/// Abstract product 2
///
public abstract class MyTextBox
{
public abstract void GetInformation();
}
///
/// Product 1.1
///
/// Product 1.1
///
public class WebButton: MyButton
{
public override void GetInformation()
{
Console.Write("\nWebButton");
}
}
{
public override void GetInformation()
{
Console.Write("\nWebButton");
}
}
///
/// Product 1.2
///
/// Product 1.2
///
public class WindowButton : MyButton
{
public override void GetInformation()
{
Console.Write("\nWindowButton");
}
}
{
public override void GetInformation()
{
Console.Write("\nWindowButton");
}
}
///
/// Product 2.1
///
/// Product 2.1
///
public class WebTextBox : MyTextBox
{
public override void GetInformation()
{
Console.Write("\nWebTextBox");
}
}
{
public override void GetInformation()
{
Console.Write("\nWebTextBox");
}
}
///
/// Product 2.2
///
/// Product 2.2
///
public class WindowTextBox : MyTextBox
{
public override void GetInformation()
{
Console.Write("\nWindowTextBox");
}
}
{
public override void GetInformation()
{
Console.Write("\nWindowTextBox");
}
}
///
/// Client Class
///
/// Client Class
///
public class MyApplication
{
MyButton _button;
MyTextBox _textbox;
public MyApplication(MyApplicationFactory objFactory)
{
_button = objFactory.CreateButton();
_textbox = objFactory.CreateText();
}
{
MyButton _button;
MyTextBox _textbox;
public MyApplication(MyApplicationFactory objFactory)
{
_button = objFactory.CreateButton();
_textbox = objFactory.CreateText();
}
public void GetDetail()
{
_button.GetInformation();
_textbox.GetInformation();
}
}
}
{
_button.GetInformation();
_textbox.GetInformation();
}
}
}
with thanks
RS Prajapati
hi! friends i have get help from it.thanks
ReplyDeleteSatyam