I've created factory to create WPF controls. I have string with control name and I map it to enum. I think it's bad implementation of factory pattern. So please show me where I've made mistake.
Main interface
public interface IWpfControl
{
string Name { get; set; }
double ActualHeight { get; }
double ActualWidth { get; }
double Height { get; set; }
double Width { get; set; }
Size InitialSize { get; set; }
}
Second interface
public interface IWpfControlWithItems : IWpfControl
{
ItemCollection Items { get; }
}
Factory class
public class WpfControlFactory
{
public static IWpfControl CreateWpfControl(WpfControl wpfControl)
{
object[] controlItems = {};
IWpfControl control = null;
switch (wpfControl)
{
case WpfControl.Button:
control = new WpfButton { InitialSize = new Size(85, 20) };
break;
case WpfControl.CheckBox:
control = new WpfCheckBox { InitialSize = new Size(85, 20) };
break;
case WpfControl.TextBox:
control = new WpfTextBox { InitialSize = new Size(75, 20) };
break;
case WpfControl.ComboBox:
break;
case WpfControl.ListBox:
control = new WpfListBox { InitialSize = new Size(100, 110) };
controlItems = CreateListItems();
break;
case WpfControl.GroupBox:
control = new WpfGroupBox { InitialSize = new Size(120, 100) };
break;
case WpfControl.RadioButton:
control = new WpfRadioButton { InitialSize = new Size(85, 20) };
break;
case WpfControl.TextBlock:
control = new WpfTextBlock { InitialSize = new Size(75, 20) };
break;
case WpfControl.Image:
control = new WpfImage { InitialSize = new Size(100, 100) };
break;
case WpfControl.HScrollBar:
control = new WpfHScrollBar { InitialSize = new Size(130, 20), Orientation = Orientation.Horizontal };
break;
case WpfControl.VScrollBar:
control = new WpfVScrollBar { InitialSize = new Size(20, 130), Orientation = Orientation.Horizontal };
break;
case WpfControl.Slider:
control = new WpfSlider { InitialSize = new Size(130, 20) };
break;
case WpfControl.ProgressBar:
control = new WpfProgressBar { InitialSize = new Size(130, 15), Value = 75};
break;
case WpfControl.ListView:
var gridView = new GridView();
gridView.Columns.Add(new GridViewColumn
{
Header = "Items",
});
control = new WpfListView { InitialSize = new Size(100, 110), View = gridView };
controlItems = CreateListItems();
break;
case WpfControl.TreeView:
control = new WpfTreeView { InitialSize = new Size(150, 100) };
controlItems = CreateTreeViewItems();
break;
case WpfControl.TabControl:
control = new WpfTabControl { InitialSize = new Size(120, 80) };
controlItems = CreateTabControlItems();
break;
case WpfControl.RichTextBox:
control = new WpfRichTextBox { InitialSize = new Size(80, 80) };
break;
case WpfControl.DatePicker:
control = new WpfDatePicker { InitialSize = new Size(130, 25) };
break;
}
if (control != null)
{
control.Name = Consts.DefaultEaControlName;
}
var controlWithItems = control as IWpfControlWithItems;
if (controlWithItems != null)
{
AddElementToItem(controlWithItems, controlItems);
}
return control;
}
private static void AddElementToItem(IWpfControlWithItems wpfControl, params object[] items)
{
foreach (var item in items)
{
wpfControl.Items.Add(item);
}
}
private static object[] CreateTreeViewItems()
{
var rootExpandedNode = new TreeViewItem { Header = "Expanded Node", IsExpanded = true };
var expandedNode = new TreeViewItem { Header = "Expanded Node", IsExpanded = true };
var collapsedNode = new TreeViewItem { Header = "Collapsed Node", IsExpanded = false };
var leaf1 = new TreeViewItem { Header = "Leaf" };
var leaf2 = new TreeViewItem { Header = "Leaf" };
var leaf3 = new TreeViewItem { Header = "Leaf" };
expandedNode.Items.Add(leaf1);
expandedNode.Items.Add(leaf2);
rootExpandedNode.Items.Add(expandedNode);
rootExpandedNode.Items.Add(collapsedNode);
return new object[] { rootExpandedNode, leaf3};
}
private static object[] CreateTabControlItems()
{
return new object[]
{
new TabItem { Header = "Tab1"} ,
new TabItem { Header = "Tab2" },
new TabItem { Header = "Tab3" }
};
}
private static object[] CreateListItems()
{
return new object[] { "item1", "item2", "item3", "item4", "item5" };
}
}
These controlItems
are always the same and it never change so I don't pass it in constructor.