ITPub博客

首页 > 应用开发 > IT综合 > 设计模式之C#实现(一)--AbstractFactory之二(转)

设计模式之C#实现(一)--AbstractFactory之二(转)

原创 IT综合 作者:hphubei 时间:2007-07-08 15:04:49 0 删除 编辑
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);

}

}

}

我们在测试的WinForm里面放了一个richTextBox1实例,他用来显示结构。

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()+" ");

this.richTextBox1.AppendText(b.ShowSelf()+" ");

this.richTextBox1.AppendText(b.ShowOther(a)+" ");

this.richTextBox1.AppendText(a.ShowOther(b)+" ");



this.richTextBox1.AppendText(a.GetType().ToString()+" ");

this.richTextBox1.AppendText(b.GetType().ToString()+" ");



}

为了清楚的说明问题我们在生成的对象中使用了ShowSelf和ShowOther的方法用来显示自己和另外一个对象。

如要查看书中该例子的C#实现请到这里:

http://www.csdn.net/Develop/read_article.asp?id=20949

希望本文可以对即将要学习设计模式或者还有疑惑的人提供帮助,由于作者的能力有限如果文中又不对的地方请指出,或者联系我(wu_jian830@hotmail.com)谢谢![@more@]

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/79548/viewspace-925305/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
  • 博文量
    181
  • 访问量
    274757