Skip to content

Commit 13a1c72

Browse files
committed
factory
0 parents  commit 13a1c72

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed

factory/abstract_factory.linq

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<Query Kind="Program" />
2+
3+
void Main()
4+
{
5+
new NavigationBar(new Android());
6+
new DropdownMenu(new Android());
7+
}
8+
9+
public class NavigationBar
10+
{
11+
public NavigationBar(IUIFactory factory) => factory.CreateButton();
12+
}
13+
14+
public class DropdownMenu
15+
{
16+
public DropdownMenu(IUIFactory factory) => factory.CreateButton();
17+
}
18+
19+
public interface IUIFactory
20+
{
21+
public Button CreateButton();
22+
23+
public Button CreateButton();
24+
}
25+
26+
public class Apple : IUIFactory
27+
{
28+
public Button CreateButton()
29+
{
30+
return new Button { Type = "iOS Button".Dump() };
31+
}
32+
}
33+
34+
public class Android : IUIFactory
35+
{
36+
public Button CreateButton()
37+
{
38+
return new Button { Type = "Android Button".Dump() };
39+
}
40+
}
41+
42+
public class Button
43+
{
44+
public string Type { get; set; }
45+
}

factory/factory.linq

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<Query Kind="Program" />
2+
3+
void Main()
4+
{
5+
new NavigationBar();
6+
new DropdownMenu();
7+
}
8+
9+
public class NavigationBar {
10+
public NavigationBar() => ButtonFactory.CreateButton();
11+
}
12+
13+
public class DropdownMenu
14+
{
15+
public DropdownMenu() => ButtonFactory.CreateButton();
16+
}
17+
18+
public class ButtonFactory
19+
{
20+
public static Button CreateButton()
21+
{
22+
return new Button { Type = "Red Button".Dump() };
23+
}
24+
}
25+
26+
public class Button
27+
{
28+
public string Type { get; set; }
29+
}

factory/factory_method.linq

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<Query Kind="Program" />
2+
3+
void Main()
4+
{
5+
new NavigationBar();
6+
new DropdownMenu();
7+
new AndroidNavigationBar();
8+
new AndroidDropdownMenu();
9+
}
10+
11+
public abstract class Element
12+
{
13+
protected abstract Button CreateButton();
14+
15+
public Element() => CreateButton();
16+
}
17+
18+
public class NavigationBar : Element
19+
{
20+
protected override Button CreateButton()
21+
{
22+
return new Button { Type = "Default Button".Dump() };
23+
}
24+
}
25+
26+
public class DropdownMenu : Element
27+
{
28+
protected override Button CreateButton()
29+
{
30+
return new Button { Type = "Default Button".Dump() };
31+
}
32+
}
33+
34+
public class AndroidNavigationBar : Element
35+
{
36+
protected override Button CreateButton()
37+
{
38+
return new Button { Type = "Android Button".Dump() };
39+
}
40+
}
41+
42+
public class AndroidDropdownMenu : Element
43+
{
44+
protected override Button CreateButton()
45+
{
46+
return new Button { Type = "Android Button".Dump() };
47+
}
48+
}
49+
50+
public class Button
51+
{
52+
public string Type { get; set; }
53+
}

0 commit comments

Comments
 (0)