【发布时间】:2016-09-27 19:47:37
【问题描述】:
我正在尝试使用抽象工厂模式创建一个具有可自定义“主题”的表单应用程序(只是为了获得一些经验)。我已经创建了一个这样的主题工厂的实现:
public class BlueTheme : IThemeFactory
{
public Button CreateButton() => new BlueButton();
// ... more controls here ...
}
现在我通过 Form 的构造函数传递一个 IThemeFactory 实例:
private IThemeFactory _themeFactory;
public Form1(IThemeFactory theme)
{
_themeFactory = theme; // e.g. new BlueTheme()
InitializeComponent();
}
我的问题是:有没有办法让我的表单使用IThemeFactory.CreateButton() 方法来生成表单上的所有按钮?
【问题讨论】:
-
我正在考虑在初始化后用主题按钮替换(映射)所有普通按钮,但必须有更好的解决方案,对吧?
标签: c# winforms user-interface design-patterns factory-pattern