Design Mode and instance code: Template Method Mode

Source: Internet
Author: User

Intent/Definition: Define an operationAlgorithmTo extend some steps to sub-classes. This allows subclass to redefine the steps of the Algorithm without changing the structure of the algorithm.

Problem: to complete a process or a series of steps at a specific level of detail, the individual steps may be different at a more detailed level.

Solution: allow the definition of variable sub-steps while keeping the basic process consistent

Participants and collaborators: The template method is composed of an abstract class that defines the basic methods to be overwritten. Each class derived from this abstract class will implement a new method for this template.

Effect: The template provides a betterCodeUse the platform. It also helps ensure the implementation of the required steps. It binds the override steps of each specific class, so the template method is applied only when these changes always occur and can only happen together.

Implementation: create a one-person abstract class and use an abstract method to implement a process. These abstract methods must be implemented in subclass to execute every step of the process. If these steps change independently, each step can be implemented in the Policy mode.

The following is an example of a template method. Two classes are used to access the database. The two classes implement common operations in the base class, such as opening or closing a connection, however, in this connection, the specific table and processing are implemented by the Base class.

Using system; using system. data; using system. data. oledb; namespace template {// <summary> // mainapp startup class for real-world // template design pattern. /// </Summary> class mainapp {/// <summary> // entry point into console application. /// </Summary> static void main () {dataaccessobject daocategories = new categories (); daocategories. run (); dataaccessobject daoproducts = new products (); daoproducts. run (); // wait for user console. readkey () ;}/// <summary> // The 'abstractclass' abstract class // </Summary> abstract class dataaccessobject {protected string connectionstring; protected dataset; public Virtual void connect () {// make sure MDB is available to app connectionstring = "provider = Microsoft. jet. oledb.4.0; "+" Data Source = .. \\.. \\.. \ db1.mdb ";} public abstract void select (); public abstract void process (); Public Virtual void disconnect () {connectionstring = "";} // The 'template method' public void run () {connect (); select (); process (); disconnect ();}} /// <summary> // A 'concreteclass' class // </Summary> class categories: dataaccessobject {public override void select () {string SQL = "select categoryname from categories"; oledbdataadapter dataadapter = new oledbdataadapter (SQL, connectionstring); dataset = new dataset (); dataadapter. fill (dataset, "categories");} public override void process () {console. writeline ("categories ----"); datatable = dataset. tables ["categories"]; foreach (datarow row in datatable. rows) {console. writeline (row ["categoryname"]);} console. writeline () ;}/// <sumsummary> // A 'concreteclass' class /// </Summary> class products: dataaccessobject {public override void select () {string SQL = "select productname from products"; oledbdataadapter dataadapter = new oledbdataadapter (SQL, connectionstring); dataset = new dataset (); dataadapter. fill (dataset, "Products");} public override void process () {console. writeline ("products ----"); datatable = dataset. tables ["Products"]; foreach (datarow row in datatable. rows) {console. writeline (row ["productname"]);} console. writeline ();}}}

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.