1+ < Query Kind = "Program" / >
2+
3+ void Main ( )
4+ {
5+
6+ }
7+
8+
9+ public class Icon
10+ {
11+ public Icon ( string type )
12+ {
13+ // load icon
14+ }
15+ }
16+
17+ static class ButtonProvider
18+ {
19+ private static Dictionary < string , Button > _cache = new Dictionary < string , Button > ( ) ;
20+
21+ public static Button GetButton ( string type , Func < Button > buttonFactory )
22+ {
23+ if ( ! _cache . ContainsKey ( type ) )
24+ {
25+ _cache [ type ] = buttonFactory ( ) ;
26+ }
27+
28+ return _cache [ type ] ;
29+ }
30+ }
31+
32+ static Button SettingsButtonFactory ( ) => new SettingsButton ( ) ;
33+
34+ abstract class Button
35+ {
36+ // intrinsic state
37+ public Icon Icon { get ; set ; }
38+ public abstract void Click ( ) ;
39+ }
40+
41+ class SettingsButton : Button
42+ {
43+ public SettingsButton ( )
44+ {
45+ Icon = new Icon ( "settings" ) ;
46+ }
47+
48+ public override void Click ( )
49+ {
50+ // do something
51+ }
52+ }
53+
54+ class DropdownButton : Button
55+ {
56+ Button _btn ;
57+ public DropdownButton ( Button btn , params string [ ] options )
58+ {
59+ _btn = btn ;
60+ }
61+
62+ public override void Click ( )
63+ {
64+ _btn . Click ( ) ; // do the original thing
65+ // display options
66+ }
67+ }
68+
69+ class SolutionWindow
70+ {
71+ Button settings = new DropdownButton ( ButtonProvider . GetButton ( "settings" , SettingsButtonFactory ) , "option1" , "option2" ) ;
72+ }
73+
74+ class TerminalWindow
75+ {
76+ Button settings = new DropdownButton ( ButtonProvider . GetButton ( "settings" , SettingsButtonFactory ) , "option2" , "option3" , "option4" ) ;
77+ }
78+
79+ class TestRunnerWindow
80+ {
81+ Button settings = new DropdownButton ( ButtonProvider . GetButton ( "settings" , SettingsButtonFactory ) , "option1" , "option2" , "option4" ) ;
82+ }
0 commit comments