/// <summary> /// 界面皮肤工厂接口,充当抽象工厂 /// </summary> public interface ISkinFactory { IButton CreateButton(); ITextField CreateTextField(); IComboBox CreateComboBox(); } /// <summary> /// 按钮接口,充当抽象产品 /// </summary> public interface IButton { void Display(); } /// <summary> /// 组合框接口,充当抽象产品 /// </summary> public interface IComboBox { void Display(); } /// <summary> /// 文本框接口,充当抽象产品 /// </summary> public interface ITextField { void Display(); } /// <summary> /// Spring皮肤工厂,充当具体工厂 /// </summary> public class SpringSkinFactory : ISkinFactory { public IButton CreateButton() { return new SpringButton(); } public ITextField CreateTextField() { return new SpringTextField(); } public IComboBox CreateComboBox() { return new SpringComboBox(); } } /// <summary> /// Spring按钮类,充当具体产品 /// </summary> public class SpringButton : IButton { public void Display() { Console.WriteLine("显示浅绿色按钮。"); } } /// <summary> /// Spring文本框类,充当具体产品 /// </summary> public class SpringTextField : ITextField { public void Display() { Console.WriteLine("显示绿色边框文本框。"); } } /// <summary> /// Spring组合框类,充当具体产品 /// </summary> public class SpringComboBox : IComboBox { public void Display() { Console.WriteLine("显示绿色边框组合框。"); } }
客户端调用:
ISkinFactory factory = new SpringSkinFactory(); IButton bt = factory.CreateButton(); bt.Display();
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:C#设计模式之抽象工厂 - Python技术站