Template Method pattern of Design Pattern

Source: Internet
Author: User

We know that China is a "manufacturing power" rather than a "creating power". Why? Because the wisdom of the Chinese people is unparalleled, We have inherent talent for "Imitation", but it is still difficult from the perspective of "innovation. This time I will talk about the wisdom of Chinese people-"Imitation ". Speaking of "Imitation", you have to mention "template" because there is a "template" to imitate what you need. In other words, the domestic stockade machine was a popular one, so let's take a look at the "template method model" by making a mobile phone ". Code structure diagram: Here Telephone is an abstract class, which is actually an abstract template. It defines and implements a template method call (). This template method is a specific method that provides a skeleton of top-level logic, and the logical composition steps are postponed to subclass implementation in the corresponding abstract operations. Template code: [csharp] <SPAN style = "FONT-SIZE: 18px"> // call public abstract class Telephone {// public abstract void dial (); // implement the call function public abstract void communicate (); // call the public abstract void hangup (); // call the call-skeleton public void call () {// dial this first. dial (); // call this after receiving the call. communicate (); // after the call is completed, stop this. hangup (); Console. writeLine ("") ;}}</SPAN> // mobile phone public abstract class Telephone {// can call p Ublic abstract void dial (); // implement the call function public abstract void communicate (); // end the call public abstract void hangup (); // summarize, call-skeleton public void call () {// dial this first. dial (); // call this after receiving the call. communicate (); // after the call is completed, stop this. hangup (); Console. writeLine ("") ;}} Nokia code: [csharp] <SPAN style = "FONT-SIZE: 18px"> // cottage plan-Nokia class Nokia: telephone {public override void dial () {// unique sound Console of Nokia. writeLine ("Bell, Nokia users dialing... ");} public override void communicate () {Console. writeLine ("I have used Nokia for good news");} public override void hangup () {Console. writeLine ("Drop .. drop. ") ;}}</SPAN> // cottage Program-Nokia class Nokia: Telephone {public override void dial () {// unique sound Console of Nokia. writeLine ("Ling, Nokia users dialing... ");} public override void communicate () {Console. writeLine ("I have used Nokia for good news");} public override void hangup () {Console. writeLine ("Drop .. drop. ") ;}} Iphone code: [csharp] <SPAN style =" FONT-SIZE: 18px "> // cottage plan-Iphone class Iphone: Telephone {public override void dial () {// unique sound Console of Nokia. writeLine ("La la, Iphone dialing... ");} public override void communicate () {Console. writeLine ("Xiao Li, I bought an apple. Envy Me... ");} public override void hangup () {Console. writeLine ("Dong .. doon .. ") ;}}</SPAN> // cottage plan-Iphone class Iphone: Telephone {Public override void dial () {// unique sound Console of Nokia. writeLine ("La la, Iphone dialing... ");} public override void communicate () {Console. writeLine ("Xiao Li, I bought an apple. Envy Me... ");} public override void hangup () {Console. writeLine ("Dong .. doon .. ") ;}} client code: [csharp] <SPAN style =" FONT-SIZE: 18px "> // client static void Main (string [] args) {// use nokia Telephone nokia1 = new Nokia (); nokia1.call (); // use Iphone Telephone iph One = new Iphone (); iphone. call (); Console. read () ;}</SPAN> // client static void Main (string [] args) {// use nokia Telephone nokia1 = new Nokia (); nokia1.call (); // use the Iphone Telephone iphone = new Iphone (); iphone. call (); Console. read ();} changed the variable declaration to the parent class on the client, and the code was reused using polymorphism. After the launch: such a cheap nokia and iphone came into being. Through this simple small example, do you have a preliminary understanding of the template method mode. What are the characteristics of the template mode? 1. when we want to complete a process or a series of steps at a specific level of detail, but the implementation of individual steps at a more detailed level may be different, we usually consider using the template method for processing; 2. the template method mode demonstrates its advantages by moving the unchanged behavior to the superclass and removing repeated code from the subclass. 3. the template method provides a good platform for code reuse. 4. when the same and variable behavior are mixed in the implementation of the method subclass, the unchanged behavior will appear again in the subclass, we use the template method to move these actions to a single place, which helps the subclass to get rid of the entanglement of repeated unchanged behaviors.

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.