Repetitive Labor
In a main project, there are often multiple sub-functions of the entrance, which may be a button, there may be a drop-down menu.
Initially developed, it must be easy to place a button on the interface, give it a name, and then write a piece of code into his click event to let the child function interface pop up.
But as the number of sub-functions continues to change, this inconspicuous few small jobs, we are constantly repeating.
The most headache is, suddenly one day, your PM to tell you that this interface is not, or the sub-function of the entrance to another form, you can not remember this obscure lines of code where.
What's the problem?
This design pattern is also the easiest way for beginners to go.
We analyze logically that the function module should be determined by itself what to display on the interface and how it should be opened.
Imagine our Windows operating system, the icon of each program, and the text on the icon, should be determined by the program itself, not the operating system, so that these icons, text, and even its existence, can easily be modified.
Use the interface to return functions that are "sub-features" to "sub-features"
We can imitate the practice of windows, compared to the label, text, and open "What to do", these things all made an interface
1 /// <summary>2 ///defines an interface for describing a sub-function module3 /// </summary>4 Public InterfaceIsubmoudle5 {6 /// <summary>7 ///Feature name8 /// </summary>9 stringModuleName {Get; }Ten One /// <summary> A ///the path to the icon file, or null if the icon is not used - /// </summary> - stringIconPath {Get; } the - /// <summary> - ///Open this function module - /// </summary> + voidOpen (); -}
All of the sub-functional modules We make later implement this interface, for example
1 /// <summary>2 ///a sub-function that shows the current time3 /// </summary>4 Public classShowtimemoudle:isubmoudle5 {6 7 Public stringModuleName8 {9 Get{return "Show Time"; }Ten } One A Public stringIconPath - { - Get{return ""; } the } - - Public voidOpen () - { + Console.WriteLine (DateTime.Now.ToString ()); - } +}
The main content within the function is in the open method.
Using sub-functions
The sub-function is already defined.
Let's use it.
First of all, to the main function module, we want to have a list
All Isubmoudle are recorded in detail in the list.
We then dynamically output this content to a form, a console, or a web, and we take the console as an example
1 Static voidMain (string[] args)2 {3List<isubmoudle> moudlelist =NewList<isubmoudle>();4Moudlelist.add (NewShowtimemoudle ());5 6 //start to output all the features in Moudlelist7 for(inti =0; i < Moudlelist.count; i++) 8 {9Console.WriteLine ("{0}: {1}", i +1, Moudlelist[i]. ModuleName);Ten } One A console.readline (); -}
Enter sub-function
The sub-function has been dynamically displayed, and we are ready to use the corresponding numbers in front of each self-function.
In fact, we analyze the input characters, convert to the number type, in the moudlelist to find the corresponding position, and then call its open () method
1List<isubmoudle> moudlelist =NewList<isubmoudle>();2Moudlelist.add (NewShowtimemoudle ());3 4 //start to output all the features in Moudlelist5 for(inti =0; i < Moudlelist.count; i++) 6 {7Console.WriteLine ("{0}: {1}", i +1, Moudlelist[i]. ModuleName);8 }9 Ten stringcmd =console.readline (); One intj =int. Parse (cmd); AIsubmoudle Moudle = moudlelist[j-1]; - Moudle. Open (); - theConsole.ReadLine ();
10-13 lines is the appended code
Test
End
Above, is to demonstrate another use of the interface: dynamically expand your program, the focus of your development is only placed within the function module.
Here, we output all the submodules to the console interface in a run-time manner:
What is an interface (Getting started)--Making Your program function rich