Template Method Mode

Source: Internet
Author: User

Think back to all the programs you have written. Many of them may have the following basic loop structure.

Initialize (); <br/> while (! Done () <br/>{< br/> idle (); <br/>}< br/> cleanup ();

Initialize the application first. Next, go to the main loop to complete the work that needs to be done. These work may be to handle GUI events, or to process database records. Finally, once the work is completed, the program will launch the main loop and clear the work before the program.

This structure is very common, so it can be encapsulated in a class named application. Then we can reuse this class in every new program we want to write. Think about it! We no longer need to write this loop.

For example, we can see all the components of this Standard Program, including initializing textreader and textwriter, and there is a main loop from the console. in reads the Fahrenheit temperature and converts the temperature to the Celsius temperature. Finally, an exit message is printed.

Using system; <br/> using system. io; </P> <p> public class ftocraw <br/> {<br/> Public static void main (string [] ARGs) <br/>{< br/> bool done = false; <br/> while (! Done) <br/>{< br/> string fahrstring = console. in. readline (); <br/> If (fahrstring = NULL | fahrstring. length = 0) <br/>{< br/> done = true; <br/>}< br/> else <br/>{< br/> double Fahr = double. parse (fahrstring); <br/> double Celcius = 5.0/9.0 * (Fahr-32); <br/> console. out. writeline ("F = {0}, c = {1}", Fahr, Celcius); <br/>}< br/> console. out. writeline ("ftoc exit"); <br/>}< br/>}

This program fully complies with the main loop structure. It initializes the program first, completes the work in the main loop, and finally cleans up the program and exits.

We can use the template method mode to separate this basic structure from the ftoc program. This mode puts all common code into the implementation method of an abstract base class. This implementation method completes the general algorithm, but all the implementation details are delivered to the abstract method of the base class.

In this way, for example, we can encapsulate this main loop structure in an abstract base class named application. The Code is as follows:

 Public abstract class application <br/>{< br/> private bool isdone = false; </P> <p> protected abstract void Init (); <br/> protected abstract void idle (); <br/> protected abstract void cleanup (); </P> <p> protected void setdone () <br/>{< br/> isdone = true; <br/>}</P> <p> protected bool done () <br/>{< br/> return isdone; <br/>}</P> <p> Public void run () <br/>{< br/> Init (); <br/> while (! Done () <br/>{< br/> idle (); <br/>}< br/> cleanup (); <br/>}< br/>}

This class depicts a General Main Loop application. From the run function, you can see the main loop. You can also see that all the work is delivered to the abstract method init, idle, and cleanup. The init method handles any initialization work required. the main work of the idle method handler is repeatedly called before the setdone method is called. All cleanup operations required before the cleanup method handler exits.

We can inherit the application to rewrite the ftoc class, just implement the abstract method in the application. For example, the Code is as follows:

Using system; <br/> using system. io; </P> <p> public class ftoctemplatemethod: application <br/>{< br/> private textreader input; <br/> private textwriter output; </P> <p> Public static void main (string [] ARGs) <br/>{< br/> New ftoctemplatemethod (). run (); <br/>}</P> <p> protected override void Init () <br/>{< br/> input = console. in; <br/> output = console. out; <br/>}</P> <p> protected override void idle () <br/>{< br/> string fahrstring = input. readline (); <br/> If (fahrstring = NULL | fahrstring. length = 0) <br/>{< br/> setdone (); <br/>}< br/> else <br/>{< br/> double Fahr = double. parse (fahrstring); <br/> double celclus = 5.0/9.0 * (Fahr-32); <br/> output. writeline ("F = {0}, c = {1}", Fahr, celclus ); <br/>}</P> <p> protected override void cleanup () <br/>{< br/> output. writeline ("ftoc exit"); <br/>}< br/>}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.