using System;
namespace AbstractFactory_Me
{
public interface IAbstractFactory{
IAbstractProductA CreateProductA();
IAbstractProductB CreateProductB();
}
public interface IAbstractProductA{
string ShowSelf();
string ShowOther(IAbstractProductB b);
}
public class ProductA1 : IAbstractProductA{
public ProductA1(){}
public string ShowSelf(){
return this.ToString();
}
public string ShowOther(IAbstractProductB b){
return b.ToString();
}
}
public class ProductA2 : IAbstractProductA{
public ProductA2(){}
public string ShowSelf(){
return this.ToString();
}
public string ShowOther(IAbstractProductB b){
return b.ToString();
}
}
public interface IAbstractProductB{
string ShowSelf();
string ShowOther(IAbstractProductA a);
}
public class ProductB1 : IAbstractProductB{
public string ShowSelf(){
return this.ToString();
}
public string ShowOther(IAbstractProductA a){
return a.ToString();
}
}
public class ProductB2 : IAbstractProductB{
public string ShowSelf(){
return this.ToString();
}
public string ShowOther(IAbstractProductA a){
return a.ToString();
}
}
public class ConcreteFactory1 : IAbstractFactory{
public IAbstractProductA CreateProductA(){
return new ProductA1();
}
public IAbstractProductB CreateProductB(){
return new ProductB1();
}
}
public class ConcreteFactory2 : IAbstractFactory{
public IAbstractProductA CreateProductA(){
return new ProductA2();
}
public IAbstractProductB CreateProductB(){
return new ProductB2();
}
}
public class Client{
public void run(){
IAbstractFactory factory1 = new ConcreteFactory1();
IAbstractProductA a = factory1.CreateProductA();
a.ShowSelf();
IAbstractProductB b = factory1.CreateProductB();
b.ShowSelf();
b.ShowOther(a);
}
}
}
private void Form1_Load(object sender, System.EventArgs e) {
this.richTextBox1.Clear();
IAbstractFactory factory1 = new ConcreteFactory1();
IAbstractProductA a = factory1.CreateProductA();
IAbstractProductB b = factory1.CreateProductB();
this.richTextBox1.AppendText(a.ShowSelf()+"\n");
this.richTextBox1.AppendText(b.ShowSelf()+"\n");
this.richTextBox1.AppendText(b.ShowOther(a)+"\n");
this.richTextBox1.AppendText(a.ShowOther(b)+"\n\n\n");
this.richTextBox1.AppendText(a.GetType().ToString()+"\n");
this.richTextBox1.AppendText(b.GetType().ToString()+"\n");
}